Skip to content
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

to_json converts to UTC when encoding ISO formatted datetimes #12997

Open
Komnomnomnom opened this issue Apr 26, 2016 · 11 comments
Open

to_json converts to UTC when encoding ISO formatted datetimes #12997

Komnomnomnom opened this issue Apr 26, 2016 · 11 comments
Labels
Bug IO JSON read_json, to_json, json_normalize Timezones Timezone data dtype

Comments

@Komnomnomnom
Copy link
Contributor

Komnomnomnom commented Apr 26, 2016

Code Sample


In [99]: ts = Timestamp('2013-01-10 00:00:00-0500', tz='US/Eastern')

In [100]: pd.io.json.dumps(ts, iso_dates=True)
Out[100]: '"2013-01-10T05:00:00.000Z"'

Expected Output

In [100]: pd.io.json.dumps(ts, iso_dates=True)
Out[100]: '"2013-01-10T00:00:00.000-05:00"'

cf

In [109]: ts.isoformat()
Out[109]: '2013-01-10T00:00:00-05:00'
@jreback
Copy link
Contributor

jreback commented Apr 26, 2016

@Komnomnomnom is this an option that you want to add? (e.g. the iso_dates kw)?

@jreback jreback added IO JSON read_json, to_json, json_normalize Timezones Timezone data dtype Bug Difficulty Intermediate labels Apr 26, 2016
@jreback jreback added this to the 0.18.2 milestone Apr 26, 2016
@Komnomnomnom
Copy link
Contributor Author

iso_dates is an existing option on pd.json.dumps. I didn't use pd.to_json as it only takes Series or DataFrame but the date behaviour is the same:

In [5]: df = pd.DataFrame({'A':[ts]})
In [6]: df.to_json(date_format='iso')
Out[6]: '{"A":{"0":"2013-01-10T05:00:00.000Z"}}'

@jreback
Copy link
Contributor

jreback commented Apr 26, 2016

ahh ok, so its a bug. ok then!

@Komnomnomnom
Copy link
Contributor Author

Sorry I should have been more clear in the description :)

@jreback
Copy link
Contributor

jreback commented Apr 26, 2016

@Komnomnomnom np!

no pressure on fixing this one for 0.18.1. Can be in the next version.

@jorisvandenbossche jorisvandenbossche modified the milestones: Next Major Release, 0.19.0 Aug 18, 2016
@sdementen
Copy link
Contributor

any milestone for this feature ? currently, need to convert datetime to str before exporting to json and feed plotly charts...

@jorisvandenbossche
Copy link
Member

No specific milestone, but contributions are welcome!

@jeliashi
Copy link

Any updates?

@jreback
Copy link
Contributor

jreback commented May 14, 2021

@jeliashi you or others from the community are welcome to submit a patch

@jeliashi
Copy link

@jeliashi you or others from the community are welcome to submit a patch

seems like just updating the files that are copied from numpy (pandas/_libs/tslib/src/datetime/* should fix this. such as this one: https://github.com/numpy/numpy/blob/main/numpy/core/src/multiarray/datetime_strings.c

If I have time, I can tackle, but maybe someone else wants to do so before me

@jeliashi
Copy link

So I made a PR to fix this #41573 that tries to mimic what the numpy library does. I've been thinking about this a lot lately and was wondering if it would make more sense to move away from the numpy library way to do it.

They use the C interface to call the pytz objects directly which is what I am doing in this PR. But I don't have to do it their way. They use their pytz objects to calculate new datetime objects in utc time which they then use to calculate differences in minuted between UTC and their local time.

However, I was thinking instead we could use the datetime object directly to create the strings within the C interface.
This would be a little more involved for arrays, but still easily doable. What are people's thoughts?

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
7 participants