I am pretty new to pandas and I am struggling some dates in columns.
I have a dataframe column with dates in multiple formats, such as '2021-08-21T06:34:34.909Z' and '2021-08-21T06:34:34Z'
Is there an elegant way to remove all the milliseconds?
Thanks in advance
CodePudding user response:
You can try to convert as datetime64[s]
:
>>> df['Datetime']
0 2021-08-21T06:34:34.909Z
1 2021-08-21T06:34:34Z
Name: Datetime, dtype: object
>>> df['Datetime'].astype('datetime64[s]')
0 2021-08-21 06:34:34
1 2021-08-21 06:34:34
Name: Datetime, dtype: datetime64[ns]
# OR
>>> pd.to_datetime(df['Datetime']).dt.floor(freq='S')
0 2021-08-21 06:34:34 00:00
1 2021-08-21 06:34:34 00:00
Name: Datetime, dtype: datetime64[ns, UTC]