Skip to content
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

fix: fix read_gbq_function issue in dataframe apply method #1174

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3959,7 +3959,16 @@ def apply(self, func, *, axis=0, args: typing.Tuple = (), **kwargs):
return result_series

# Per-column apply
results = {name: func(col, *args, **kwargs) for name, col in self.items()}
if hasattr(func, "bigframes_remote_function"):
jialuoo marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TrevorBergeron could you please review if this element-wise proxy for series-wise application doesn't have any blind sight?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't dataframe.apply for column or series-wise application? Why do we need to support applying as an element-wise operator? isn't this what dataframe.map is for?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For scalar ops the end result is the same. I think we don't lose anything by being lax for user convenience. Note that this API has other non-pandas-like extended behavior

# This is a special case where we are providing not-pandas-like

if args or kwargs:
warnings.warn(
"The args and kwargs are not supported in the remote function.",
category=bigframes.exceptions.ArgsAndKwargsNotSupportedWarning,
)
results = {name: col.apply(func) for name, col in self.items()}
jialuoo marked this conversation as resolved.
Show resolved Hide resolved
else:
results = {name: func(col, *args, **kwargs) for name, col in self.items()}
jialuoo marked this conversation as resolved.
Show resolved Hide resolved

if all(
[
isinstance(val, bigframes.series.Series) or utils.is_list_like(val)
Expand Down
4 changes: 4 additions & 0 deletions bigframes/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ class ApiDeprecationWarning(FutureWarning):

class BadIndexerKeyWarning(Warning):
"""The indexer key is not used correctly."""


class ArgsAndKwargsNotSupportedWarning(Warning):
"""The args and kwargs are not supported."""
24 changes: 24 additions & 0 deletions tests/system/small/test_remote_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,30 @@ def test_read_gbq_function_enforces_explicit_types(
)


@pytest.mark.flaky(retries=2, delay=120)
def test_df_apply(session, scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
bdf = bigframes.pandas.DataFrame(
{
"Column1": scalars_df["string_col"],
"Column2": scalars_df["string_col"],
}
)

func_ref = session.read_gbq_function("bqutil.fn.cw_lower_case_ascii_only")
bf_result = bdf.apply(func_ref).to_pandas()
# The str.lower() method has the same functionality as func_ref.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true for ASCII only, we should say that in the comment

pd_result = pd.DataFrame(
{
"Column1": scalars_pandas_df["string_col"].str.lower(),
"Column2": scalars_pandas_df["string_col"].str.lower(),
}
)

pd.testing.assert_series_equal(pd_result["Column1"], bf_result["Column1"])
pd.testing.assert_series_equal(pd_result["Column2"], bf_result["Column2"])


@pytest.mark.flaky(retries=2, delay=120)
def test_read_gbq_function_multiple_inputs_not_a_row_processor(session):
with pytest.raises(ValueError) as context:
Expand Down
Loading