Skip to content

Commit 927324e

Browse files
GarrettWushobsi
authored andcommitted
chore: rewrite function axis=1 warning in blob transform functions (#1538)
1 parent d34c142 commit 927324e

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

bigframes/dataframe.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4170,8 +4170,10 @@ def apply(self, func, *, axis=0, args: typing.Tuple = (), **kwargs):
41704170
# to the applied function should be a Series, not a scalar.
41714171

41724172
if utils.get_axis_number(axis) == 1:
4173-
msg = bfe.format_message("axis=1 scenario is in preview.")
4174-
warnings.warn(msg, category=bfe.PreviewWarning)
4173+
msg = bfe.format_message(
4174+
"DataFrame.apply with parameter axis=1 scenario is in preview."
4175+
)
4176+
warnings.warn(msg, category=bfe.FunctionAxisOnePreviewWarning)
41754177

41764178
if not hasattr(func, "bigframes_bigquery_function"):
41774179
raise ValueError(

bigframes/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ class ObsoleteVersionWarning(Warning):
9595
"""The BigFrames version is too old."""
9696

9797

98+
class FunctionAxisOnePreviewWarning(PreviewWarning):
99+
"""Remote Function and Managed UDF with axis=1 preview."""
100+
101+
98102
def format_message(message: str, fill: bool = True):
99103
"""Formats a warning message with ANSI color codes for the warning color.
100104

bigframes/operations/blob.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
import os
1818
from typing import cast, Optional, Union
19+
import warnings
1920

2021
import IPython.display as ipy_display
2122
import pandas as pd
2223
import requests
2324

2425
from bigframes import clients
2526
import bigframes.dataframe
27+
import bigframes.exceptions as bfe
2628
from bigframes.operations import base
2729
import bigframes.operations as ops
2830
import bigframes.series
@@ -166,6 +168,30 @@ def _get_runtime(
166168

167169
return s._apply_unary_op(ops.ObjGetAccessUrl(mode=mode))
168170

171+
def _df_apply_udf(
172+
self, df: bigframes.dataframe.DataFrame, udf
173+
) -> bigframes.series.Series:
174+
# Catch and rethrow function axis=1 warning to be more user-friendly.
175+
with warnings.catch_warnings(record=True) as catched_warnings:
176+
s = df.apply(udf, axis=1)
177+
for w in catched_warnings:
178+
if isinstance(w.message, bfe.FunctionAxisOnePreviewWarning):
179+
warnings.warn(
180+
"Blob Functions use bigframes DataFrame Managed function with axis=1 senario, which is a preview feature.",
181+
category=w.category,
182+
stacklevel=2,
183+
)
184+
else:
185+
warnings.warn_explicit(
186+
message=w.message,
187+
category=w.category,
188+
filename=w.filename,
189+
lineno=w.lineno,
190+
source=w.source,
191+
)
192+
193+
return s
194+
169195
def read_url(self) -> bigframes.series.Series:
170196
"""Retrieve the read URL of the Blob.
171197
@@ -346,7 +372,7 @@ def image_blur(
346372

347373
df["ksize_x"], df["ksize_y"] = ksize
348374
df["ext"] = ext # type: ignore
349-
res = df.apply(image_blur_udf, axis=1)
375+
res = self._df_apply_udf(df, image_blur_udf)
350376

351377
return res
352378

@@ -375,7 +401,7 @@ def image_blur(
375401
df["ksize_x"], df["ksize_y"] = ksize
376402
df["ext"] = ext # type: ignore
377403

378-
res = df.apply(image_blur_udf, axis=1)
404+
res = self._df_apply_udf(df, image_blur_udf)
379405
res.cache() # to execute the udf
380406

381407
self._add_to_cleanup_set(image_blur_udf)
@@ -443,7 +469,7 @@ def image_resize(
443469
df["dsize_x"], df["dsizye_y"] = dsize
444470
df["fx"], df["fy"] = fx, fy
445471
df["ext"] = ext # type: ignore
446-
res = df.apply(image_resize_udf, axis=1)
472+
res = self._df_apply_udf(df, image_resize_udf)
447473

448474
return res
449475

@@ -473,7 +499,7 @@ def image_resize(
473499
df["fx"], df["fy"] = fx, fy
474500
df["ext"] = ext # type: ignore
475501

476-
res = df.apply(image_resize_udf, axis=1)
502+
res = self._df_apply_udf(df, image_resize_udf)
477503
res.cache() # to execute the udf
478504

479505
self._add_to_cleanup_set(image_resize_udf)
@@ -535,7 +561,7 @@ def image_normalize(
535561
df["beta"] = beta
536562
df["norm_type"] = norm_type
537563
df["ext"] = ext # type: ignore
538-
res = df.apply(image_normalize_udf, axis=1)
564+
res = self._df_apply_udf(df, image_normalize_udf)
539565

540566
return res
541567

@@ -566,7 +592,7 @@ def image_normalize(
566592
df["norm_type"] = norm_type
567593
df["ext"] = ext # type: ignore
568594

569-
res = df.apply(image_normalize_udf, axis=1)
595+
res = self._df_apply_udf(df, image_normalize_udf)
570596
res.cache() # to execute the udf
571597

572598
self._add_to_cleanup_set(image_normalize_udf)
@@ -681,7 +707,7 @@ def pdf_chunk(
681707
df["chunk_size"] = chunk_size
682708
df["overlap_size"] = overlap_size
683709

684-
res = df.apply(pdf_chunk_udf, axis=1)
710+
res = self._df_apply_udf(df, pdf_chunk_udf)
685711

686712
res_array = bbq.json_extract_string_array(res)
687713

0 commit comments

Comments
 (0)