Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutOfBoundsDatetime: cannot convert input 1575262698000.0 with the unit 's'

Tags:

python

pandas

I am trying to turn that number from a dataframe into a datetime, the colum time['date'] looks like the following:

0     1.575263e+12
1     1.575263e+12
2     1.575263e+12
3     1.575263e+12
4     1.575307e+12
          ...     
95    1.576521e+12
96    1.576521e+12
97    1.576521e+12
98    1.576521e+12
99    1.576521e+12
Name: date, Length: 100, dtype: float64

I tried the following:

print(pd.to_datetime(time['date'], unit='s'))

But I had the issue in the title, and if I try without unit='s' the column looks as follow:

0    1970-01-01 00:26:15.262698000
1    1970-01-01 00:26:15.262699000
2    1970-01-01 00:26:15.262698000
3    1970-01-01 00:26:15.262701000
4    1970-01-01 00:26:15.307111000
                  ...             
95   1970-01-01 00:26:16.521124623
96   1970-01-01 00:26:16.521116000
97   1970-01-01 00:26:16.521118000
98   1970-01-01 00:26:16.521145701
99   1970-01-01 00:26:16.521147000
Name: date, Length: 100, dtype: datetime64[ns]

Which is of course wrong, any idea why? thanks!

Here is an example of the input: 1575262698000.0 and the output should be the following: 2019-12-02 04:58:18

like image 475
Viktor.w Avatar asked Aug 31 '25 10:08

Viktor.w


1 Answers

Your error comes from pandas timestamp limitations: It looks like your input column is not in seconds but in milliseconds (ms). Taking only one value for simplification purposes:

>>> pd.to_datetime(1575262698000, unit='ms')
Timestamp('2019-12-02 04:58:18')

If you want to use seconds as unit, you can do

>>> pd.to_datetime(1575262698000 / 1000, unit='s')
Timestamp('2019-12-02 04:58:18') 

Removing unit arg yields datetimes around 1970 because default value for this parameter is ns ie nanoseconds. And 1575262698000 nanoseconds are around 1575 seconds or said differently around 25 minutes after 01/01/1970 !

like image 81
Théo Rubenach Avatar answered Sep 02 '25 23:09

Théo Rubenach