请你澄清我在这里做错了什么。我想创建一个新的日期列,其中新日期与列#34; inc_cr_date"相同如果" inc_cr_date_day"不是"星期天"或"星期六"。星期六或星期日,我希望新列为" inc_cr_date"分别为+1和+2天。
df是:
inc_cr_date inc_cr_date_day
2017-10-26 21:59:28.075 Thursday
2017-10-27 08:17:02.718 Friday
2017-10-22 14:44:07.695 Sunday
代码(已更新):
df['inc_cr_date_day'] = df['inc_cr_date'].dt.weekday_name
df['inc_cr_date_adjusted'] = np.select([(df['inc_cr_date_day'] == 'Saturday'),#condition working
(df['inc_cr_date_day'] == 'Sunday'),#condition working
((df['inc_cr_date_day'] == 'Monday')& (df['inc_cr_date'].dt.hour > 18 ) & df['inc_cr_date'].dt.minute > 30),
((df['inc_cr_date_day'] == 'Monday')& (df['inc_cr_date'].dt.hour < 9 ) & df['inc_cr_date'].dt.minute < 30),
((df['inc_cr_date_day'] == 'Tuesday')& (df['inc_cr_date'].dt.hour > 18 ) & df['inc_cr_date'].dt.minute > 30),
((df['inc_cr_date_day'] == 'Tuesday')& (df['inc_cr_date'].dt.hour < 9 ) & df['inc_cr_date'].dt.minute < 30),
((df['inc_cr_date_day'] == 'Wednesday')& (df['inc_cr_date'].dt.hour > 18 ) & df['inc_cr_date'].dt.minute > 30),
((df['inc_cr_date_day'] == 'Wednesday')& (df['inc_cr_date'].dt.hour < 9 ) & df['inc_cr_date'].dt.minute < 30),
((df['inc_cr_date_day'] == 'Thursday')& (df['inc_cr_date'].dt.hour > 18 ) & df['inc_cr_date'].dt.minute > 30),
((df['inc_cr_date_day'] == 'Thursday')& (df['inc_cr_date'].dt.hour < 9 ) & df['inc_cr_date'].dt.minute < 30),
((df['inc_cr_date_day'] == 'Friday')& (df['inc_cr_date'].dt.hour > 18 ) & df['inc_cr_date'].dt.minute > 30),
((df['inc_cr_date_day'] == 'Friday')& (df['inc_cr_date'].dt.hour < 9 ) & df['inc_cr_date'].dt.minute < 30)],
[(df['inc_cr_date']+pd.Timedelta('2 days')).dt.normalize() + pd.Timedelta('9 Hours 30 Minutes'),
(df['inc_cr_date']+pd.Timedelta('1 days')).dt.normalize() + pd.Timedelta('9 Hours 30 Minutes'),
(df['inc_cr_date']+pd.Timedelta('1 days')).dt.normalize() + pd.Timedelta('9 Hours 30 Minutes'),
(df['inc_cr_date']+pd.Timedelta('0 days')).dt.normalize() + pd.Timedelta('9 Hours 30 Minutes'),
(df['inc_cr_date']+pd.Timedelta('1 days')).dt.normalize() + pd.Timedelta('9 Hours 30 Minutes'),
(df['inc_cr_date']+pd.Timedelta('0 days')).dt.normalize() + pd.Timedelta('9 Hours 30 Minutes'),
(df['inc_cr_date']+pd.Timedelta('1 days')).dt.normalize() + pd.Timedelta('9 Hours 30 Minutes'),
(df['inc_cr_date']+pd.Timedelta('0 days')).dt.normalize() + pd.Timedelta('9 Hours 30 Minutes'),
(df['inc_cr_date']+pd.Timedelta('1 days')).dt.normalize() + pd.Timedelta('9 Hours 30 Minutes'),
(df['inc_cr_date']+pd.Timedelta('0 days')).dt.normalize() + pd.Timedelta('9 Hours 30 Minutes'),
(df['inc_cr_date']+pd.Timedelta('3 days')).dt.normalize() + pd.Timedelta('9 Hours 30 Minutes'),
(df['inc_cr_date']+pd.Timedelta('0 days')).dt.normalize() + pd.Timedelta('9 Hours 30 Minutes')],
df['inc_cr_date'])
非常感谢您的投入。
答案 0 :(得分:3)
您发布的问题是向list
['inc_cr_date'] + pd.DateOffset(1)
添加timedelta。试试row['inc_cr_date'] + pd.DateOffset(1)
所以代替if,elseif,否则我们可以使用np.select
和+ pd.Timedelta
的力量,即
df['inc_cr_date2'] = np.select([(df['inc_cr_date_day'] == 'Saturday'),
(df['inc_cr_date_day'] == 'Sunday')],
[df['inc_cr_date']+pd.Timedelta('1 days'),
df['inc_cr_date']+pd.Timedelta('2 days')],
df['inc_cr_date'])
inc_cr_date inc_cr_date_day inc_cr_date2 0 2017-10-26 21:59:28.075 Thursday 2017-10-26 21:59:28.075 1 2017-10-27 08:17:02.718 Friday 2017-10-27 08:17:02.718 2 2017-10-22 14:44:07.695 Sunday 2017-10-24 14:44:07.695
在特定时间,我们可以使用标准化并添加您想要的小时数,即
df['inc_cr_date2'] = np.select([(df['inc_cr_date_day'] == 'Saturday'),
(df['inc_cr_date_day'] == 'Sunday')],
[(df['inc_cr_date']+pd.Timedelta('1 days')).dt.normalize() + pd.Timedelta('9 Hours'),
(df['inc_cr_date']+pd.Timedelta('2 days')).dt.normalize() + pd.Timedelta('9 Hours')],
df['inc_cr_date'])
inc_cr_date inc_cr_date_day inc_cr_date2 0 2017-10-26 21:59:28.075 Thursday 2017-10-26 21:59:28.075 1 2017-10-27 08:17:02.718 Friday 2017-10-27 08:17:02.718 2 2017-10-22 14:44:07.695 Sunday 2017-10-24 09:00:00.000