I want to select data from the last 4 months. I would want to start from the beginning of the month, so if it is currently July 28, I would want data from March1-July28.
Currently I use DateOffset, and I realized that it is calling March28-July28 and leaving out a lot of my data.
df = pd.read_csv('MyData.csv')
df['recvd_dttm'] = pd.to_datetime(df['recvd_dttm'])
#Only retrieve data before now (ignore typos that are future dates)
mask = df['recvd_dttm'] <= datetime.datetime.now()
df = df.loc[mask]
# get first and last datetime for final week of data
range_max = df['recvd_dttm'].max()
range_min = range_max - pd.DateOffset(months=4)
# take slice with final week of data
df = df[(df['recvd_dttm'] >= range_min) &
(df['recvd_dttm'] <= range_max)]
I looked up other answers and found this one: How do I calculate the date six months from the current date using the datetime Python module? So I tried using relativedelta(months=-4)
and got a ValueError: Length mismatch: Expected axis has 1 elements, new values have 3 elements
Any help would be appreciated.