Skip to content

Commit 6bd6738

Browse files
authored
feat: support astype to json (#2073)
1 parent f454779 commit 6bd6738

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

bigframes/dtypes.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,9 @@ def _dtype_from_string(dtype_string: str) -> typing.Optional[Dtype]:
641641
return BIGFRAMES_STRING_TO_BIGFRAMES[
642642
typing.cast(DtypeString, str(dtype_string))
643643
]
644+
if isinstance(dtype_string, str) and dtype_string.lower() == "json":
645+
return JSON_DTYPE
646+
644647
raise TypeError(
645648
textwrap.dedent(
646649
f"""
@@ -652,9 +655,9 @@ def _dtype_from_string(dtype_string: str) -> typing.Optional[Dtype]:
652655
The following pandas.ExtensionDtype are supported:
653656
pandas.BooleanDtype(), pandas.Float64Dtype(),
654657
pandas.Int64Dtype(), pandas.StringDtype(storage="pyarrow"),
655-
pd.ArrowDtype(pa.date32()), pd.ArrowDtype(pa.time64("us")),
656-
pd.ArrowDtype(pa.timestamp("us")),
657-
pd.ArrowDtype(pa.timestamp("us", tz="UTC")).
658+
pandas.ArrowDtype(pa.date32()), pandas.ArrowDtype(pa.time64("us")),
659+
pandas.ArrowDtype(pa.timestamp("us")),
660+
pandas.ArrowDtype(pa.timestamp("us", tz="UTC")).
658661
{constants.FEEDBACK_LINK}
659662
"""
660663
)

tests/system/small/test_series.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3903,6 +3903,18 @@ def test_float_astype_json(errors):
39033903
pd.testing.assert_series_equal(bf_result.to_pandas(), expected_result)
39043904

39053905

3906+
def test_float_astype_json_str():
3907+
data = ["1.25", "2500000000", None, "-12323.24"]
3908+
bf_series = series.Series(data, dtype=dtypes.FLOAT_DTYPE)
3909+
3910+
bf_result = bf_series.astype("json")
3911+
assert bf_result.dtype == dtypes.JSON_DTYPE
3912+
3913+
expected_result = pd.Series(data, dtype=dtypes.JSON_DTYPE)
3914+
expected_result.index = expected_result.index.astype("Int64")
3915+
pd.testing.assert_series_equal(bf_result.to_pandas(), expected_result)
3916+
3917+
39063918
@pytest.mark.parametrize("errors", ["raise", "null"])
39073919
def test_string_astype_json(errors):
39083920
data = [

0 commit comments

Comments
 (0)