Skip to content

BUG: Fix empty Data frames to JSON round-trippable back to data frames #21318

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

Merged
merged 15 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.23.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ I/O

- Bug in IO methods specifying ``compression='zip'`` which produced uncompressed zip archives (:issue:`17778`, :issue:`21144`)
- Bug in :meth:`DataFrame.to_stata` which prevented exporting DataFrames to buffers and most file-like objects (:issue:`21041`)
- Bug in IO JSON methods reading empty JSON schema back to DataFrame caused an error (:issue:`21287`)
- Bug in IO JSON method :func:`read_json` reading empty JSON schema back to :class:DataFrame caused an error (:issue:`21287`)

Plotting
^^^^^^^^
Expand Down
18 changes: 8 additions & 10 deletions pandas/tests/io/json/test_json_table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,6 @@ def test_multiindex(self):
assert result == expected


class TestParseSchema(object):

def test_empty_json_data(self):
# GH21287
df = pd.DataFrame([], columns=['a', 'b', 'c'])
json = df.to_json(None, orient='table')
df = parse_table_schema(json, True)
assert df.empty


class TestTableSchemaType(object):

@pytest.mark.parametrize('int_type', [
Expand Down Expand Up @@ -571,3 +561,11 @@ def test_multiindex(self, index_names):
out = df.to_json(orient="table")
result = pd.read_json(out, orient="table")
tm.assert_frame_equal(df, result)

def test_empty_frame_roundtrip(self):
# GH 21287
df = pd.DataFrame([], columns=['a', 'b', 'c'])
expected = df.copy()
out = df.to_json(None, orient='table')
result = pd.read_json(out, orient='table')
tm.assert_frame_equal(expected, result)
Copy link
Contributor Author

@pyryjook pyryjook Jun 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This raises an assertion error:

E       AssertionError: DataFrame.index are different
E
E       DataFrame.index classes are not equivalent
E       [left]:  Index([], dtype='object')
E       [right]: Float64Index([], dtype='float64')

That's something I need to dig deeper. If there is something obvious, that I'm missing, any pointers would be appreciated in such case.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this PR! Beat me to it :)
A bit weak but what do we think of just
pd.testing.assert_frame_equal(expected, actual, check_dtype=False) ?

Otherwise I would guess we have to go down the road of including the dtypes in the JSON representation?

Copy link
Contributor Author

@pyryjook pyryjook Jun 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! And actually it only works if I assert it like this:
pd.testing.assert_frame_equal(expected, result, check_dtype=False, check_index_type=False)
So both check_dtypeand check_index_type have to be set to False in order to get the assertion right.

Thoughts on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignoring the dtype difference is not the solution. The point of this format is to persist that metadata.

What I would do is check that the proper type information for the index is being written out (you can use a io.StringIO instance instead of writing to None). If that appears correct then there would be an issue with the reader that is ignoring or casting the type of the index after the fact

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're right, ignoring the dtype and the index_type will just hide the problem.

Did some initial testings and it seems that on the reading side empty data with data.dtype == 'object' gets coerced to Float64 without any clear reason.

I'll push a commit with fix proposal for comments.