21
21
from pandas .core .dtypes .cast import construct_1d_object_array_from_listlike
22
22
from pandas .tests .extension import base
23
23
import pytest
24
+ import db_dtypes
24
25
25
26
26
27
class TestJSONArray (base .ExtensionTests ):
@@ -111,7 +112,7 @@ def test_compare_scalar(self, data, comparison_op, request):
111
112
super ().test_compare_scalar (data , comparison_op )
112
113
113
114
def _supports_reduction (self , ser : pd .Series , op_name : str ) -> bool :
114
- return op_name in [ "min" , "max" ]
115
+ return False
115
116
116
117
def _cast_pointwise_result (self , op_name : str , obj , other , pointwise_result ):
117
118
dtype = typing .cast (pd .StringDtype , tm .get_dtype (obj ))
@@ -125,43 +126,6 @@ def _cast_pointwise_result(self, op_name: str, obj, other, pointwise_result):
125
126
def test_searchsorted (self , data_for_sorting , as_series ):
126
127
super ().test_searchsorted (self , data_for_sorting , as_series )
127
128
128
- def test_astype_str (self , data ):
129
- # Use `json.dumps(str)` instead of passing `str(obj)` directly to the super method.
130
- result = pd .Series (data [:5 ]).astype (str )
131
- expected = pd .Series (
132
- [json .dumps (x , sort_keys = True ) for x in data [:5 ]], dtype = str
133
- )
134
- tm .assert_series_equal (result , expected )
135
-
136
- @pytest .mark .parametrize (
137
- "nullable_string_dtype" ,
138
- [
139
- "string[python]" ,
140
- "string[pyarrow]" ,
141
- ],
142
- )
143
- def test_astype_string (self , data , nullable_string_dtype ):
144
- # Use `json.dumps(str)` instead of passing `str(obj)` directly to the super method.
145
- result = pd .Series (data [:5 ]).astype (nullable_string_dtype )
146
- expected = pd .Series (
147
- [json .dumps (x , sort_keys = True ) for x in data [:5 ]],
148
- dtype = nullable_string_dtype ,
149
- )
150
- tm .assert_series_equal (result , expected )
151
-
152
- def test_array_interface (self , data ):
153
- result = np .array (data )
154
- # Use `json.dumps(data[0])` instead of passing `data[0]` directly to the super method.
155
- assert result [0 ] == json .dumps (data [0 ])
156
-
157
- result = np .array (data , dtype = object )
158
- # Use `json.dumps(x)` instead of passing `x` directly to the super method.
159
- expected = np .array ([json .dumps (x ) for x in data ], dtype = object )
160
- if expected .ndim > 1 :
161
- # nested data, explicitly construct as 1D
162
- expected = construct_1d_object_array_from_listlike (list (data ))
163
- tm .assert_numpy_array_equal (result , expected )
164
-
165
129
@pytest .mark .xfail (reason = "Setting a dict as a scalar" )
166
130
def test_fillna_series (self ):
167
131
"""We treat dictionaries as a mapping in fillna, not a scalar."""
@@ -212,7 +176,7 @@ def test_series_constructor_scalar_with_index(self, data, dtype):
212
176
expected = pd .Series ([scalar ], index = ["foo" ], dtype = dtype )
213
177
tm .assert_series_equal (result , expected )
214
178
215
- # Patching `json.dumps ` to base.BaseSetitemTests because pandas' internals has
179
+ # Patching `[....] * len() ` to base.BaseSetitemTests because pandas' internals
216
180
# has trouble setting sequences of values into scalar positions.
217
181
218
182
@pytest .mark .parametrize (
@@ -228,8 +192,8 @@ def test_setitem_integer_array(self, data, idx, box_in_series):
228
192
arr = pd .Series (arr )
229
193
expected = pd .Series (expected )
230
194
231
- # Use json.dumps( arr[0]) instead of passing arr[0] directly to the super method.
232
- arr [idx ] = json . dumps ( arr [0 ])
195
+ # Use `[ arr[0]] * len()` instead of passing ` arr[0]` directly to the super method.
196
+ arr [idx ] = [ arr [0 ]] * len ( arr [ idx ])
233
197
tm .assert_equal (arr , expected )
234
198
235
199
@pytest .mark .parametrize ("setter" , ["loc" , None ])
@@ -243,60 +207,54 @@ def test_setitem_mask_broadcast(self, data, setter):
243
207
else : # __setitem__
244
208
target = ser
245
209
246
- # Use json.dumps( data[10]) instead of passing data[10] directly to the super method.
247
- target [mask ] = json . dumps ( data [10 ])
210
+ # Use `[ data[10]] * len()` instead of passing ` data[10]` directly to the super method.
211
+ target [mask ] = [ data [10 ]] * len ( target [ mask ])
248
212
assert ser [0 ] == data [10 ]
249
213
assert ser [1 ] == data [10 ]
250
214
251
215
def test_setitem_loc_scalar_mixed (self , data ):
252
216
df = pd .DataFrame ({"A" : np .arange (len (data )), "B" : data })
253
- # Use json.dumps( data[1]) instead of passing data[1] directly to the super method.
254
- df .loc [0 , "B" ] = json . dumps ( data [1 ])
217
+ # Use `[ data[1]]` instead of passing ` data[1]` directly to the super method.
218
+ df .loc [0 , "B" ] = [ data [1 ]]
255
219
assert df .loc [0 , "B" ] == data [1 ]
256
220
221
+ @pytest .mark .xfail (reason = "TODO: open an issue for ArrowExtentionArray" )
257
222
def test_setitem_loc_scalar_single (self , data ):
258
- df = pd .DataFrame ({"B" : data })
259
- # Use json.dumps(data[1]) instead of passing data[1] directly to the super method.
260
- df .loc [10 , "B" ] = json .dumps (data [1 ])
261
- assert df .loc [10 , "B" ] == data [1 ]
223
+ super ().test_setitem_loc_scalar_single (data )
262
224
263
225
def test_setitem_loc_iloc_slice (self , data ):
264
226
arr = data [:5 ].copy ()
265
227
s = pd .Series (arr , index = ["a" , "b" , "c" , "d" , "e" ])
266
228
expected = pd .Series (data .take ([0 , 0 , 0 , 3 , 4 ]), index = s .index )
267
229
268
230
result = s .copy ()
269
- # Use json.dumps( data[0]) instead of passing data[0] directly to the super method.
270
- result .iloc [:3 ] = json . dumps ( data [0 ])
231
+ # Use `[ data[0]] * len()` instead of passing ` data[0]` directly to the super method.
232
+ result .iloc [:3 ] = [ data [0 ]] * len ( result . iloc [: 3 ])
271
233
tm .assert_equal (result , expected )
272
234
273
235
result = s .copy ()
274
- result .loc [:"c" ] = json . dumps ( data [0 ])
236
+ result .loc [:"c" ] = [ data [0 ]] * len ( result . loc [: "c" ])
275
237
tm .assert_equal (result , expected )
276
238
239
+ @pytest .mark .xfail (reason = "TODO: open an issue for ArrowExtentionArray" )
277
240
def test_setitem_iloc_scalar_single (self , data ):
278
- df = pd .DataFrame ({"B" : data })
279
- # Use json.dumps(data[1]) instead of passing data[1] directly to the super method.
280
- df .iloc [10 , 0 ] = json .dumps (data [1 ])
281
- assert df .loc [10 , "B" ] == data [1 ]
241
+ super ().test_setitem_iloc_scalar_single (data )
282
242
283
243
def test_setitem_iloc_scalar_mixed (self , data ):
284
244
df = pd .DataFrame ({"A" : np .arange (len (data )), "B" : data })
285
- # Use json.dumps( data[1]) instead of passing data[1] directly to the super method.
286
- df .iloc [0 , 1 ] = json . dumps ( data [1 ])
245
+ # Use `[ data[1]] * len()` instead of passing ` data[1]` directly to the super method.
246
+ df .iloc [0 , 1 ] = [ data [1 ]] * len ( df . iloc [ 0 , 1 ])
287
247
assert df .loc [0 , "B" ] == data [1 ]
288
248
289
- @pytest .mark .xfail (reaons = "eq not implemented for <class 'dict'>" )
249
+ @pytest .mark .xfail (reason = "eq not implemented for <class 'dict'>" )
290
250
def test_setitem_mask_boolean_array_with_na (self , data , box_in_series ):
291
251
super ().test_setitem_mask_boolean_array_with_na (data , box_in_series )
292
252
293
253
@pytest .mark .parametrize ("setter" , ["loc" , "iloc" ])
254
+
255
+ @pytest .mark .xfail (reason = "TODO: open an issue for ArrowExtentionArray" )
294
256
def test_setitem_scalar (self , data , setter ):
295
- arr = pd .Series (data )
296
- setter = getattr (arr , setter )
297
- # Use json.dumps(data[1]) instead of passing data[1] directly to the super method.
298
- setter [0 ] = json .dumps (data [1 ])
299
- assert arr [0 ] == data [1 ]
257
+ super ().test_setitem_scalar (data , setter )
300
258
301
259
@pytest .mark .parametrize (
302
260
"mask" ,
@@ -313,35 +271,24 @@ def test_setitem_mask(self, data, mask, box_in_series):
313
271
if box_in_series :
314
272
arr = pd .Series (arr )
315
273
expected = pd .Series (expected )
316
- # Use json.dumps( data[0]) instead of passing data[0] directly to the super method.
317
- arr [mask ] = json . dumps ( data [0 ])
274
+ # Use `[ data[0]] * len()` instead of passing ` data[0]` directly to the super method.
275
+ arr [mask ] = [ data [0 ]] * len ( arr [ mask ])
318
276
tm .assert_equal (expected , arr )
319
277
278
+ @pytest .mark .xfail (reasons = "Setting a `dict` to an expansion row is not supported" )
320
279
def test_setitem_with_expansion_row (self , data , na_value ):
321
- df = pd .DataFrame ({"data" : data [:1 ]})
322
-
323
- # Use json.dumps(data[1]) instead of passing data[1] directly to the super method.
324
- df .loc [1 , "data" ] = json .dumps (data [1 ])
325
- expected = pd .DataFrame ({"data" : data [:2 ]})
326
- tm .assert_frame_equal (df , expected )
327
-
328
- # https://github.com/pandas-dev/pandas/issues/47284
329
- df .loc [2 , "data" ] = na_value
330
- expected = pd .DataFrame (
331
- {"data" : pd .Series ([data [0 ], data [1 ], na_value ], dtype = data .dtype )}
332
- )
333
- tm .assert_frame_equal (df , expected )
280
+ super ().test_setitem_with_expansion_row (data , na_value )
334
281
335
282
def test_setitem_iloc_scalar_multiple_homogoneous (self , data ):
336
283
df = pd .DataFrame ({"A" : data , "B" : data })
337
- # Use json.dumps( data[1]) instead of passing data[1] directly to the super method.
338
- df .iloc [10 , 1 ] = json . dumps ( data [1 ])
284
+ # Use `[ data[1]]` instead of passing ` data[1]` directly to the super method.
285
+ df .iloc [10 , 1 ] = [ data [1 ]]
339
286
assert df .loc [10 , "B" ] == data [1 ]
340
287
341
288
def test_setitem_loc_scalar_multiple_homogoneous (self , data ):
342
289
df = pd .DataFrame ({"A" : data , "B" : data })
343
- # Use json.dumps( data[1]) instead of passing data[1] directly to the super method.
344
- df .loc [10 , "B" ] = json . dumps ( data [1 ])
290
+ # Use `[ data[1]]` instead of passing ` data[1]` directly to the super method.
291
+ df .loc [10 , "B" ] = [ data [1 ]]
345
292
assert df .loc [10 , "B" ] == data [1 ]
346
293
347
294
def test_setitem_slice (self , data , box_in_series ):
@@ -351,8 +298,8 @@ def test_setitem_slice(self, data, box_in_series):
351
298
arr = pd .Series (arr )
352
299
expected = pd .Series (expected )
353
300
354
- # Use json.dumps( data[0]) instead of passing data[0] directly to the super method.
355
- arr [:3 ] = json . dumps ( data [0 ])
301
+ # Use `[ data[0]] * 3` instead of passing ` data[0]` directly to the super method.
302
+ arr [:3 ] = [ data [0 ]] * 3
356
303
tm .assert_equal (arr , expected )
357
304
358
305
@pytest .mark .xfail (reason = "only integer scalar arrays can be converted" )
0 commit comments