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-
2818
2919def assert_json_roundtrip_equal (result , expected , orient ):
3020 if orient == "records" or orient == "values" :
@@ -36,12 +26,6 @@ def assert_json_roundtrip_equal(result, expected, orient):
3626
3727@pytest .mark .filterwarnings ("ignore:the 'numpy' keyword is deprecated:FutureWarning" )
3828class TestPandasContainer :
39- @pytest .fixture (autouse = True )
40- def setup (self ):
41- self .categorical = _cat_frame .copy ()
42-
43- yield
44-
4529 def test_frame_double_encoded_labels (self , orient ):
4630 df = DataFrame (
4731 [["a" , "b" ], ["c" , "d" ]],
@@ -183,25 +167,21 @@ def test_roundtrip_str_axes(self, orient, convert_axes, numpy, dtype):
183167 @pytest .mark .parametrize ("convert_axes" , [True , False ])
184168 @pytest .mark .parametrize ("numpy" , [True , False ])
185169 def test_roundtrip_categorical (self , orient , convert_axes , numpy ):
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 } ')" )
170+ cats = ["a" , "b" ]
171+ df = pd .DataFrame (
172+ pd .Categorical (cats ), index = pd .CategoricalIndex (cats ), columns = ["cat" ]
173+ )
189174
190- data = self . categorical .to_json (orient = orient )
191- if numpy and orient in ( "records" , "values" ) :
175+ data = df .to_json (orient = orient )
176+ if numpy and orient != "split" :
192177 pytest .xfail (f"Orient { orient } is broken with numpy=True" )
193178
194179 result = pd .read_json (
195180 data , orient = orient , convert_axes = convert_axes , numpy = numpy
196181 )
197182
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-
183+ # Categorical dtypes are not preserved on round trip
184+ expected = pd .DataFrame (cats , index = cats , columns = ["cat" ])
205185 assert_json_roundtrip_equal (result , expected , orient )
206186
207187 @pytest .mark .parametrize ("convert_axes" , [True , False ])
0 commit comments