Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert 16 digit number to human readable date

I have read a large csv file into a Jupyter notebook, and it contains a column of 16 digit numbers (e.g. 1352160000000000) as an Epoch/Unix timestamp.

I have checked this number on an online Epoch/Unix timestamp converter site and it returns the time I know it should (GMT: Tuesday, November 6, 2012 12:00:00 AM).

I then used the following code (adapted from other answers to this question on this site): df['TIME'] = pd.to_datetime(df['TIME'], unit='u') to convert this number to human readable form in my data frame but I either get an error, or I get (1970-01-16 15:36:00.000).

Can anyone point out to me where it is I am going wrong, and what I am missing?

like image 992
DreamingMan Avatar asked Oct 21 '25 11:10

DreamingMan


2 Answers

You need to use 'us' unit.

df['TIME'] = pd.to_datetime(df['TIME'], unit='us')

pd.to_datetime(1352160000000000,unit='us')
Out[24]: Timestamp('2012-11-06 00:00:00')
like image 56
Allen Avatar answered Oct 24 '25 02:10

Allen


The unit is microseconds, so you should use 'us' as unit:

pd.to_datetime(1352160000000000, unit='us')

for example:

>>> pd.to_datetime(1352160000000000, unit='us')
Timestamp('2012-11-06 00:00:00')
like image 30
Willem Van Onsem Avatar answered Oct 24 '25 01:10

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!