Code
today = pd.to_datetime('today').strftime("%d/%m/%Y")
last = (pd.to_datetime(today) - pd.DateOffset(years=1)).strftime("%d/%m/%Y")
listofmonths = pd.date_range(start=last, end=today, freq='MS')
listofmonths
Output:
DatetimeIndex(['2021-06-01', '2021-07-01', '2021-08-01', '2021-09-01',
'2021-10-01', '2021-11-01', '2021-12-01', '2022-01-01'],
dtype='datetime64[ns]', freq='MS')
Note: Refer the output in which i am getting only 8 months and i want past 12 months first date from the current date
Expected Output:
**DatetimeIndex(['2021-06-01', '2021-07-01', '2021-08-01', '2021-09-01',
'2021-10-01', '2021-11-01', '2021-12-01','2021-01-01', '2022-02-01',
'2022-03-01','2022-04-01', '2022-05-01', '2022-06-01',],
dtype='datetime64[ns]', freq='MS')**
CodePudding user response:
Try to update strftime from "%d/%m/%Y" to "%m/%d/%Y":
today = pd.to_datetime('today').strftime("%m/%d/%Y")
last = (pd.to_datetime(today) - pd.DateOffset(years=1)).strftime("%m/%d/%Y")
listofmonths = pd.date_range(start=last, end=today, freq='MS')
Output:
DatetimeIndex(['2021-06-01', '2021-07-01', '2021-08-01', '2021-09-01',
'2021-10-01', '2021-11-01', '2021-12-01', '2022-01-01',
'2022-02-01', '2022-03-01', '2022-04-01', '2022-05-01',
'2022-06-01'],
dtype='datetime64[ns]', freq='MS')
P.s. the issue was, that 'today' was treated as January, 6:
>>> last
'06/01/2021'
>>> today
'01/06/2022'
CodePudding user response:
Problem is that %d/%m/%Y
is dayfirst however by default pandas use month first
today = pd.to_datetime('today')
last = (pd.to_datetime(today) - pd.DateOffset(years=1))
listofmonths = pd.date_range(start=last, end=today, freq='MS').strftime("%d/%m/%Y")
Index(['01/06/2021', '01/07/2021', '01/08/2021', '01/09/2021', '01/10/2021',
'01/11/2021', '01/12/2021', '01/01/2022', '01/02/2022', '01/03/2022',
'01/04/2022', '01/05/2022', '01/06/2022'],
dtype='object')
CodePudding user response:
It'd be more advisable to use datetime/timestamp object for the pd.date_range
instead of string.
today_ts = pd.to_datetime('today').floor(freq='D')
last_ts = (pd.to_datetime(today_ts) - pd.DateOffset(years=1))
listofmonths = pd.date_range(start=last_ts, end=today_ts, freq='MS')
listofmonths
DatetimeIndex(['2021-06-01', '2021-07-01', '2021-08-01', '2021-09-01',
'2021-10-01', '2021-11-01', '2021-12-01', '2022-01-01',
'2022-02-01', '2022-03-01', '2022-04-01', '2022-05-01',
'2022-06-01'],
dtype='datetime64[ns]', freq='MS')
And if you need to use them as string, you may convert it later
CodePudding user response:
Try using standard datetime python library for anything datetime related.
You error might have been a type error.
This code works fine for me.
from datetime import timedelta, datetime
today = datetime.now().strftime("%Y-%m-%d")
past = (datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d")
pd.date_range(past, today, freq="MS")
Output:
DatetimeIndex(['2021-06-01', '2021-07-01', '2021-08-01', '2021-09-01',
'2021-10-01', '2021-11-01', '2021-12-01', '2022-01-01',
'2022-02-01', '2022-03-01', '2022-04-01', '2022-05-01',
'2022-06-01'],
dtype='datetime64[ns]', freq='MS')