4
4
import os
5
5
6
6
import numpy as np
7
- from pandas import Series , DataFrame , DatetimeIndex , Timestamp
7
+ from pandas import Series , DataFrame , DatetimeIndex , Timestamp , CategoricalIndex
8
8
from datetime import timedelta
9
9
import pandas as pd
10
10
read_json = pd .read_json
23
23
for k , v in compat .iteritems (_seriesd )))
24
24
25
25
_tsframe = DataFrame (_tsd )
26
+ _cat_frame = _frame .copy ()
27
+ cat = ['bah' ]* 5 + ['bar' ]* 5 + ['baz' ]* 5 + ['foo' ]* (len (_cat_frame )- 15 )
28
+ _cat_frame .index = pd .CategoricalIndex (cat ,name = 'E' )
29
+ _cat_frame ['E' ] = list (reversed (cat ))
30
+ _cat_frame ['sort' ] = np .arange (len (_cat_frame ))
26
31
27
32
_mixed_frame = _frame .copy ()
28
33
@@ -48,6 +53,7 @@ def setUp(self):
48
53
self .intframe = _intframe .copy ()
49
54
self .tsframe = _tsframe .copy ()
50
55
self .mixed_frame = _mixed_frame .copy ()
56
+ self .categorical = _cat_frame .copy ()
51
57
52
58
def tearDown (self ):
53
59
del self .dirpath
@@ -128,8 +134,22 @@ def _check(df):
128
134
129
135
def test_frame_from_json_to_json (self ):
130
136
def _check_orient (df , orient , dtype = None , numpy = False ,
131
- convert_axes = True , check_dtype = True , raise_ok = None ):
132
- df = df .sort ()
137
+ convert_axes = True , check_dtype = True , raise_ok = None ,
138
+ sort = None ):
139
+ if sort is not None :
140
+ df = df .sort (sort )
141
+ else :
142
+ df = df .sort ()
143
+
144
+ # if we are not unique, then check that we are raising ValueError
145
+ # for the appropriate orients
146
+ if not df .index .is_unique and orient in ['index' ,'columns' ]:
147
+ self .assertRaises (ValueError , lambda : df .to_json (orient = orient ))
148
+ return
149
+ if not df .columns .is_unique and orient in ['index' ,'columns' ,'records' ]:
150
+ self .assertRaises (ValueError , lambda : df .to_json (orient = orient ))
151
+ return
152
+
133
153
dfjson = df .to_json (orient = orient )
134
154
135
155
try :
@@ -141,7 +161,10 @@ def _check_orient(df, orient, dtype=None, numpy=False,
141
161
return
142
162
raise
143
163
144
- unser = unser .sort ()
164
+ if sort is not None and sort in unser .columns :
165
+ unser = unser .sort (sort )
166
+ else :
167
+ unser = unser .sort ()
145
168
146
169
if dtype is False :
147
170
check_dtype = False
@@ -160,7 +183,9 @@ def _check_orient(df, orient, dtype=None, numpy=False,
160
183
# index and col labels might not be strings
161
184
unser .index = [str (i ) for i in unser .index ]
162
185
unser .columns = [str (i ) for i in unser .columns ]
163
- unser = unser .sort ()
186
+
187
+ if sort is None :
188
+ unser = unser .sort ()
164
189
assert_almost_equal (df .values , unser .values )
165
190
else :
166
191
if convert_axes :
@@ -169,45 +194,45 @@ def _check_orient(df, orient, dtype=None, numpy=False,
169
194
assert_frame_equal (df , unser , check_less_precise = False ,
170
195
check_dtype = check_dtype )
171
196
172
- def _check_all_orients (df , dtype = None , convert_axes = True , raise_ok = None ):
197
+ def _check_all_orients (df , dtype = None , convert_axes = True , raise_ok = None , sort = None ):
173
198
174
199
# numpy=False
175
200
if convert_axes :
176
- _check_orient (df , "columns" , dtype = dtype )
177
- _check_orient (df , "records" , dtype = dtype )
178
- _check_orient (df , "split" , dtype = dtype )
179
- _check_orient (df , "index" , dtype = dtype )
180
- _check_orient (df , "values" , dtype = dtype )
181
-
182
- _check_orient (df , "columns" , dtype = dtype , convert_axes = False )
183
- _check_orient (df , "records" , dtype = dtype , convert_axes = False )
184
- _check_orient (df , "split" , dtype = dtype , convert_axes = False )
185
- _check_orient (df , "index" , dtype = dtype , convert_axes = False )
186
- _check_orient (df , "values" , dtype = dtype ,convert_axes = False )
201
+ _check_orient (df , "columns" , dtype = dtype , sort = sort )
202
+ _check_orient (df , "records" , dtype = dtype , sort = sort )
203
+ _check_orient (df , "split" , dtype = dtype , sort = sort )
204
+ _check_orient (df , "index" , dtype = dtype , sort = sort )
205
+ _check_orient (df , "values" , dtype = dtype , sort = sort )
206
+
207
+ _check_orient (df , "columns" , dtype = dtype , convert_axes = False , sort = sort )
208
+ _check_orient (df , "records" , dtype = dtype , convert_axes = False , sort = sort )
209
+ _check_orient (df , "split" , dtype = dtype , convert_axes = False , sort = sort )
210
+ _check_orient (df , "index" , dtype = dtype , convert_axes = False , sort = sort )
211
+ _check_orient (df , "values" , dtype = dtype ,convert_axes = False , sort = sort )
187
212
188
213
# numpy=True and raise_ok might be not None, so ignore the error
189
214
if convert_axes :
190
215
_check_orient (df , "columns" , dtype = dtype , numpy = True ,
191
- raise_ok = raise_ok )
216
+ raise_ok = raise_ok , sort = sort )
192
217
_check_orient (df , "records" , dtype = dtype , numpy = True ,
193
- raise_ok = raise_ok )
218
+ raise_ok = raise_ok , sort = sort )
194
219
_check_orient (df , "split" , dtype = dtype , numpy = True ,
195
- raise_ok = raise_ok )
220
+ raise_ok = raise_ok , sort = sort )
196
221
_check_orient (df , "index" , dtype = dtype , numpy = True ,
197
- raise_ok = raise_ok )
222
+ raise_ok = raise_ok , sort = sort )
198
223
_check_orient (df , "values" , dtype = dtype , numpy = True ,
199
- raise_ok = raise_ok )
224
+ raise_ok = raise_ok , sort = sort )
200
225
201
226
_check_orient (df , "columns" , dtype = dtype , numpy = True ,
202
- convert_axes = False , raise_ok = raise_ok )
227
+ convert_axes = False , raise_ok = raise_ok , sort = sort )
203
228
_check_orient (df , "records" , dtype = dtype , numpy = True ,
204
- convert_axes = False , raise_ok = raise_ok )
229
+ convert_axes = False , raise_ok = raise_ok , sort = sort )
205
230
_check_orient (df , "split" , dtype = dtype , numpy = True ,
206
- convert_axes = False , raise_ok = raise_ok )
231
+ convert_axes = False , raise_ok = raise_ok , sort = sort )
207
232
_check_orient (df , "index" , dtype = dtype , numpy = True ,
208
- convert_axes = False , raise_ok = raise_ok )
233
+ convert_axes = False , raise_ok = raise_ok , sort = sort )
209
234
_check_orient (df , "values" , dtype = dtype , numpy = True ,
210
- convert_axes = False , raise_ok = raise_ok )
235
+ convert_axes = False , raise_ok = raise_ok , sort = sort )
211
236
212
237
# basic
213
238
_check_all_orients (self .frame )
@@ -233,6 +258,9 @@ def _check_all_orients(df, dtype=None, convert_axes=True, raise_ok=None):
233
258
_check_all_orients (DataFrame (biggie , dtype = 'U3' ), dtype = 'U3' ,
234
259
convert_axes = False , raise_ok = ValueError )
235
260
261
+ # categorical
262
+ _check_all_orients (self .categorical , sort = 'sort' , raise_ok = ValueError )
263
+
236
264
# empty
237
265
_check_all_orients (self .empty_frame )
238
266
0 commit comments