在我当前的表中,我有日期列,从该列我可以找到工作日。通过使用to_timedelta,我创建了week_start列,但它没有给出正确的日期。 这里的代码是:
final_data['weekday'] = final_data['DateOfInvoice'].dt.weekday
final_data['Weekstart'] = final_data['DateOfInvoice'] - pd.to_timedelta(final_data['weekday'],unit='ns', box=True, coerce=True)
输出为:
Date weekday weekstart
2016-07-23 5 2016-07-22
答案 0 :(得分:1)
IIUC你可以构造一个TimedeltaIndex并从另一列中减去:
In [152]:
df['weekstart'] = df['Date'] - pd.TimedeltaIndex(df['weekday'], unit='D')
df
Out[152]:
Date weekday weekstart
0 2016-07-23 5 2016-07-18
实际上工作日专栏是不必要的:
In [153]:
df['weekstart'] = df['Date'] - pd.TimedeltaIndex(df['Date'].dt.dayofweek, unit='D')
df
Out[153]:
Date weekday weekstart
0 2016-07-23 5 2016-07-18