8000 Column information lost in to_json() if data frame is empty · Issue #7445 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

Column information lost in to_json() if data frame is empty #7445

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
aizvorski opened this issue Jun 12, 2014 · 3 comments
Closed

Column information lost in to_json() if data frame is empty #7445

aizvorski opened this issue Jun 12, 2014 · 3 comments
Labels
Bug IO JSON read_json, to_json, json_normalize
Milestone

Comments

@aizvorski
Copy link

Create empty dataframe with a column and write it out with to_json(), then read it back in:

In [55]: s = pandas.DataFrame({'test': [] }, index=[] ).to_json(orient='columns')
In [56]: s
Out[56]: '{}'
In [57]: str( pandas.read_json(s, orient='columns') )
Out[57]: 'Empty DataFrame\nColumns: []\nIndex: []'

I think the expected string is '{"test":{}}'

This situation arises when code filters data frames and writes them out, while not being very careful to make sure the filtered frames contain at least one row.

@jreback jreback added the JSON label Jun 12, 2014
@jreback
Copy link
Contributor
jreback commented Jun 12, 2014

cc @Komnomnomnom

@jreback jreback added the Bug label Jun 12, 2014
@jreback jreback added this to the 0.15.0 milestone Jun 12, 2014
@Komnomnomnom
Copy link
Contributor

Thanks @aizvorski I can confirm this bug, and I agree about the expected output.

The JSON handling is producing its output by looping over the dataframe values array, which is why in this case the labels end up being ignored during serialisation.

@jreback I'm on vacation until mid July but I'll see if I can get this fixed for 0.15. There's also the other JSON issues / features I've been meaning to do since pre 0.14...I'll see if I can knock them out at the same time.

@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 6, 2015
@behzadnouri
Copy link
Contributor

closed by #9805

>>> pd.DataFrame({'test':[]}, index=[]).to_json(orient='columns')
'{"test":{}}'

@jorisvandenbossche jorisvandenbossche modified the milestones: 0.16.1, Next Major Release Dec 16, 2016
@jreback jreback closed this as completed Dec 16, 2016
mroeschke added a commit to mroeschke/pandas that referenced this issue Dec 16, 2016
jreback pushed a commit that referenced this issue Dec 16, 2016
closes #7445

Author: Matt Roeschke <emailformattr@gmail.com>

Closes #14893 from mroeschke/test_7445 and squashes the following commits:

740cafe [Matt Roeschke] TST: to_json keeps column info with empty dataframe (#7445)
ischurov pushed a commit to ischurov/pandas that referenced this issue Dec 19, 2016
closes pandas-dev#7445

Author: Matt Roeschke <emailformattr@gmail.com>

Closes pandas-dev#14893 from mroeschke/test_7445 and squashes the following commits:

740cafe [Matt Roeschke] TST: to_json keeps column info with empty dataframe (pandas-dev#7445)
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
0