From 94e528af1b67af8a3469e6ed50e4c8a955c48f79 Mon Sep 17 00:00:00 2001 From: bganglia <48276939+bganglia@users.noreply.github.com> Date: Mon, 23 Sep 2019 14:52:56 -0500 Subject: [PATCH 1/6] Add assert_json_roundtrip_equal function Add assert_json_roundtrip_equal function to make repeated pattern modular --- pandas/tests/io/json/test_pandas.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 415b1d81eb3e4..1405342ddfb60 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -36,6 +36,12 @@ _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) From 7cb5f8571c13565d89eab8b67456bab4c5f6626b Mon Sep 17 00:00:00 2001 From: bganglia <48276939+bganglia@users.noreply.github.com> Date: Mon, 23 Sep 2019 15:13:48 -0500 Subject: [PATCH 2/6] assert_json_roundtrip_equal calls Replaced repeated code with calls to assert_json_roundtrip_equal --- pandas/tests/io/json/test_pandas.py | 51 +++++------------------------ 1 file changed, 8 insertions(+), 43 deletions(-) diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 1405342ddfb60..516ff4f9a3099 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -96,25 +96,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): @@ -178,12 +168,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]) @@ -252,12 +237,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]) @@ -283,12 +263,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]) @@ -326,12 +301,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]) @@ -360,12 +330,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", From 318669d134b1e8dad69ec0e0299d41310ab8d6df Mon Sep 17 00:00:00 2001 From: bganglia <48276939+bganglia@users.noreply.github.com> Date: Thu, 26 Sep 2019 18:40:31 -0500 Subject: [PATCH 3/6] Remove trailing whitespace Remove trailing whitespace --- pandas/tests/io/json/test_pandas.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 516ff4f9a3099..d28d1d19d7c58 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -36,6 +36,7 @@ _mixed_frame = _frame.copy() + def assert_json_roundtrip_equal(result, expected, orient): if orient == "records" or orient == "values": expected = expected.reset_index(drop=True) @@ -97,7 +98,7 @@ def test_frame_double_encoded_labels(self, orient): expected = df.copy() 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"]) From e3244b82ab7900ee677c216a7ffb7125818afa27 Mon Sep 17 00:00:00 2001 From: bganglia <48276939+bganglia@users.noreply.github.com> Date: Thu, 26 Sep 2019 19:18:16 -0500 Subject: [PATCH 4/6] Remove trailing whitespace Remove trailing whitespace --- pandas/tests/io/json/test_pandas.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index d28d1d19d7c58..34b5fb591ad10 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -44,6 +44,7 @@ def assert_json_roundtrip_equal(result, expected, orient): expected.columns = range(len(expected.columns)) assert_frame_equal(result, expected) + class TestPandasContainer: @pytest.fixture(scope="function", autouse=True) def setup(self, datapath): From 23d2b1cc9c2a67fa249126184860b81ecce172f7 Mon Sep 17 00:00:00 2001 From: bganglia <48276939+bganglia@users.noreply.github.com> Date: Fri, 27 Sep 2019 15:52:16 -0500 Subject: [PATCH 5/6] Update test_pandas.py --- pandas/tests/io/json/test_pandas.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 34b5fb591ad10..3563fac34bc60 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -44,7 +44,6 @@ def assert_json_roundtrip_equal(result, expected, orient): expected.columns = range(len(expected.columns)) assert_frame_equal(result, expected) - class TestPandasContainer: @pytest.fixture(scope="function", autouse=True) def setup(self, datapath): @@ -184,11 +183,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()) @@ -198,7 +192,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]) From 10a69ad70d87451b77cd37b0c3b9e57b08952b5a Mon Sep 17 00:00:00 2001 From: bganglia <48276939+bganglia@users.noreply.github.com> Date: Fri, 27 Sep 2019 16:29:01 -0500 Subject: [PATCH 6/6] Remove whitespace Remove whitespace --- pandas/tests/io/json/test_pandas.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 3563fac34bc60..2195bf248f43a 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -44,6 +44,7 @@ def assert_json_roundtrip_equal(result, expected, orient): expected.columns = range(len(expected.columns)) assert_frame_equal(result, expected) + class TestPandasContainer: @pytest.fixture(scope="function", autouse=True) def setup(self, datapath):