1

I want to convert index of my time series DataFrame into datetime. The problem is that some of index is timestamp and some datetime.

time                     C1
2020-10-18 13:38:43.349  0.046   
2020-10-18 13:52:34.104  0.099  
1602824859304            1.000   
1602824934121            0.007   

This: df['time'] = pd.to_datetime(df['time'], unit='ms')
Yields: ValueError: non convertible value time with the unit 'ms'

This: df["time"] = df["time"].apply(lambda x: pd.to_datetime(x,errors='ignore').strftime('%Y-%m-%d %H:%M') if len(x) !=0 else "----")
Yields: AttributeError: 'str' object has no attribute 'strftime'

This is a similar question but isn't applicable in my case:
Convert dataframe column to datetime only if length of string is not zero

My expected output is all index rows being datetime format.

normal_human
  • 165
  • 9

1 Answers1

1

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
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74