【发布时间】:2019-12-24 13:10:40
【问题描述】:
我遇到了if 语句的问题,并使用lambda 函数和apply 方法返回两个日期之间的差异。当条件为true 时,['conus_days'] 以纳秒为单位返回时间/天数。我的代码有什么问题?
us_bd = CustomBusinessDay(calendar=USFederalHolidayCalendar())
def get_conusdays(row):
if row['Month']== row['conus_mth']:
return forecast['Start Date'] - forecast['start_month'].apply(us_bd)
else:
return 0
forecast ['conus_days']= forecast.apply(lambda row: get_conusdays(row), axis=1)
print(forecast)
Name EID Start Date End Date Country year Month \
0 XX 123456 2019-08-01 2020-01-03 AF 2020 1
1 XT. 3456789 2019-09-22 2020-02-16 Conus 2020 1
2 MH. 456789 2019-12-05 2020-03-12 Conus 2020 1
3 DR. 789456 2019-09-11 2020-03-04 IR 2020 1
4 JR. 985756 2020-01-03 2020-05-06 GE 2020 1
days_in_month start_month end_month working_days hours conus_mth \
0 31 2020-01-01 2020-01-31 21 372 8
1 31 2020-01-01 2020-01-31 21 168 9
2 31 2020-01-01 2020-01-31 21 168 12
3 31 2020-01-01 2020-01-31 21 372 9
4 31 2020-01-01 2020-01-31 21 310 1
cd conus_days
0 -154 days 0
1 -102 days 0
2 -28 days 0
3 -113 days 0
4 1 days [-13305600000000000 nanoseconds, -881280000000...
【问题讨论】:
-
我认为您可以直接将
get_conusdays传递给apply,如下所示:forecast.apply(get_conusdays, axis=1)。
标签: python pandas lambda apply