1

I have a pandas dataframe with following index:

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)

How can I extend this index to present date i.e. 15th July, 2017?

user308827
  • 21,227
  • 87
  • 254
  • 417

1 Answers1

1

You can use Index.union with date_range and max value of 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')

Or select last value by [-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')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252