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
2 changes: 2 additions & 0 deletions bigframes/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def __new__(
pd_df = pandas.DataFrame(index=data)
block = df.DataFrame(pd_df, session=session)._block
else:
if isinstance(dtype, str) and dtype.lower() == "json":
dtype = bigframes.dtypes.JSON_DTYPE
pd_index = pandas.Index(data=data, dtype=dtype, name=name)
pd_df = pandas.DataFrame(index=pd_index)
block = df.DataFrame(pd_df, session=session)._block
Expand Down
3 changes: 3 additions & 0 deletions bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ def __init__(
block = block.multi_apply_unary_op(ops.AsTypeOp(to_type=bf_dtype))

else:
if isinstance(dtype, str) and dtype.lower() == "json":
dtype = bigframes.dtypes.JSON_DTYPE

import bigframes.pandas

pd_dataframe = pandas.DataFrame(
Expand Down
2 changes: 2 additions & 0 deletions bigframes/operations/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def __init__(
bf_dtype = bigframes.dtypes.bigframes_type(dtype)
block = block.multi_apply_unary_op(ops.AsTypeOp(to_type=bf_dtype))
else:
if isinstance(dtype, str) and dtype.lower() == "json":
dtype = bigframes.dtypes.JSON_DTYPE
pd_series = pd.Series(
data=data,
index=index, # type:ignore
Expand Down
20 changes: 20 additions & 0 deletions tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,26 @@ def test_df_construct_from_dict():
)


@pytest.mark.parametrize(
("json_type"),
[
pytest.param(dtypes.JSON_DTYPE),
pytest.param("json"),
],
)
def test_df_construct_w_json_dtype(json_type):
data = [
"1",
"false",
'["a", {"b": 1}, null]',
None,
]
df = dataframe.DataFrame({"json_col": data}, dtype=json_type)

assert df["json_col"].dtype == dtypes.JSON_DTYPE
assert df["json_col"][1] == "false"


def test_df_construct_inline_respects_location(reset_default_session_and_location):
# Note: This starts a thread-local session.
with bpd.option_context("bigquery.location", "europe-west1"):
Expand Down
21 changes: 21 additions & 0 deletions tests/system/small/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pandas as pd
import pytest

from bigframes import dtypes
import bigframes.pandas as bpd
from bigframes.testing.utils import assert_pandas_index_equal_ignore_index_type

Expand Down Expand Up @@ -61,6 +62,26 @@ def test_index_construct_from_index():
pd.testing.assert_index_equal(bf_result, pd_result)


@pytest.mark.parametrize(
("json_type"),
[
pytest.param(dtypes.JSON_DTYPE),
pytest.param("json"),
],
)
def test_index_construct_w_json_dtype(json_type):
data = [
"1",
"false",
'["a", {"b": 1}, null]',
None,
]
index = bpd.Index(data, dtype=json_type)

assert index.dtype == dtypes.JSON_DTYPE
assert index[1] == "false"


def test_get_index(scalars_df_index, scalars_pandas_df_index):
index = scalars_df_index.index
bf_result = index.to_pandas()
Expand Down
12 changes: 10 additions & 2 deletions tests/system/small/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,14 @@ def test_series_construct_local_unordered_has_sequential_index(unordered_session
pd.testing.assert_index_equal(series.index.to_pandas(), expected)


def test_series_construct_w_dtype_for_json():
@pytest.mark.parametrize(
("json_type"),
[
pytest.param(dtypes.JSON_DTYPE),
pytest.param("json"),
],
)
def test_series_construct_w_json_dtype(json_type):
data = [
"1",
'"str"',
Expand All @@ -335,8 +342,9 @@ def test_series_construct_w_dtype_for_json():
None,
'{"a": {"b": [1, 2, 3], "c": true}}',
]
s = bigframes.pandas.Series(data, dtype=dtypes.JSON_DTYPE)
s = bigframes.pandas.Series(data, dtype=json_type)

assert s.dtype == dtypes.JSON_DTYPE
assert s[0] == "1"
assert s[1] == '"str"'
assert s[2] == "false"
Expand Down