10BC0 feat: support astype to the string "json" by chelsea-lin · Pull Request #2073 · googleapis/python-bigquery-dataframes · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions bigframes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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}
"""
)
Expand Down
12 changes: 12 additions & 0 deletions tests/system/small/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand D 3A69 own
0