Turn all index values to datetime when index is both timestamp and datetime

Let’s try to identify all the rows that are timestamp and convert them separately:

mask = df['time'].str.contains(' ')

df['time'] = (pd.to_datetime(df.loc[mask,'time'])
                   .reindex(df.index)
                   .fillna(pd.to_datetime(df.loc[~mask, 'time'], unit='ms'))
                )

Output:

                     time     C1
0 2020-10-18 13:38:43.349  0.046
1 2020-10-18 13:52:34.104  0.099
2 2020-10-16 05:07:39.304  1.000
3 2020-10-16 05:08:54.121  0.007

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top