consider the DateTimeIndex
dates
dates = pd.date_range('2016-01-29', periods=4, freq='BM')
dates
DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29'],
dtype='datetime64[ns]', freq='BM')
I want to extend the index by one period at the frequency attached to the object.
I expect
pd.date_range('2016-01-29', periods=5, freq='BM')
DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29',
'2016-05-31'],
dtype='datetime64[ns]', freq='BM')
I've tried
dates.append(dates[[-1]] + pd.offsets.BusinessMonthEnd())
However
dates
PerformanceWarning: Non-vectorized DateOffset being applied to Series or DatetimeIndex
The best solution is:
import pandas as pd
dates = pd.date_range('2016-01-29', periods=4, freq='BM')
extended = dates.union(dates.shift(n)[-n:])
where n is the number of periods you want to add. With n=4
, you will get an extended date range like this:
DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29',
'2016-05-31', '2016-06-30', '2016-07-29', '2016-08-31'],
dtype='datetime64[ns]', freq='BM')
To follow up on this, for pandas==1.1.1
, I found this to be the best solution:
dates.union(pd.date_range(dates[-1] + dates.freq, periods=1, freq=dates.freq))
n=3
dates.union(pd.date_range(dates[-1] + dates.freq, periods=n, freq=dates.freq))
Taken by combining @alberto-garcia-raboso's answer and @ballpointben's comment.
Index
, not a DateTimeIndex
:
dates.union([dates[-1] + dates.freq])
dates[-1] + 1
is deprecated.The timestamps in your DatetimeIndex
already know that they are describing business month ends, so you can simply add 1:
import pandas as pd
dates = pd.date_range('2016-01-29', periods=4, freq='BM')
print(repr(dates[-1]))
# => Timestamp('2016-04-29 00:00:00', offset='BM')
print(repr(dates[-1] + 1))
# => Timestamp('2016-05-31 00:00:00', offset='BM')
You can add the latter to your index using .union
:
dates = dates.union([dates[-1] + 1])
print(dates)
# => DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29',
# '2016-05-31'],
# dtype='datetime64[ns]', freq='BM')
Compared to .append
, this retains knowledge of the offset.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With