此数据框的输出显示每个2月份有28天
dates = pd.DataFrame({"start_date": pd.date_range(datetime.datetime(1900, 1, 1).strftime("%Y-%m-%d"), datetime.datetime(2020, 1, 1).strftime("%Y-%m-%d"), freq='MS')})
dates["start_date"] = dates["start_date"].apply(pd.datetools.normalize_date)
dates["end_date"] = dates["start_date"].shift(-1)
dates['end_date'] = dates['end_date'] - pd.offsets.Day(1)
dates["end_date"][1440]= datetime.datetime(2020, 1, 31).strftime("%Y-%m-%d")
def prettyrow(start,end):
result = end-start+ pd.offsets.Day(1)
return result
data = []
for i in range(0,dates.shape[0]):
days = prettyrow(dates["start_date"][i],dates["end_date"][i])
data.append(int(str(days)[:2]))
df = pd.DataFrame(data,columns=["days"])
timedata = pd.concat([dates, df], axis=1)
timedata
我必须单独组合这两个数据帧,因为我无法解决如何将特定数据帧列中的每一行传递给具有多个参数的函数。
根据以下评论,
的输出print(timedata [(timedata.start_date.dt.year%4 == 0)&(timedata.start_date.dt.month == 2)])
start_date end_date days
1 1900-02-01 1900-02-28 28
49 1904-02-01 1904-02-29 29
97 1908-02-01 1908-02-29 29
145 1912-02-01 1912-02-29 29
193 1916-02-01 1916-02-29 29
241 1920-02-01 1920-02-29 29
289 1924-02-01 1924-02-29 29
337 1928-02-01 1928-02-29 29
385 1932-02-01 1932-02-29 29
433 1936-02-01 1936-02-29 29
481 1940-02-01 1940-02-29 29
529 1944-02-01 1944-02-29 29
577 1948-02-01 1948-02-29 29
625 1952-02-01 1952-02-29 29
673 1956-02-01 1956-02-29 29
721 1960-02-01 1960-02-29 29
769 1964-02-01 1964-02-29 29
817 1968-02-01 1968-02-29 29
865 1972-02-01 1972-02-29 29
913 1976-02-01 1976-02-29 29
961 1980-02-01 1980-02-29 29
1009 1984-02-01 1984-02-29 29
1057 1988-02-01 1988-02-29 29
1105 1992-02-01 1992-02-29 29
1153 1996-02-01 1996-02-29 29
1201 2000-02-01 2000-02-29 29
1249 2004-02-01 2004-02-29 29
1297 2008-02-01 2008-02-29 29
1345 2012-02-01 2012-02-29 29
1393 2016-02-01 2016-02-29 29
以下评论的输出是:
start_date end_date days
1 1900-02-01 1900-02-28 28
49 1904-02-01 1904-02-29 29
97 1908-02-01 1908-02-29 29
145 1912-02-01 1912-02-29 29
193 1916-02-01 1916-02-29 29
241 1920-02-01 1920-02-29 29
289 1924-02-01 1924-02-29 29
337 1928-02-01 1928-02-29 29
385 1932-02-01 1932-02-29 29
433 1936-02-01 1936-02-29 29
481 1940-02-01 1940-02-29 29
529 1944-02-01 1944-02-29 29
577 1948-02-01 1948-02-29 29
625 1952-02-01 1952-02-29 29
673 1956-02-01 1956-02-29 29
721 1960-02-01 1960-02-29 29
769 1964-02-01 1964-02-29 29
817 1968-02-01 1968-02-29 29
865 1972-02-01 1972-02-29 29
913 1976-02-01 1976-02-29 29
961 1980-02-01 1980-02-29 29
1009 1984-02-01 1984-02-29 29
1057 1988-02-01 1988-02-29 29
1105 1992-02-01 1992-02-29 29
1153 1996-02-01 1996-02-29 29
1201 2000-02-01 2000-02-29 29
1249 2004-02-01 2004-02-29 29
1297 2008-02-01 2008-02-29 29
1345 2012-02-01 2012-02-29 29
1393 2016-02-01 2016-02-29 29