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