2017-07-15 140 views
1

我有以下指數大熊貓數據幀:擴展大熊貓日期時間指數當前日期

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日?

回答

1

您可以使用Index.uniondate_rangemax價值index

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') 
+0

的感謝!這工作 – user308827