我有一个事件表,每个事件都有开始日期和结束日期,我需要得到每个事件在不同月份的天数。
例如:
Event 1 Start date: 1.1.2021 End date: 4.3.2021
January 2021: 31 days
February 2021: 28 days
March 2021: 4 days
无论如何使用pandas/python库来做这个?
使用pandas
,您可以遵循以下方法:
import pandas as pd
import datetime as dt
# YOUR INPUTS
start_date = dt.datetime(2020, 1, 1)
end_date = dt.datetime(2020, 4, 4)
# All the days between start_date and end_date
days = pd.date_range(start_date, end_date)
# Retrieve first day for each of the month within the range
d = {x.strftime("%Y%m"):x for x in sorted(days, reverse=True)}
# Append end_date
d = [*sorted(d.values()), end_date]
for d1, d2 in zip(d[:-1], d[1:]):
n_days = (d2 - d1).days + (d1.month == d2.month)
print("{:15s}:{:d}".format(d1.strftime("%B %Y"), n_days))
下面这段代码打印日期d0
和d1
之间每个月包含的天数。
打印d0
个月到d0
个月结束的天数。然后打印接下来每个月的天数,直到达到d1
month。此时,它只打印d1
日期的日期。
在本例中,datetime
和dateutil.relativedelta
用于处理datetime对象和月份增长。
from datetime import date
from dateutil.relativedelta import *
d0 = date(2008, 8, 18)
d1 = date(2009, 9, 26)
def days_in_month(month, year):
if month == 12:
return (date(year+1, 1, 1) - date(year, month, 1)).days
return (date(year, month+1, 1) - date(year, month, 1)).days
def get_month_year_string(date):
return date.strftime("%B %Y")
d = d0
while d.month < d1.month or d.year < d1.year:
days_to_end_month = days_in_month(d.month, d.year)
if d == d0:
days_to_end_month -= d0.day
print(f'{get_month_year_string(d)}: {days_to_end_month} days')
d = d + relativedelta(months=+1)
print(f'{get_month_year_string(d1)}: {d1.day} days')
# Output:
# August 2008: 13 days
# September 2008: 30 days
# October 2008: 31 days
# November 2008: 30 days
# December 2008: 31 days
# January 2009: 31 days
# February 2009: 28 days
# March 2009: 31 days
# April 2009: 30 days
# May 2009: 31 days
# June 2009: 30 days
# July 2009: 31 days
# August 2009: 31 days
# September 2009: 26 days