Python 将datetime索引扩展到当前日期
我有一个熊猫数据框,索引如下:Python 将datetime索引扩展到当前日期,python,pandas,Python,Pandas,我有一个熊猫数据框,索引如下: DatetimeIndex(['2013-04-16', '2013-04-17', '2013-04-18', '2013-04-19', '2013-04-20', '2013-04-21', '2013-04-22', '2013-04-23', '2013-04-24', '2013-04-25', ... '2017-07-03', '
DatetimeIndex(['2013-04-16', '2013-04-17', '2013-04-18', '2013-04-19',
'2013-04-20', '2013-04-21', '2013-04-22', '2013-04-23',
'2013-04-24', '2013-04-25',
...
'2017-07-03', '2017-07-04', '2017-07-05', '2017-07-06',
'2017-07-07', '2017-07-08', '2017-07-09', '2017-07-10',
'2017-07-11', '2017-07-12'],
dtype='datetime64[ns]', length=406, freq=None)
如何将此索引扩展到当前日期,即2017年7月15日?您可以使用索引的最大值和值:
idx = pd.DatetimeIndex(['2017-07-07', '2017-07-08', '2017-07-09', '2017-07-10',
'2017-07-11', '2017-07-12'])
idx = pd.date_range(idx.max(),pd.datetime.today()).union(idx)
print (idx)
DatetimeIndex(['2017-07-07', '2017-07-08', '2017-07-09', '2017-07-10',
'2017-07-11', '2017-07-12', '2017-07-13', '2017-07-14',
'2017-07-15'],
dtype='datetime64[ns]', freq='D')
或通过[-1]
选择最后一个值:
idx = pd.date_range(idx[-1],pd.datetime.today()).union(idx)
print (idx)
DatetimeIndex(['2017-07-07', '2017-07-08', '2017-07-09', '2017-07-10',
'2017-07-11', '2017-07-12', '2017-07-13', '2017-07-14',
'2017-07-15'],
dtype='datetime64[ns]', freq='D')