37
37
_mixed_frame = _frame .copy ()
38
38
39
39
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
+
40
48
class TestPandasContainer :
41
49
@pytest .fixture (scope = "function" , autouse = True )
42
50
def setup (self , datapath ):
@@ -90,25 +98,15 @@ def test_frame_double_encoded_labels(self, orient):
90
98
result = read_json (df .to_json (orient = orient ), orient = orient )
91
99
expected = df .copy ()
92
100
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 )
99
102
100
103
@pytest .mark .parametrize ("orient" , ["split" , "records" , "values" ])
101
104
def test_frame_non_unique_index (self , orient ):
102
105
df = DataFrame ([["a" , "b" ], ["c" , "d" ]], index = [1 , 1 ], columns = ["x" , "y" ])
103
106
result = read_json (df .to_json (orient = orient ), orient = orient )
104
107
expected = df .copy ()
105
108
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 )
112
110
113
111
@pytest .mark .parametrize ("orient" , ["index" , "columns" ])
114
112
def test_frame_non_unique_index_raises (self , orient ):
@@ -172,12 +170,7 @@ def test_roundtrip_simple(self, orient, convert_axes, numpy, dtype):
172
170
# TODO: debug why sort is required
173
171
expected = expected .sort_index ()
174
172
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 )
181
174
182
175
@pytest .mark .parametrize ("dtype" , [False , np .int64 ])
183
176
@pytest .mark .parametrize ("convert_axes" , [True , False ])
@@ -191,11 +184,6 @@ def test_roundtrip_intframe(self, orient, convert_axes, numpy, dtype):
191
184
if not numpy and (orient == "index" or (PY35 and orient == "columns" )):
192
185
expected = expected .sort_index ()
193
186
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
-
199
187
if (
200
188
numpy
201
189
and (is_platform_32bit () or is_platform_windows ())
@@ -205,7 +193,7 @@ def test_roundtrip_intframe(self, orient, convert_axes, numpy, dtype):
205
193
# TODO: see what is causing roundtrip dtype loss
206
194
expected = expected .astype (np .int32 )
207
195
208
- tm . assert_frame_equal (result , expected )
196
+ assert_json_roundtrip_equal (result , expected , orient )
209
197
210
198
@pytest .mark .parametrize ("dtype" , [None , np .float64 , np .int , "U3" ])
211
199
@pytest .mark .parametrize ("convert_axes" , [True , False ])
@@ -246,12 +234,7 @@ def test_roundtrip_str_axes(self, orient, convert_axes, numpy, dtype):
246
234
elif orient == "records" and convert_axes :
247
235
expected .columns = expected .columns .astype (np .int64 )
248
236
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 )
255
238
256
239
@pytest .mark .parametrize ("convert_axes" , [True , False ])
257
240
@pytest .mark .parametrize ("numpy" , [True , False ])
@@ -277,12 +260,7 @@ def test_roundtrip_categorical(self, orient, convert_axes, numpy):
277
260
if not numpy and (orient == "index" or (PY35 and orient == "columns" )):
278
261
expected = expected .sort_index ()
279
262
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 )
286
264
287
265
@pytest .mark .parametrize ("convert_axes" , [True , False ])
288
266
@pytest .mark .parametrize ("numpy" , [True , False ])
@@ -320,12 +298,7 @@ def test_roundtrip_timestamp(self, orient, convert_axes, numpy):
320
298
321
299
expected .index = idx
322
300
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 )
329
302
330
303
@pytest .mark .parametrize ("convert_axes" , [True , False ])
331
304
@pytest .mark .parametrize ("numpy" , [True , False ])
@@ -354,12 +327,7 @@ def test_roundtrip_mixed(self, orient, convert_axes, numpy):
354
327
if not numpy and (orient == "index" or (PY35 and orient == "columns" )):
355
328
expected = expected .sort_index ()
356
329
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 )
363
331
364
332
@pytest .mark .parametrize (
365
333
"data,msg,orient" ,
0 commit comments