|
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 |
25 | 24 |
|
26 | 25 |
|
27 | 26 | class TestJSONArray(base.ExtensionTests):
|
@@ -126,6 +125,43 @@ def _cast_pointwise_result(self, op_name: str, obj, other, pointwise_result):
|
126 | 125 | def test_searchsorted(self, data_for_sorting, as_series):
|
127 | 126 | super().test_searchsorted(self, data_for_sorting, as_series)
|
128 | 127 |
|
| 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 | + |
129 | 165 | @pytest.mark.xfail(reason="Setting a dict as a scalar")
|
130 | 166 | def test_fillna_series(self):
|
131 | 167 | """We treat dictionaries as a mapping in fillna, not a scalar."""
|
@@ -251,7 +287,6 @@ def test_setitem_mask_boolean_array_with_na(self, data, box_in_series):
|
251 | 287 | super().test_setitem_mask_boolean_array_with_na(data, box_in_series)
|
252 | 288 |
|
253 | 289 | @pytest.mark.parametrize("setter", ["loc", "iloc"])
|
254 |
| - |
255 | 290 | @pytest.mark.xfail(reason="TODO: open an issue for ArrowExtentionArray")
|
256 | 291 | def test_setitem_scalar(self, data, setter):
|
257 | 292 | super().test_setitem_scalar(data, setter)
|
@@ -310,3 +345,26 @@ def test_setitem_2d_values(self, data):
|
310 | 345 | @pytest.mark.parametrize("engine", ["c", "python"])
|
311 | 346 | def test_EA_types(self, engine, data, request):
|
312 | 347 | super().test_EA_types(engine, data, request)
|
| 348 | + |
| 349 | + @pytest.mark.xfail( |
| 350 | + reason="`to_numpy` returns serialized JSON, " |
| 351 | + + "while `__getitem__` returns JSON objects." |
| 352 | + ) |
| 353 | + def test_setitem_frame_2d_values(self, data): |
| 354 | + super().test_setitem_frame_2d_values(data) |
| 355 | + |
| 356 | + @pytest.mark.xfail( |
| 357 | + reason="`to_numpy` returns serialized JSON, " |
| 358 | + + "while `__getitem__` returns JSON objects." |
| 359 | + ) |
| 360 | + def test_transpose_frame(self, data): |
| 361 | + # `DataFrame.T` calls `to_numpy` to get results. |
| 362 | + super().test_transpose_frame(data) |
| 363 | + |
| 364 | + @pytest.mark.xfail( |
| 365 | + reason="`to_numpy` returns serialized JSON, " |
| 366 | + + "while `__getitem__` returns JSON objects." |
| 367 | + ) |
| 368 | + def test_where_series(self, data, na_value, as_frame): |
| 369 | + # `Series.where` calls `to_numpy` to get results. |
| 370 | + super().test_where_series(data, na_value, as_frame) |
0 commit comments