Skip to content

Commit e0ff186

Browse files
committed
fix: remove unbox json functionality from JSONArrowType
1 parent dc3ef63 commit e0ff186

File tree

3 files changed

+28
-54
lines changed

3 files changed

+28
-54
lines changed

db_dtypes/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
# To use JSONArray and JSONDtype, you'll need Pandas 1.5.0 or later. With the removal
5151
# of Python 3.7 compatibility, the minimum Pandas version will be updated to 1.5.0.
5252
if packaging.version.Version(pandas.__version__) >= packaging.version.Version("1.5.0"):
53-
from db_dtypes.json import JSONArray, JSONArrowScalar, JSONArrowType, JSONDtype
53+
from db_dtypes.json import JSONArray, JSONArrowType, JSONDtype
5454
else:
5555
JSONArray = None
5656
JSONDtype = None
@@ -375,7 +375,6 @@ def __sub__(self, other):
375375
"JSONDtype",
376376
"JSONArray",
377377
"JSONArrowType",
378-
"JSONArrowScalar",
379378
"TimeArray",
380379
"TimeDtype",
381380
]

db_dtypes/json.py

-10
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,6 @@ def __array__(self, dtype=None, copy: bool | None = None) -> np.ndarray:
256256
return result
257257

258258

259-
class JSONArrowScalar(pa.ExtensionScalar):
260-
def as_py(self, **kwargs):
261-
return JSONArray._deserialize_json(
262-
self.value.as_py(**kwargs) if self.value else None
263-
)
264-
265-
266259
class JSONArrowType(pa.ExtensionType):
267260
"""Arrow extension type for the `dbjson` Pandas extension type."""
268261

@@ -282,9 +275,6 @@ def __hash__(self) -> int:
282275
def to_pandas_dtype(self):
283276
return JSONDtype()
284277

285-
def __arrow_ext_scalar_class__(self):
286-
return JSONArrowScalar
287-
288278

289279
# Register the type to be included in RecordBatches, sent over IPC and received in
290280
# another Python process.

tests/unit/test_json.py

+27-42
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,15 @@ def test_json_arrow_to_pandas():
160160
s = arr.to_pandas()
161161
assert isinstance(s.dtypes, db_dtypes.JSONDtype)
162162
assert s[0]
163-
assert s[1] == 100
164-
assert math.isclose(s[2], 0.98)
165-
assert s[3] == "hello world"
166-
assert math.isclose(s[4][0], 0.1)
167-
assert math.isclose(s[4][1], 0.2)
168-
assert s[5] == {
169-
"null_field": None,
170-
"order": {
171-
"items": ["book", "pen", "computer"],
172-
"total": 15,
173-
"address": {"street": "123 Main St", "city": "Anytown"},
174-
},
175-
}
176-
assert pd.isna(s[6])
163+
assert s[1] == "100"
164+
assert s[2] == "0.98"
165+
assert s[3] == '"hello world"'
166+
assert s[4] == "[0.1,0.2]"
167+
assert (
168+
s[5]
169+
== '{"null_field":null,"order":{"address":{"city":"Anytown","street":"123 Main St"},"items":["book","pen","computer"],"total":15}}'
170+
)
171+
assert s[6] == "null"
177172

178173

179174
def test_json_arrow_to_pylist():
@@ -186,20 +181,15 @@ def test_json_arrow_to_pylist():
186181
s = arr.to_pylist()
187182
assert isinstance(s, list)
188183
assert s[0]
189-
assert s[1] == 100
190-
assert math.isclose(s[2], 0.98)
191-
assert s[3] == "hello world"
192-
assert math.isclose(s[4][0], 0.1)
193-
assert math.isclose(s[4][1], 0.2)
194-
assert s[5] == {
195-
"null_field": None,
196-
"order": {
197-
"items": ["book", "pen", "computer"],
198-
"total": 15,
199-
"address": {"street": "123 Main St", "city": "Anytown"},
200-
},
201-
}
202-
assert s[6] is None
184+
assert s[1] == "100"
185+
assert s[2] == "0.98"
186+
assert s[3] == '"hello world"'
187+
assert s[4] == "[0.1,0.2]"
188+
assert (
189+
s[5]
190+
== '{"null_field":null,"order":{"address":{"city":"Anytown","street":"123 Main St"},"items":["book","pen","computer"],"total":15}}'
191+
)
192+
assert s[6] == "null"
203193

204194

205195
def test_json_arrow_record_batch():
@@ -226,17 +216,12 @@ def test_json_arrow_record_batch():
226216

227217
assert isinstance(s, list)
228218
assert s[0]
229-
assert s[1] == 100
230-
assert math.isclose(s[2], 0.98)
231-
assert s[3] == "hello world"
232-
assert math.isclose(s[4][0], 0.1)
233-
assert math.isclose(s[4][1], 0.2)
234-
assert s[5] == {
235-
"null_field": None,
236-
"order": {
237-
"items": ["book", "pen", "computer"],
238-
"total": 15,
239-
"address": {"street": "123 Main St", "city": "Anytown"},
240-
},
241-
}
242-
assert s[6] is None
219+
assert s[1] == "100"
220+
assert s[2] == "0.98"
221+
assert s[3] == '"hello world"'
222+
assert s[4] == "[0.1,0.2]"
223+
assert (
224+
s[5]
225+
== '{"null_field":null,"order":{"address":{"city":"Anytown","street":"123 Main St"},"items":["book","pen","computer"],"total":15}}'
226+
)
227+
assert s[6] == "null"

0 commit comments

Comments
 (0)