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 3 commits
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`)
Copy link
Member

Choose a reason for hiding this comment

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

Explicitly reference :func:`read_json` and make sure to qualify that this only applies when ``orient='table'``

Copy link
Member

Choose a reason for hiding this comment

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

Can also update reference to :class:`DataFrame`


Plotting
^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/json/table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def parse_table_schema(json, precise_float):
"""
table = loads(json, precise_float=precise_float)
col_order = [field['name'] for field in table['schema']['fields']]
df = DataFrame(table['data'])[col_order]
df = DataFrame(table['data'], columns=col_order)[col_order]

dtypes = {field['name']: convert_json_field_to_pandas_type(field)
for field in table['schema']['fields']}
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/io/json/test_json_table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pandas.io.json.table_schema import (
as_json_table_type,
build_table_schema,
parse_table_schema,
convert_pandas_type_to_json_field,
convert_json_field_to_pandas_type,
set_default_names)
Expand Down Expand Up @@ -86,6 +87,16 @@ def test_multiindex(self):
assert result == expected


class TestParseSchema(object):
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't need a new class here - can you just move this to TestTableOrientReader?


def test_empty_json_data(self):
Copy link
Member

Choose a reason for hiding this comment

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

Rename this to test_empty_frame_roundtrip

# GH21287
df = pd.DataFrame([], columns=['a', 'b', 'c'])
json = df.to_json(None, orient='table')
df = parse_table_schema(json, True)
Copy link
Member

Choose a reason for hiding this comment

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

Let's use pd.read_json here instead

assert df.empty
Copy link
Member

Choose a reason for hiding this comment

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

To make sure that we've preserved the frame metadata we should use the tm.assert_frame_equal function here. Typically with tests we will:

  1. Create a variable called expected, which is very explicit about what we want (here that's just a copy of df)
  2. Assign to a variable called result, which here would be the result of pd.read_json
  3. Make sure result and expected match using tm.assert_frame_equal



class TestTableSchemaType(object):

@pytest.mark.parametrize('int_type', [
Expand Down