DataFrame中只有时间的Datetime对象



这是普通列

0       06:55:22
1       06:55:23
2       06:55:24
3       06:55:25
4       06:55:26

我想把那列放在索引中,问题是当我尝试使用方法resample((时,我总是会遇到同样的问题:

TypeError:仅对DatetimeIndex、TimedeltaIndex或PeriodIndex有效,但获得了"Index"的实例

我一直在使用它将时间列更改为

datetime dt['Time'] = pd.to_datetime(dt['Time'],format).apply(lambda x: x.time())

您可以使用set_indexTime列设置为数据帧的索引。

In [1954]: df.set_index('Time')                                                                                                                                                                             
Out[1954]: 
a
Time       
06:55:23  1
06:55:24  2
06:55:25  3
06:55:26  4

OP评论后更新

如果您没有date列,那么panda将在您将其转换为datetime时附加默认日期1900-01-01。像这样:

In [1985]: pd.to_datetime(df['Time'], format='%H:%M:%S')                                                                                                                                                    
Out[1985]: 
0   1900-01-01 06:55:23
1   1900-01-01 06:55:24
2   1900-01-01 06:55:25
3   1900-01-01 06:55:26
Name: Time, dtype: datetime64[ns]

最新更新