Skip to content

pandas.Series.to_json() incorrectly localizes tz-naive datetimes to UTC #38760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
simplydac opened this issue Dec 28, 2020 · 3 comments · Fixed by #46730
Closed

pandas.Series.to_json() incorrectly localizes tz-naive datetimes to UTC #38760

simplydac opened this issue Dec 28, 2020 · 3 comments · Fixed by #46730
Assignees
Labels
Bug IO JSON read_json, to_json, json_normalize Timezones Timezone data dtype
Milestone

Comments

@simplydac
Copy link

simplydac commented Dec 28, 2020

Seemingly related but not identical to #12997

How to reproduce the bug

Checked with pandas 0.24.2, 1.1.5 and 1.2.0.

import pandas as pd

index = pd.date_range(
    start='2020-12-28 00:00:00',
    end='2020-12-28 02:00:00',
    freq='1H',
)
a = pd.Series(
    data=range(3),
    index=index,
)
a.to_json(date_format='iso')

Output

'{"2020-12-28T00:00:00.000Z":0,"2020-12-28T01:00:00.000Z":1,"2020-12-28T02:00:00.000Z":2}'

(Notice the "Z" indicating UTC)

Expected output

'{"2020-12-28T00:00:00.000":0,"2020-12-28T01:00:00.000":1,"2020-12-28T02:00:00.000":2}'

(without "Z")

@simplydac simplydac changed the title pandas.Series.to_json() unexpectedly localizes tz-naive datetimes to UTC pandas.Series.to_json() incorrectly localizes tz-naive datetimes to UTC Dec 28, 2020
@simplydac
Copy link
Author

Solving #12997 should close this issue as well.

@jreback
Copy link
Contributor

jreback commented Dec 28, 2020

Seemingly related but not identical to #12997

how so?

@simplydac
Copy link
Author

simplydac commented Dec 28, 2020

how so?

Related:

Not identical:

import pandas as pd

pd.Timestamp('2020-01-01 00:00:00+00:00') == pd.Timestamp('2020-01-01 05:00:00+05:00')  # True
pd.Timestamp('2020-01-01 00:00:00.000Z') == pd.Timestamp('2020-01-01 00:00:00')  # False

Besides, the 2nd comparison above should throw an exception (does it make sens to compare tz-aware with tz-naive?!), but that is another subject.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO JSON read_json, to_json, json_normalize Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants