如何从 Pandas DataFrame.Index 谁的对象类型 datetime.time 中 add/subtract 时间(小时、分钟等)?
How to add/subtract time (hours, minutes, etc.) from a Pandas DataFrame.Index whos objects are of type datetime.time?
我有一个 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
Liam 的 link 看起来不错,但也可以查看 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)
这个对我有用:
>> print(df)
TotalVolume Symbol
2016-04-15 09:00:00 108400 2802.T
2016-04-15 09:05:00 50300 2802.T
>> print(df.set_index(pd.to_datetime(df.index.values) - datetime(2016, 4, 15)))
TotalVolume Symbol
09:00:00 108400 2802.T
09:05:00 50300 2802.T
Philippe 解决方案但更清洁:
我的减法数据是:'2018-09-22T11:05:00.000Z'
import datetime
import pandas as pd
df_modified = pd.to_datetime(df_reference.index.values) - datetime.datetime(2018, 9, 22, 11, 5, 0)
我有一个 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
Liam 的 link 看起来不错,但也可以查看 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)
这个对我有用:
>> print(df)
TotalVolume Symbol
2016-04-15 09:00:00 108400 2802.T
2016-04-15 09:05:00 50300 2802.T
>> print(df.set_index(pd.to_datetime(df.index.values) - datetime(2016, 4, 15)))
TotalVolume Symbol
09:00:00 108400 2802.T
09:05:00 50300 2802.T
Philippe 解决方案但更清洁:
我的减法数据是:'2018-09-22T11:05:00.000Z'
import datetime
import pandas as pd
df_modified = pd.to_datetime(df_reference.index.values) - datetime.datetime(2018, 9, 22, 11, 5, 0)