0

I am able to get t1 conversion pd.to_datetime output in seconds(deafult to ns precision)

> t1=1508687283.891365

> pd.to_datetime(t1,unit='s')

Output: Timestamp('2017-10-22 15:48:03.891365051')

But i am unable to get in microsecond/millisecond precision for t1, it gets defaults to unix start time 1970-01-01

> pd.to_datetime(t1,unit='us')

Output: Timestamp('1970-01-01 00:25:08.687283891')

> pd.to_datetime(t,unit='ms')

Output: Timestamp('1970-01-18 11:04:47.283891357')
  • 1
    it doesn't *default to 1970*, it assumes you're giving it ms/µs since the epoch – FObersteiner Jul 29 '20 at 06:39
  • 1
    Note: changing units will not improve precision. You started with floating points, which have limited precision on number of decimals. Changing the units will just change the exponent (so nothing about precision). If you do not care about very old years, just change the epoch (so you get more precision). Maybe doing in few steps: remove decimal part, convert into second (and integer), and then multiply and add decimal part. In this case (if you choose large integer) you may get your precision. With 64-bit (and floating point) you cannot store much precision. – Giacomo Catenazzi Jul 29 '20 at 06:44
  • https://www.epochconverter.com/ converts well t1=1508687283.891365 in to Sunday, October 22, 2017 3:48:03.891 PM ms precision, any pandas command example for above converting similarly? – Praneeth varma Jul 29 '20 at 06:52
  • 1
    That is exactly what `pd.to_datetime(t1,unit='s')` gives you. It is unclear to me what your question is? Is this about *formatting* the output? -> if so, have a look at https://strftime.org/ – FObersteiner Jul 29 '20 at 06:55
  • @MrFuppes i was expecting output of pd.to_datetime(t1,unit='ms') as Timestamp('2017-10-22 15:48:03.891') but it doesn't give in this format – Praneeth varma Jul 29 '20 at 07:12
  • 1
    Double precision floating point number can store at maximum 15 digits, your t1 has 16 digits, so you have lost precision already in `t1`. – Giacomo Catenazzi Jul 29 '20 at 07:12
  • are you looking for https://stackoverflow.com/questions/7588511/format-a-datetime-into-a-string-with-milliseconds ? – FObersteiner Jul 29 '20 at 07:17

0 Answers0