how can i have on a new line the number of days for each month between start_date and end_date in pandas? image
this what i have
sea_start_date sea_end_date
2016-04-04 2016-07-04
2016-07-04 2016-09-04
this is what i want
sea_start_date sea_end_date
2016-04 2016-05
2016-05 2016-06
2016-06 2016-07
2016-07 2016-08
2016-08 2016-09
Thank you
I believe this is what you're looking for:
df = df.melt()
df.value = pd.to_datetime(df.value)
min_month = df["value"].min()
max_month = df["value"].max()
min_month = datetime.datetime(min_month.year, min_month.month, 1)
res = pd.DataFrame({"sea_start_date": pd.date_range(min_month, max_month, freq="MS"),
"sea_end_date": pd.date_range(min_month, max_month, freq="MS").shift()})
res = res.iloc[:-1]
print(res)
Output:
sea_start_date sea_end_date
0 2016-04-01 2016-05-01
1 2016-05-01 2016-06-01
2 2016-06-01 2016-07-01
3 2016-07-01 2016-08-01
4 2016-08-01 2016-09-01
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.