I got DataFrame with columns 'start_date' and 'end_date'.
start_date finish_date
0 2019-06-16 2019-06-23
1 2019-05-29 2019-06-05
2 2019-03-26 2019-03-28
3 2019-04-22 2019-04-24
4 2019-05-08 2019-05-08
I want to create a column that will contain a list of months in this range, like this:
start_date finish_date range
0 2019-06-16 2019-06-23 [2019-06]
1 2019-05-29 2019-06-05 [2019-05, 2019-06]
2 2019-03-26 2019-03-28 [2019-03]
3 2019-04-22 2019-08-24 [2019-04, 2019-05, 2019-06, 2019-07]
4 2018-12-08 2019-02-08 [2018-12, 2019-01, 2019-02]
I tried to use period_range:
df['range'] = df.apply(lambda x: pd.period_range(start=df['start_date'], end=df['finish_date'], freq='M'))
And something like this, but I got only errors. Can You, please, help me - is it possible to use period_range/date_range to solve my problem?
Thank You for Your time!
Try:
df['range'] = pd.Series([pd.date_range(i, j, freq='D').strftime('%Y-%m').unique().to_numpy()
for i, j in zip(df['start_date'], df['finish_date'])])
print(df)
Output:
start_date finish_date range
0 2019-06-16 2019-06-23 [2019-06]
1 2019-05-29 2019-06-05 [2019-05, 2019-06]
2 2019-03-26 2019-03-28 [2019-03]
3 2019-04-22 2019-08-24 [2019-04, 2019-05, 2019-06, 2019-07, 2019-08]
4 2018-12-08 2019-02-08 [2018-12, 2019-01, 2019-02]
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