0

whats the approach to converting a unix time stamp which is in microseconds to datetime?

for example: 1632527998056329

pd.to_datetime(data['local_timestamp'],unit='ms')

doesn't seem to work. I have a column with unix timestamp with microsecond granularity which I need to convert and set as index.

The above code returns an error: pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: cannot convert input with unit 'ms'

The above code works when the unix time stamp is for milliseconds. I don't think there is one for microseconds.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
user1234440
  • 22,521
  • 18
  • 61
  • 103

1 Answers1

1

In Pandas, it's actually named us. Not ms since that represents milliseconds.

So microseconds also starts with m, but it's named us:

>>> pd.to_datetime(1632527998056329, unit='us')
Timestamp('2021-09-24 23:59:58.056329')
>>> 

So in your case do:

data['local_timestamp'] = pd.to_datetime(data['local_timestamp'], unit='us')
U13-Forward
  • 69,221
  • 14
  • 89
  • 114