8000 feat: Support isin with bigframes.pandas.Index arg by TrevorBergeron · Pull Request #1779 · googleapis/python-bigquery-dataframes · GitHub
[go: up one dir, main page]

Skip to content

feat: Support isin with bigframes.pandas.Index arg #1779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 3, 2025
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
4 changes: 4 additions & 0 deletions bigframes/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@ def unique(self, level: Hashable | int | None = None) -> Index:
return self.get_level_values(level).drop_duplicates()

def isin(self, values) -> Index:
import bigframes.series as series

if isinstance(values, (series.Series, Index)):
return Index(self.to_series().isin(values))
if not utils.is_list_like(values):
raise TypeError(
"only list-like objects are allowed to be passed to "
Expand Down
4 changes: 3 additions & 1 deletion bigframes/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,10 @@ def nsmallest(self, n: int = 5, keep: str = "first") -> Series:
)

def isin(self, values) -> "Series" | None:
if isinstance(values, (Series,)):
if isinstance(values, Series):
return Series(self._block.isin(values._block))
if isinstance(values, indexes.Index):
return Series(self._block.isin(values.to_series()._block))
if not _is_list_like(values):
raise TypeError(
"only list-like objects are allowed to be passed to "
Expand Down
34 changes: 33 additions & 1 deletion tests/system/small/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def test_index_drop_duplicates(scalars_df_index, scalars_pandas_df_index, keep):
)


def test_index_isin(scalars_df_index, scalars_pandas_df_index):
def test_index_isin_list(scalars_df_index, scalars_pandas_df_index):
col_name = "int64_col"
bf_series = (
scalars_df_index.set_index(col_name).index.isin([2, 55555, 4]).to_pandas()
Expand All @@ -389,6 +389,38 @@ def test_index_isin(scalars_df_index, scalars_pandas_df_index):
)


def test_index_isin_bf_series(scalars_df_index, scalars_pandas_df_index, session):
col_name = "int64_col"
bf_series = (
scalars_df_index.set_index(col_name)
.index.isin(bpd.Series([2, 55555, 4], session=session))
.to_pandas()
)
pd_result_array = scalars_pandas_df_index.set_index(col_name).index.isin(
[2, 55555, 4]
)
pd.testing.assert_index_equal(
pd.Index(pd_result_array).set_names(col_name),
bf_series,
)


def test_index_isin_bf_index(scalars_df_index, scalars_pandas_df_index, session):
col_name = "int64_col"
bf_series = (
scalars_df_index.set_index(col_name)
.index.isin(bpd.Index([2, 55555, 4], session=session))
.to_pandas()
)
pd_result_array = scalars_pandas_df_index.set_index(col_name).index.isin(
[2, 55555, 4]
)
pd.testing.assert_index_equal(
pd.Index(pd_result_array).set_names(col_name),
bf_series,
)


def test_multiindex_name_is_none(session):
df = pd.DataFrame(
{
Expand Down
18 changes: 18 additions & 0 deletions tests/system/small/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,24 @@ def test_isin_bigframes_values(scalars_dfs, col_name, test_set, session):
)


def test_isin_bigframes_index(scalars_dfs, session):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = (
scalars_df["string_col"]
.isin(bigframes.pandas.Index(["Hello, World!", "Hi", "こんにちは"], session=session))
.to_pandas()
)
pd_result = (
scalars_pandas_df["string_col"]
.isin(pd.Index(["Hello, World!", "Hi", "こんにちは"]))
.astype("boolean")
)
pd.testing.assert_series_equal(
pd_result,
bf_result,
)


@pytest.mark.parametrize(
( 42F1
"col_name",
Expand Down
0