Xiang Yang
Xiang Yang

Reputation: 23

python datetime and pandas to_datetime have different output when covering same timestamp

When I try to convert timestamp, I used 2 method and their outcomes are different from each other

Method 1:

pd.to_datetime(1523808011.979,unit='s')  # I got Timestamp('2018-04-15 16:00:11.979000')

Method 2:

yyy=dt.datetime.fromtimestamp(1523808011.979).isoformat()  # I got '2018-04-16T00:00:11.979000'

Could you please help me to understand why?

Best regards,

YX

Upvotes: 2

Views: 149

Answers (2)

nutic
nutic

Reputation: 457

In repl which internal time is UTC the result is the same: https://repl.it/repls/IndolentBurlywoodAddons

On my local machine (UTC+2) two values differ exactly for two hours. So looks like the explanation is the timezone.

Upvotes: 0

Scratch'N'Purr
Scratch'N'Purr

Reputation: 10399

The fromtimestamp returns the local time so if you're in a timezone that isn't UTC, then you'll get a different result.

You should use utcfromtimestamp instead.

>>> from datetime import datetime
>>> datetime.utcfromtimestamp(1523808011.979).isoformat()
'2018-04-15T16:00:11.979000'

Upvotes: 1

Related Questions