Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to convert a Pandas DataFrame column from UNIX to Datetime, but I either get a mismatch error or the new dates are all 1970-01-01... I'm stuck for hours not knowing how to fix this...

What I have tried:

When I look at how my UNIX looks like in datetime:
<pre>mydatetime = datetime.fromtimestamp(1618185600000 // 1000, tz=tzutc())
print('mydatetime',mydatetime)


I get:
Quote:
mydatetime 2021-04-12 00:00:00+00:00


So when I use the conversion function:
df2 = pd.to_datetime(df1['ds'].astype(str), format='%Y-%m-%d %H:%M:%S+%f:%Z')


I get:
Quote:
ValueError: time data '1618185600000' does not match format '%Y-%m-%d %H:%M:%S+%fZ' (match)


So, when I use the lazy road:
#df2 = pd.to_datetime(df1['ds'], unit='ns')


The results are:
Quote:
DATAFRAMED MARKET TAILED: 86 1970-01-01 00:27:05.616000
87 1970-01-01 00:27:05.702400
88 1970-01-01 00:27:05.788800
89 1970-01-01 00:27:05.875200
90 1970-01-01 00:27:05.886197
Name: ds, dtype: datetime64[ns]
Posted
Comments
Peter_in_2780 10-Jul-21 2:37am    
Count your trailing zeroes... Try your lazy one with unit='ms'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900