Python 使用列表中的日期及其偏移量作为for循环中切片的范围来切片数据帧
这是以日历日频率和DateTime对象作为索引的my DataFrame df。该数据自1989年1月3日起至今:Python 使用列表中的日期及其偏移量作为for循环中切片的范围来切片数据帧,python,pandas,datetime,timestamp,time-series,Python,Pandas,Datetime,Timestamp,Time Series,这是以日历日频率和DateTime对象作为索引的my DataFrame df。该数据自1989年1月3日起至今: Price 1989-01-03 30.620064 1989-01-04 30.540062 1989-01-05 30.590061 1989-01-06 30.600063 1989-01-07 NaN 1989-01-08
Price
1989-01-03 30.620064
1989-01-04 30.540062
1989-01-05 30.590061
1989-01-06 30.600063
1989-01-07 NaN
1989-01-08 NaN
1989-01-09 30.610062
1989-01-10 30.590061
1989-01-11 30.660061
1989-01-12 30.660061
这是我的df.index.values:
array(['1989-01-03T00:00:00.000000000', '1989-01-04T00:00:00.000000000',
'1989-01-05T00:00:00.000000000', ...,
'2017-06-24T00:00:00.000000000', '2017-06-25T00:00:00.000000000',
'2017-06-26T00:00:00.000000000'], dtype='datetime64[ns]')
我有一份日期清单:
[Timestamp('1989-02-01 00:00:00'),Timestamp('1989-03-01 00:00:00'),Timestamp('1989-04-01 00:00:00'),Timestamp('1989-05-01 00:00:00'),Timestamp('1989-06-01 00:00:00'),Timestamp('1989-07-01 00:00:00'),Timestamp('1989-08-01 00:00:00'),Timestamp('1989-09-01 00:00:00'),Timestamp('1989-10-01 00:00:00')]
我想为dates列表中的每个日期分割df,以便获得dates-100和dates(日历日)之间的数据
我得到这个错误:
TypeError: cannot do slice indexing on <class 'pandas.core.indexes.datetimes.DatetimeIndex'> with these indexers [1988-10-24] of <class 'datetime.date'>
TypeError:无法使用的这些索引器[1988-10-24]对进行切片索引
有人能帮我解决这个错误吗?并提出更好的方法 我想我已经发现了我的错误
我应该使用gg=df.loc[(m-DateOffset(days=100)):m]来获得这个工作我想我解决了这个问题,我的错误是使用了我应该使用的iloc.loc
TypeError: cannot do slice indexing on <class 'pandas.core.indexes.datetimes.DatetimeIndex'> with these indexers [1988-10-24] of <class 'datetime.date'>