python - Hi, I am trying to add weekstart column -
in current table having date column , column able find out weekday.by using to_timedelta have created week_start column not giving correct date. here code is:
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)
output as:
date weekday weekstart 2016-07-23 5 2016-07-22
iiuc can construct timedeltaindex , subtract other column:
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 fact weekday column unnecessary:
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
Comments
Post a Comment