Skip to content

Commit 9e92dc7

Browse files
committed
only import when pandas version is higher than 1.5.0
1 parent 8cda5e8 commit 9e92dc7

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

db_dtypes/__init__.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import pyarrow.compute
2929

3030
from db_dtypes import core
31-
from db_dtypes.json import JSONArray, JSONDtype
3231
from db_dtypes.version import __version__
3332

3433
date_dtype_name = "dbdate"
@@ -44,7 +43,14 @@
4443
# nanosecond precision when boxing scalars.
4544
_NP_BOX_DTYPE = "datetime64[us]"
4645

47-
pandas_release = packaging.version.parse(pandas.__version__).release
46+
47+
# To use JSONArray and JSONDtype, you'll need Pandas 1.5.0 or later. With the removal
48+
# of Python 3.7 compatibility, the minimum Pandas version will be updated to 1.5.0.
49+
if packaging.version.Version(pandas.__version__) >= packaging.version.Version("1.5.0"):
50+
from db_dtypes.json import JSONArray, JSONDtype
51+
else:
52+
JSONArray = None
53+
JSONDtype = None
4854

4955

5056
@pandas.api.extensions.register_extension_dtype

db_dtypes/json.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,13 @@ def na_value(self) -> pd.NA:
4848

4949
@property
5050
def type(self) -> type[str]:
51-
"""Return the scalar type for the array, e.g. int."""
52-
return dict
51+
"""
52+
Return the scalar type for the array elements.
53+
The standard JSON data types can be one of `dict`, `list`, `str`, `int`, `float`,
54+
`bool` and `None`. However, this method returns a `str` type to indicate its
55+
storage type, because the union of multiple types are not supported well in pandas.
56+
"""
57+
return str
5358

5459
@property
5560
def _is_numeric(self) -> bool:

tests/compliance/json/test_json_compliance.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818
import numpy as np
1919
import pandas as pd
2020
import pandas._testing as tm
21-
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
22-
from pandas.tests.extension import base
21+
import pandas.tests.extension.base
2322
import pytest
2423

2524

26-
class TestJSONArray(base.ExtensionTests):
25+
class TestJSONArray(pandas.tests.extension.base.ExtensionTests):
2726
@pytest.mark.xfail(reason="Unhashable")
2827
def test_value_counts_with_normalize(self, data):
2928
super().test_value_counts_with_normalize(data)
@@ -157,9 +156,9 @@ def test_array_interface(self, data):
157156
result = np.array(data, dtype=object)
158157
# Use `json.dumps(x)` instead of passing `x` directly to the super method.
159158
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))
159+
# if expected.ndim > 1:
160+
# # nested data, explicitly construct as 1D
161+
# expected = construct_1d_object_array_from_listlike(list(data))
163162
tm.assert_numpy_array_equal(result, expected)
164163

165164
@pytest.mark.xfail(reason="Setting a dict as a scalar")
@@ -212,6 +211,16 @@ def test_series_constructor_scalar_with_index(self, data, dtype):
212211
expected = pd.Series([scalar], index=["foo"], dtype=dtype)
213212
tm.assert_series_equal(result, expected)
214213

214+
@pytest.mark.xfail(reason="Unhashable")
215+
def test_getitem_scalar(self, data):
216+
"""
217+
`_getitem_` can return any JSON-types objects while `data.dtype.type` returns
218+
a string to indicate its storage type.
219+
> assert isinstance(result, data.dtype.type)
220+
E AssertionError
221+
"""
222+
super().test_getitem_scalar()
223+
215224
# Patching `[....] * len()` to base.BaseSetitemTests because pandas' internals
216225
# has trouble setting sequences of values into scalar positions.
217226

0 commit comments

Comments
 (0)