Skip to content

REF: Assert json roundtrip equal #28626

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 6 commits into from
Sep 27, 2019
Merged
Changes from all 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
64 changes: 16 additions & 48 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
_mixed_frame = _frame.copy()


def assert_json_roundtrip_equal(result, expected, orient):
if orient == "records" or orient == "values":
expected = expected.reset_index(drop=True)
if orient == "values":
expected.columns = range(len(expected.columns))
assert_frame_equal(result, expected)


class TestPandasContainer:
@pytest.fixture(scope="function", autouse=True)
def setup(self, datapath):
Expand Down Expand Up @@ -90,25 +98,15 @@ def test_frame_double_encoded_labels(self, orient):
result = read_json(df.to_json(orient=orient), orient=orient)
expected = df.copy()

if orient == "records" or orient == "values":
expected = expected.reset_index(drop=True)
if orient == "values":
expected.columns = range(len(expected.columns))

assert_frame_equal(result, expected)
assert_json_roundtrip_equal(result, expected, orient)

@pytest.mark.parametrize("orient", ["split", "records", "values"])
def test_frame_non_unique_index(self, orient):
df = DataFrame([["a", "b"], ["c", "d"]], index=[1, 1], columns=["x", "y"])
result = read_json(df.to_json(orient=orient), orient=orient)
expected = df.copy()

if orient == "records" or orient == "values":
expected = expected.reset_index(drop=True)
if orient == "values":
expected.columns = range(len(expected.columns))

assert_frame_equal(result, expected)
assert_json_roundtrip_equal(result, expected, orient)

@pytest.mark.parametrize("orient", ["index", "columns"])
def test_frame_non_unique_index_raises(self, orient):
Expand Down Expand Up @@ -172,12 +170,7 @@ def test_roundtrip_simple(self, orient, convert_axes, numpy, dtype):
# TODO: debug why sort is required
expected = expected.sort_index()

if orient == "records" or orient == "values":
expected = expected.reset_index(drop=True)
if orient == "values":
expected.columns = range(len(expected.columns))

tm.assert_frame_equal(result, expected)
assert_json_roundtrip_equal(result, expected, orient)

@pytest.mark.parametrize("dtype", [False, np.int64])
@pytest.mark.parametrize("convert_axes", [True, False])
Expand All @@ -191,11 +184,6 @@ def test_roundtrip_intframe(self, orient, convert_axes, numpy, dtype):
if not numpy and (orient == "index" or (PY35 and orient == "columns")):
expected = expected.sort_index()

if orient == "records" or orient == "values":
expected = expected.reset_index(drop=True)
if orient == "values":
expected.columns = range(len(expected.columns))

if (
numpy
and (is_platform_32bit() or is_platform_windows())
Expand All @@ -205,7 +193,7 @@ def test_roundtrip_intframe(self, orient, convert_axes, numpy, dtype):
# TODO: see what is causing roundtrip dtype loss
expected = expected.astype(np.int32)

tm.assert_frame_equal(result, expected)
assert_json_roundtrip_equal(result, expected, orient)

@pytest.mark.parametrize("dtype", [None, np.float64, np.int, "U3"])
@pytest.mark.parametrize("convert_axes", [True, False])
Expand Down Expand Up @@ -246,12 +234,7 @@ def test_roundtrip_str_axes(self, orient, convert_axes, numpy, dtype):
elif orient == "records" and convert_axes:
expected.columns = expected.columns.astype(np.int64)

if orient == "records" or orient == "values":
expected = expected.reset_index(drop=True)
if orient == "values":
expected.columns = range(len(expected.columns))

tm.assert_frame_equal(result, expected)
assert_json_roundtrip_equal(result, expected, orient)

@pytest.mark.parametrize("convert_axes", [True, False])
@pytest.mark.parametrize("numpy", [True, False])
Expand All @@ -277,12 +260,7 @@ def test_roundtrip_categorical(self, orient, convert_axes, numpy):
if not numpy and (orient == "index" or (PY35 and orient == "columns")):
expected = expected.sort_index()

if orient == "records" or orient == "values":
expected = expected.reset_index(drop=True)
if orient == "values":
expected.columns = range(len(expected.columns))

tm.assert_frame_equal(result, expected)
assert_json_roundtrip_equal(result, expected, orient)

@pytest.mark.parametrize("convert_axes", [True, False])
@pytest.mark.parametrize("numpy", [True, False])
Expand Down Expand Up @@ -320,12 +298,7 @@ def test_roundtrip_timestamp(self, orient, convert_axes, numpy):

expected.index = idx

if orient == "records" or orient == "values":
expected = expected.reset_index(drop=True)
if orient == "values":
expected.columns = range(len(expected.columns))

tm.assert_frame_equal(result, expected)
assert_json_roundtrip_equal(result, expected, orient)

@pytest.mark.parametrize("convert_axes", [True, False])
@pytest.mark.parametrize("numpy", [True, False])
Expand Down Expand Up @@ -354,12 +327,7 @@ def test_roundtrip_mixed(self, orient, convert_axes, numpy):
if not numpy and (orient == "index" or (PY35 and orient == "columns")):
expected = expected.sort_index()

if orient == "records" or orient == "values":
expected = expected.reset_index(drop=True)
if orient == "values":
expected.columns = range(len(expected.columns))

tm.assert_frame_equal(result, expected)
assert_json_roundtrip_equal(result, expected, orient)

@pytest.mark.parametrize(
"data,msg,orient",
Expand Down