如何从Pandas DataFrame.Index中添加/减去时间(小时,分钟等),而对象的类型是datetime.time?

Cam*_*ark 38 python time datetime pandas

我有一个DataFrame谁的索引只是datetime.time而且DataFrame.Index和datetime.time中没有方法来改变时间.datetime.time已经替换但是只能用于系列的各个项目?

以下是使用的索引的示例:

In[526]:  dfa.index[:5]
Out[526]: Index([21:12:19, 21:12:20, 21:12:21, 21:12:21, 21:12:22], dtype='object')

In[527]:  type(dfa.index[0])
Out[527]: datetime.time
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex 51

利亚姆的链接看起来很棒,但也看看pandas.Timedelta- 看起来它与NumPy和Python的时间增量很好地配合.

https://pandas.pydata.org/pandas-docs/stable/timedeltas.html

pd.date_range('2014-01-01', periods=10) + pd.Timedelta(days=1)
Run Code Online (Sandbox Code Playgroud)

  • 使用df.index + pd.Timedelta(hours = 12)给出了错误"TypeError:notpported operand type(s)for +:'numpy.ndarray'和'Timedelta'".索引本身是pandas.core.index.Index,但每个值都是datetime.time,它以某种方式干扰tshift()方法以及Timedelta添加. (3认同)
  • df_series.index = (df_series.index + pd.Timedelta(天=1)) (2认同)