15
15
from pandas import DataFrame , DatetimeIndex , Series , Timestamp , read_json
16
16
import pandas ._testing as tm
17
17
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
-
28
18
29
19
def assert_json_roundtrip_equal (result , expected , orient ):
30
20
if orient == "records" or orient == "values" :
@@ -36,12 +26,6 @@ def assert_json_roundtrip_equal(result, expected, orient):
36
26
37
27
@pytest .mark .filterwarnings ("ignore:the 'numpy' keyword is deprecated:FutureWarning" )
38
28
class TestPandasContainer :
39
- @pytest .fixture (autouse = True )
40
- def setup (self ):
41
- self .categorical = _cat_frame .copy ()
42
-
43
- yield
44
-
45
29
def test_frame_double_encoded_labels (self , orient ):
46
30
df = DataFrame (
47
31
[["a" , "b" ], ["c" , "d" ]],
@@ -183,25 +167,21 @@ def test_roundtrip_str_axes(self, orient, convert_axes, numpy, dtype):
183
167
@pytest .mark .parametrize ("convert_axes" , [True , False ])
184
168
@pytest .mark .parametrize ("numpy" , [True , False ])
185
169
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
+ )
189
174
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" :
192
177
pytest .xfail (f"Orient { orient } is broken with numpy=True" )
193
178
194
179
result = pd .read_json (
195
180
data , orient = orient , convert_axes = convert_axes , numpy = numpy
196
181
)
197
182
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" ])
205
185
assert_json_roundtrip_equal (result , expected , orient )
206
186
207
187
@pytest .mark .parametrize ("convert_axes" , [True , False ])
0 commit comments