I seem to be coming across an issue in trying to add columns to my dataframe, specific to each month for a given start date and end date.
Input:
In [1]: df = pd.DataFrame([[01/04/2022, 03/05/2022], [07/04/2022,01/05/2022]], columns=['Contract From', '
Contract To'])
I apply the following to the specific columns I need to use:
df['Contract To'] = pd.to_datetime(df['Contract To'], dayfirst=True)
df['Start Date'] = df['Contract From']
df['End Date'] = df['Contract To']
df['End Date'].fillna("31/03/2023", inplace=True)
df['Start Date'] = pd.to_datetime(df['Start Date'])
df['End Date'] = pd.to_datetime(df['End Date'])
Then I try the following:
res = df.join(df.apply(lambda x: pd.Series(pd.date_range(x['Start Date'], x['End Date'], freq='D').to_period('M')), axis=1).apply(pd.value_counts, axis=1).fillna(0).astype(int))
However on opening the new excel spreadsheet that is saved I'm not seeing any additional columns being added. I'd expect to see the following:
Start Date | End Date | 2022-04 | 2022-05 | 2022-06 |
---|---|---|---|---|
01/04/2022 | 03/05/2022 | 30 | 3 | 0 |
07/04/2022 | 01/05/2022 | 23 | 1 | 0 |
Any help with this would be greatly appreciated, I think this is throwing me as I'm not getting any error pop up in relation to the above so not sure what's exactly going wrong here
Edit - Added example input