tacpdt
tacpdt

Reputation: 79

Python - Convert epoch in object format to datetime

I'm pulling data from API that gives me date values in epoch in milliseconds, but the values are in this format:

enter image description here

I am trying to convert these to datetime with pd.to_datetime(df1.loc[:, 'Curve Value'], unit='ms'), but am getting the error

ValueError: non convertible value 1,609,459,200,000.0000000000 with the unit 'ms'

I then tried to format the column into float with df1["Curve Value"] = df["Curve Value"].astype(float), but get then get this error

ValueError: could not convert string to float: '1,609,459,200,000.0000000000'

I've also tried various ways to remove the commas and convert to float, but get errors trying that, as well.

Upvotes: 0

Views: 68

Answers (1)

norie
norie

Reputation: 9857

A bit unwieldy and requires importing datetime from datetime but it works as far as I can see.

df['Real Time'] = df['Curve Value'].apply(lambda t: datetime.fromtimestamp(int(''.join(t.split(',')[:4]))))

Upvotes: 1

Related Questions