Search code examples
pythonpandasdatetimeepoch

Date time string to epoch: pandas dataframe


One of my pandas df has date time string column. The format is given below:

TimeStamp                 value
11/12/2015 10:07:34 AM   24.5
11/12/2015 10:07:35 AM   55.1
so on

I tried to convert the values of the column TimeStamp to epoch using:

dataframe['TimeStamp'] = pd.to_datetime(dataframe['TimeStamp']).values.astype(np.int64) // 10 ** 6

I am getting error while converting date time string to unix timestamp:

TypeError: Unrecognized value type: <class 'str'>
  File "/usr/local/lib/python3.7/site-packages/dateutil/parser/_parser.py", line 649, in parse
    raise ValueError("Unknown string format:", timestr)
ValueError: ('Unknown string format:', 'TimeStamp')

Solution

  • I figured out that the values of TimeStamp are not string. Thanks @meW for mentioning that there may be some problem in the data.

    So, I converted the values to string before converting string data time to epoch.

    dataframe['TimeStamp'] = dataframe['TimeStamp'].astype(str)
    dataframe['TimeStamp'] = (pd.to_datetime(dataframe['TimeStamp'])
                              .values.astype(np.int64) // 10 ** 6)