how to change datetime format in pandas. fastest way?

I import from .csv and get object-type columns:

    begin                           end
0   2019-03-29 17:02:32.838469+00   2019-04-13 17:32:19.134874+00
1   2019-06-13 19:22:19.331201+00   2019-06-13 19:51:21.987534+00
2   2019-03-27 06:56:51.138795+00   2019-03-27 06:56:54.834751+00
3   2019-05-28 11:09:29.320478+00   2019-05-29 06:47:21.794092+00
4   2019-03-24 07:03:03.582679+00   2019-03-24 09:50:32.595199+00

I need to get in datetime format:

    begin       end
0   2019-03-29  2019-04-13
1   2019-06-13  2019-06-13
2   2019-03-27  2019-03-27
3   2019-05-28  2019-05-29
4   2019-03-24  2019-03-24

what I do (at first I make them datetime format, then cut to dates only, then make datetime format again):

df['begin'] = pd.to_datetime(df['begin'], dayfirst=False)
df['end'] = pd.to_datetime(df['end'], dayfirst=False)

df['begin'] = df['begin'].dt.date
df['end'] = df['end'].dt.date

df['begin'] = pd.to_datetime(df['begin'], dayfirst=False)
df['end'] = pd.to_datetime(df['end'], dayfirst=False)

Is there any short way to do this without converting 2 times?



Read more here: https://stackoverflow.com/questions/68488294/how-to-change-datetime-format-in-pandas-fastest-way

Content Attribution

This content was originally published by Johnny at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: