【发布时间】:2021-08-19 17:02:57
【问题描述】:
我有一个带有日期时间索引的时间序列数据框“his”,我想从中提取一定范围的时间步长。起点“selected_var_start”采用日期时间索引格式(从另一个数据帧中提取)。我想总共提取 24 小时。
我尝试这样做:his = his.iloc[selected_var_start:(selected_var_start+pd.DateOffset(hours=24))]
我收到以下错误TypeError: cannot do positional indexing on DatetimeIndex with these indexers [DatetimeIndex(['2010-11-12 19:00:00'], dtype='datetime64[ns]', name='Datetime', freq=None)] of type DatetimeIndex
我做错了什么?
【问题讨论】:
-
如果我使用
his = his.loc[selected_var_start:(selected_var_start+pd.DateOffset(hours=24))]我会得到 "pandas.errors.InvalidIndexError: DatetimeIndex(['2010-11-12 19:00:00'], dtype='datetime64[ns]', name ='日期时间', freq=None)" -
这只是一般建议;如需详细解答,请提供minimal reproducible example。
标签: python pandas datetime indexing