Python:在dataset中使用单独的日期和时间值为dataframe分配时间戳

Python:在dataset中使用单独的日期和时间值为dataframe分配时间戳,python,pandas,timestamp,time-series,Python,Pandas,Timestamp,Time Series,我使用df=pd.to_csv(file,header=0)加载csv,我有两列,Date和Time,格式如下: 20120718(yyyymmdd)和15:59:56.319000(H:M:S:MS) 如何将两者结合起来,以便使用此日期和时间戳列设置数据帧df索引,并具有适当的索引时间序列 df.sort_index(by=['Date', 'Time'], inplace=True) df.index = np.arange(1, len(df) + 1) 这是最简单的方法。如果您确实希望

我使用
df=pd.to_csv(file,header=0)
加载csv,我有两列,
Date
Time
,格式如下:
20120718
(yyyymmdd)和
15:59:56.319000
(H:M:S:MS)

如何将两者结合起来,以便使用此日期和时间戳列设置数据帧
df
索引,并具有适当的索引时间序列

df.sort_index(by=['Date', 'Time'], inplace=True)
df.index = np.arange(1, len(df) + 1)
这是最简单的方法。如果您确实希望将时间序列作为索引,则:

df['timeseries'] = pd.to_datetime(df.Date + ' ' + df.Time, unit='ms')

谢谢,但df.日期在
int
中。所以我使用上面的代码:
df['timeseries']=pd.to_datetime(df.Date+''+df.Time,unit='ms')
并将
str
添加到df.Date中,它会把一切都搞糟。你说得对,它不能作为int来工作。我还以为它是一个字符串。试试
(df.Date.astype(str)+''+df.Time,单位为毫秒)