Skip to content

Commit 60deef1

Browse files
authored
fix: remove unbox json functionality from JSONArrowType (#325)
* fix: remove unbox json functionality from JSONArrowType * lint
1 parent dc3ef63 commit 60deef1

File tree

3 files changed

+28
-55
lines changed

3 files changed

+28
-55
lines changed

db_dtypes/__init__.py

Lines changed: 1 addition & 2 deletions
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

Lines changed: 0 additions & 10 deletions
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

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import json
16-
import math
1716

1817
import numpy as np
1918
import pandas as pd
@@ -160,20 +159,15 @@ def test_json_arrow_to_pandas():
160159
s = arr.to_pandas()
161160
assert isinstance(s.dtypes, db_dtypes.JSONDtype)
162161
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])
162+
assert s[1] == "100"
163+
assert s[2] == "0.98"
164+
assert s[3] == '"hello world"'
165+
assert s[4] == "[0.1,0.2]"
166+
assert (
167+
s[5]
168+
== '{"null_field":null,"order":{"address":{"city":"Anytown","street":"123 Main St"},"items":["book","pen","computer"],"total":15}}'
169+
)
170+
assert s[6] == "null"
177171

178172

179173
def test_json_arrow_to_pylist():
@@ -186,20 +180,15 @@ def test_json_arrow_to_pylist():
186180
s = arr.to_pylist()
187181
assert isinstance(s, list)
188182
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
183+
assert s[1] == "100"
184+
assert s[2] == "0.98"
185+
assert s[3] == '"hello world"'
186+
assert s[4] == "[0.1,0.2]"
187+
assert (
188+
s[5]
189+
== '{"null_field":null,"order":{"address":{"city":"Anytown","street":"123 Main St"},"items":["book","pen","computer"],"total":15}}'
190+
)
191+
assert s[6] == "null"
203192

204193

205194
def test_json_arrow_record_batch():
@@ -226,17 +215,12 @@ def test_json_arrow_record_batch():
226215

227216
assert isinstance(s, list)
228217
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
218+
assert s[1] == "100"
219+
assert s[2] == "0.98"
220+
assert s[3] == '"hello world"'
221+
assert s[4] == "[0.1,0.2]"
222+
assert (
223+
s[5]
224+
== '{"null_field":null,"order":{"address":{"city":"Anytown","street":"123 Main St"},"items":["book","pen","computer"],"total":15}}'
225+
)
226+
assert s[6] == "null"

0 commit comments

Comments
 (0)