Python 数据帧中的Datetime对象,仅包含时间

Python 数据帧中的Datetime对象,仅包含时间,python,pandas,datetime,object,indexing,Python,Pandas,Datetime,Object,Indexing,这是普通柱 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_d

这是普通柱

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())
您可以使用设置
时间
列作为数据帧的索引

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评论后更新 如果没有
日期
列,那么熊猫将在将其转换为
日期时间
时附加默认日期
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]

主要问题是我无法将该列的dtype:object更改为datetime,并将该列作为datetime放入索引中…@renton您是否也有
date
列?如果没有,我已经提供了一个解决方案。请检查我的最新答案。不,我没有,但我只需要时间,我不想要日期,这是可能的,或者程序总是需要一个日期来制作我想要的东西。如果你没有给出日期,它会添加一个默认日期。请检查我答案的第二部分。是的,那将是理想的。