4

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. Help would be much appriciated. Thanks.

Swati
  • 535
  • 11
  • 25
  • It runs fine for me. The result is `[1447322854000, 1447322855000]`. What error are you facing? – meW Feb 28 '19 at 04:10
  • @meW Error is: ```TypeError: Unrecognized value type: 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') ``` – Swati Feb 28 '19 at 04:44
  • Then it's the problem with your `TimeStamp` data. It may have some spaces at the end or a similar problem. Refer to @jezrael solution [here](https://stackoverflow.com/a/34507381/10734525) to find out the bug in your data or else share with us how are you creating this dataframe. – meW Feb 28 '19 at 04:47
  • yes, there's a rogue unparsable string in your dataset, either locate and investigate or use coerce=True. – Andy Hayden Feb 28 '19 at 05:06

1 Answers1

8

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
Swati
  • 535
  • 11
  • 25