I have start and end times that look as follows:
Start: 2017-03-05T19:18:53Z
End: 2017-03-05T19:57:54.042000Z
Current iteration that leaves me with the right format but the wrong dtype:
format_time_user_df[['start', 'end']] = format_time_user_df[['start', 'end']].apply(pd.to_datetime, errors='coerce')
cols = ['start', 'end']
format_time_user_df[cols] = format_time_user_df[cols].apply(lambda x: pd.to_datetime(x, unit='ms').dt.date)
resulting in the start and end times that look as follows:
Start: 2017-03-05
End: 2017-03-05
but these are now objects and not dates.
What I am trying to accomplish is to get the dates into ascending order and also, if there is a way to section of a week at a time (Monday through Sunday) for data analysis.
CodePudding user response:
If you use .loc[:, ...] =, your values will be updated but the column not, so:
Replace:
df.loc[:, ['start', 'end']] = ...
By:
df[['start', 'end']] = ...
Now you can use:
>>> df['start'].dt.normalize()
0 2017-03-05 00:00:00 00:00
Name: start, dtype: datetime64[ns, UTC]
Setup:
df = pd.DataFrame({'start': ['2017-03-05T19:18:53Z'],
'end': ['2017-03-05T19:57:54.042000Z']})
