python - DateOffset from Year to Month and Week -
i pulling chunk of data within range of time. pulling date , times column recvd_dttm. takes data starting year ago. want modify can pull month or day, pd.dateoffset(months=1) giving keyerror:1
error. same error if change days=7. works fine years=1. going on here?
df = pd.read_csv('mydata.csv') # filter countries @ least 1 medal , sort df['recvd_dttm'] = pd.to_datetime(df['recvd_dttm']) #only retrieve data before (ignore typos future dates) mask = df['recvd_dttm'] <= datetime.datetime.now() df = df.loc[mask] # first , last datetime final week of data range_max = df['recvd_dttm'].max() range_min = range_max - pd.dateoffset(years=1) # take slice final week of data df = df[(df['recvd_dttm'] >= range_min) & (df['recvd_dttm'] <= range_max)]
edit: problem coming elsewhere in code!
have tried being more explicit pd.dateoffset acting on?
for example:
range_max = df['recvd_dttm'].max() range_min = range_max - (df['recvd_dttm']+pd.dateoffset(years=1))
then substitute month , days values.
Comments
Post a Comment