Skip to content Skip to sidebar Skip to footer

Extend Pandas Datetime Index To Present Date

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', '201

Solution 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')

Post a Comment for "Extend Pandas Datetime Index To Present Date"