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 5 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
6 changes: 5 additions & 1 deletion bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3674,7 +3674,11 @@ 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

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
17 changes: 17 additions & 0 deletions tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,23 @@ def test_apply_series_scalar_callable(
pandas.testing.assert_series_equal(bf_result, pd_result)


def test_apply_from_read_gbq_function(dataset_id_permanent):
jialuoo marked this conversation as resolved.
Show resolved Hide resolved
@bpd.remote_function()
jialuoo marked this conversation as resolved.
Show resolved Hide resolved
def tenfold(num: float) -> float:
return num * 10.0

# Read back the deployed BQ remote function via read_gbq_function.
tenfold_ref = bpd.read_gbq_function(function_name=tenfold.bigframes_remote_function)

data = {"a": [1.0, 2.0], "b": [3.0, 4.0], "c": [5.0, 6.0]}
bdf = bpd.DataFrame(data)
# Applying the remote function in different ways should result in the same results.
result = bdf.apply(tenfold).to_pandas()
result_gbq = bdf.apply(tenfold_ref).to_pandas()

pandas.testing.assert_frame_equal(result, result_gbq)


def test_df_pipe(
scalars_df_index,
scalars_pandas_df_index,
Expand Down
Loading