Skip to content

Commit 49b4b0a

Browse files
bgangliaWillAyd
authored andcommitted
REF: Assert json roundtrip equal (#28626)
1 parent 90d7eee commit 49b4b0a

File tree

1 file changed

+16
-48
lines changed

1 file changed

+16
-48
lines changed

pandas/tests/io/json/test_pandas.py

+16-48
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
_mixed_frame = _frame.copy()
3838

3939

40+
def assert_json_roundtrip_equal(result, expected, orient):
41+
if orient == "records" or orient == "values":
42+
expected = expected.reset_index(drop=True)
43+
if orient == "values":
44+
expected.columns = range(len(expected.columns))
45+
assert_frame_equal(result, expected)
46+
47+
4048
class TestPandasContainer:
4149
@pytest.fixture(scope="function", autouse=True)
4250
def setup(self, datapath):
@@ -90,25 +98,15 @@ def test_frame_double_encoded_labels(self, orient):
9098
result = read_json(df.to_json(orient=orient), orient=orient)
9199
expected = df.copy()
92100

93-
if orient == "records" or orient == "values":
94-
expected = expected.reset_index(drop=True)
95-
if orient == "values":
96-
expected.columns = range(len(expected.columns))
97-
98-
assert_frame_equal(result, expected)
101+
assert_json_roundtrip_equal(result, expected, orient)
99102

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

106-
if orient == "records" or orient == "values":
107-
expected = expected.reset_index(drop=True)
108-
if orient == "values":
109-
expected.columns = range(len(expected.columns))
110-
111-
assert_frame_equal(result, expected)
109+
assert_json_roundtrip_equal(result, expected, orient)
112110

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

175-
if orient == "records" or orient == "values":
176-
expected = expected.reset_index(drop=True)
177-
if orient == "values":
178-
expected.columns = range(len(expected.columns))
179-
180-
tm.assert_frame_equal(result, expected)
173+
assert_json_roundtrip_equal(result, expected, orient)
181174

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

194-
if orient == "records" or orient == "values":
195-
expected = expected.reset_index(drop=True)
196-
if orient == "values":
197-
expected.columns = range(len(expected.columns))
198-
199187
if (
200188
numpy
201189
and (is_platform_32bit() or is_platform_windows())
@@ -205,7 +193,7 @@ def test_roundtrip_intframe(self, orient, convert_axes, numpy, dtype):
205193
# TODO: see what is causing roundtrip dtype loss
206194
expected = expected.astype(np.int32)
207195

208-
tm.assert_frame_equal(result, expected)
196+
assert_json_roundtrip_equal(result, expected, orient)
209197

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

249-
if orient == "records" or orient == "values":
250-
expected = expected.reset_index(drop=True)
251-
if orient == "values":
252-
expected.columns = range(len(expected.columns))
253-
254-
tm.assert_frame_equal(result, expected)
237+
assert_json_roundtrip_equal(result, expected, orient)
255238

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

280-
if orient == "records" or orient == "values":
281-
expected = expected.reset_index(drop=True)
282-
if orient == "values":
283-
expected.columns = range(len(expected.columns))
284-
285-
tm.assert_frame_equal(result, expected)
263+
assert_json_roundtrip_equal(result, expected, orient)
286264

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

321299
expected.index = idx
322300

323-
if orient == "records" or orient == "values":
324-
expected = expected.reset_index(drop=True)
325-
if orient == "values":
326-
expected.columns = range(len(expected.columns))
327-
328-
tm.assert_frame_equal(result, expected)
301+
assert_json_roundtrip_equal(result, expected, orient)
329302

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

357-
if orient == "records" or orient == "values":
358-
expected = expected.reset_index(drop=True)
359-
if orient == "values":
360-
expected.columns = range(len(expected.columns))
361-
362-
tm.assert_frame_equal(result, expected)
330+
assert_json_roundtrip_equal(result, expected, orient)
363331

364332
@pytest.mark.parametrize(
365333
"data,msg,orient",

0 commit comments

Comments
 (0)