What I am trying to do
I am trying to create a list or array of dates all falling on a specific day of the month, and then changing the first item. E.g. something like:
20-Jan-2023,15-Feb-2023,15-Mar-2023, etc.
I am sure this is documented somewhere, but I find the quality of pandas documentation, at least when it comes to dates, absolutely atrocious and I have already banged my head against the wall for a while, so please be gentle if this has already been answered elsewhere :)
What I have tried but doesn't work (pandas.date_range and bdate_range)
I have tried with pandas.date_range
and bdate_range
, but I have 2 problems:
- I cannot change any item in the output generated below
- If there is a way to use these functions to generate a list of dates every 15th day of a month, I have not found it. The docs for
date_range
https://pandas.pydata.org/docs/reference/api/pandas.date_range.html don't mention what the valid options forfreq
are (see why I find the docs atrocious?). There is a list here https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases but what I need doesn't seem to be there
What I have tried is:
import pandas as pd
x = pd.bdate_range(start='15-Jan-2023', periods=60, freq='MS')
x[0] = pd.to_datetime('15-Jan-2023') # doesn't work
TypeError: Index does not support mutable operations
What works (but is convoluted, there must be a better way)
The code below works, but it seems convoluted and clunky. I want to hope there is a better way? I create a dataframe with a column of months to add (zero to 60), then apply DateOffset to each row. Also, this is not vectorised and likely to be slow (not in this toy example but maybe on large datasets).
import pandas as pd
import numpy as np
day_0 = pd.to_datetime('15-Jan-2023')
df = pd.DataFrame()
df['months to add'] = np.arange(0,60)
df['dates'] = df.apply(lambda x: day_0 + pd.DateOffset(months = x['months to add'] ), axis=1)
df.loc[0,'dates'] = pd.to_datetime('20-Jan-2023')
df = df.drop('months to add', axis=1)