From 52e16c9eb3b842ec446925404fb837a2428e1936 Mon Sep 17 00:00:00 2001 From: Chelsea Lin Date: Thu, 11 Sep 2025 20:29:03 +0000 Subject: [PATCH] feat: support astype to json --- bigframes/dtypes.py | 9 ++++++--- tests/system/small/test_series.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/bigframes/dtypes.py b/bigframes/dtypes.py index ef1b9e7871..ae68dbe7d3 100644 --- a/bigframes/dtypes.py +++ b/bigframes/dtypes.py @@ -641,6 +641,9 @@ def _dtype_from_string(dtype_string: str) -> typing.Optional[Dtype]: return BIGFRAMES_STRING_TO_BIGFRAMES[ typing.cast(DtypeString, str(dtype_string)) ] + if isinstance(dtype_string, str) and dtype_string.lower() == "json": + return JSON_DTYPE + raise TypeError( textwrap.dedent( f""" @@ -652,9 +655,9 @@ def _dtype_from_string(dtype_string: str) -> typing.Optional[Dtype]: The following pandas.ExtensionDtype are supported: pandas.BooleanDtype(), pandas.Float64Dtype(), pandas.Int64Dtype(), pandas.StringDtype(storage="pyarrow"), - pd.ArrowDtype(pa.date32()), pd.ArrowDtype(pa.time64("us")), - pd.ArrowDtype(pa.timestamp("us")), - pd.ArrowDtype(pa.timestamp("us", tz="UTC")). + pandas.ArrowDtype(pa.date32()), pandas.ArrowDtype(pa.time64("us")), + pandas.ArrowDtype(pa.timestamp("us")), + pandas.ArrowDtype(pa.timestamp("us", tz="UTC")). {constants.FEEDBACK_LINK} """ ) diff --git a/tests/system/small/test_series.py b/tests/system/small/test_series.py index 165e3b6df0..ca08f8dece 100644 --- a/tests/system/small/test_series.py +++ b/tests/system/small/test_series.py @@ -3903,6 +3903,18 @@ def test_float_astype_json(errors): pd.testing.assert_series_equal(bf_result.to_pandas(), expected_result) +def test_float_astype_json_str(): + data = ["1.25", "2500000000", None, "-12323.24"] + bf_series = series.Series(data, dtype=dtypes.FLOAT_DTYPE) + + bf_result = bf_series.astype("json") + assert bf_result.dtype == dtypes.JSON_DTYPE + + expected_result = pd.Series(data, dtype=dtypes.JSON_DTYPE) + expected_result.index = expected_result.index.astype("Int64") + pd.testing.assert_series_equal(bf_result.to_pandas(), expected_result) + + @pytest.mark.parametrize("errors", ["raise", "null"]) def test_string_astype_json(errors): data = [