1515from pandas import DataFrame , DatetimeIndex , Series , Timestamp , read_json
1616import pandas ._testing as tm
1717
18+ _seriesd = tm .getSeriesData ()
19+
20+ _frame = DataFrame (_seriesd )
21+
22+ _cat_frame = _frame .copy ()
23+ cat = ["bah" ] * 5 + ["bar" ] * 5 + ["baz" ] * 5 + ["foo" ] * (len (_cat_frame ) - 15 )
24+ _cat_frame .index = pd .CategoricalIndex (cat , name = "E" )
25+ _cat_frame ["E" ] = list (reversed (cat ))
26+ _cat_frame ["sort" ] = np .arange (len (_cat_frame ), dtype = "int64" )
27+
1828
1929def assert_json_roundtrip_equal (result , expected , orient ):
2030 if orient == "records" or orient == "values" :
@@ -26,6 +36,12 @@ def assert_json_roundtrip_equal(result, expected, orient):
2636
2737@pytest .mark .filterwarnings ("ignore:the 'numpy' keyword is deprecated:FutureWarning" )
2838class TestPandasContainer :
39+ @pytest .fixture (autouse = True )
40+ def setup (self ):
41+ self .categorical = _cat_frame .copy ()
42+
43+ yield
44+
2945 def test_frame_double_encoded_labels (self , orient ):
3046 df = DataFrame (
3147 [["a" , "b" ], ["c" , "d" ]],
@@ -167,21 +183,25 @@ def test_roundtrip_str_axes(self, orient, convert_axes, numpy, dtype):
167183 @pytest .mark .parametrize ("convert_axes" , [True , False ])
168184 @pytest .mark .parametrize ("numpy" , [True , False ])
169185 def test_roundtrip_categorical (self , orient , convert_axes , numpy ):
170- cats = ["a" , "b" ]
171- df = pd .DataFrame (
172- pd .Categorical (cats ), index = pd .CategoricalIndex (cats ), columns = ["cat" ]
173- )
186+ # TODO: create a better frame to test with and improve coverage
187+ if orient in ("index" , "columns" ):
188+ pytest .xfail (f"Can't have duplicate index values for orient '{ orient } ')" )
174189
175- data = df .to_json (orient = orient )
176- if numpy and orient != "split" :
190+ data = self . categorical .to_json (orient = orient )
191+ if numpy and orient in ( "records" , "values" ) :
177192 pytest .xfail (f"Orient { orient } is broken with numpy=True" )
178193
179194 result = pd .read_json (
180195 data , orient = orient , convert_axes = convert_axes , numpy = numpy
181196 )
182197
183- # Categorical dtypes are not preserved on round trip
184- expected = pd .DataFrame (cats , index = cats , columns = ["cat" ])
198+ expected = self .categorical .copy ()
199+ expected .index = expected .index .astype (str ) # Categorical not preserved
200+ expected .index .name = None # index names aren't preserved in JSON
201+
202+ if not numpy and orient == "index" :
203+ expected = expected .sort_index ()
204+
185205 assert_json_roundtrip_equal (result , expected , orient )
186206
187207 @pytest .mark .parametrize ("convert_axes" , [True , False ])
0 commit comments