|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import pandas |
| 16 | +import pyarrow |
16 | 17 | import pytest |
17 | 18 |
|
| 19 | +import bigframes |
18 | 20 | from bigframes.functions import _function_session as bff_session |
19 | 21 | from bigframes.functions._utils import get_python_version |
20 | 22 | import bigframes.pandas as bpd |
@@ -164,3 +166,161 @@ def func(x, y): |
164 | 166 | cleanup_function_assets( |
165 | 167 | session.bqclient, session.cloudfunctionsclient, managed_func |
166 | 168 | ) |
| 169 | + |
| 170 | + |
| 171 | +@pytest.mark.parametrize( |
| 172 | + "array_dtype", |
| 173 | + [ |
| 174 | + bool, |
| 175 | + int, |
| 176 | + float, |
| 177 | + str, |
| 178 | + ], |
| 179 | +) |
| 180 | +@pytest.mark.skipif( |
| 181 | + get_python_version() not in bff_session._MANAGED_FUNC_PYTHON_VERSIONS, |
| 182 | + reason=f"Supported version: {bff_session._MANAGED_FUNC_PYTHON_VERSIONS}", |
| 183 | +) |
| 184 | +def test_managed_function_array_output(session, scalars_dfs, dataset_id, array_dtype): |
| 185 | + try: |
| 186 | + |
| 187 | + @session.udf(dataset=dataset_id) |
| 188 | + def featurize(x: int) -> list[array_dtype]: # type: ignore |
| 189 | + return [array_dtype(i) for i in [x, x + 1, x + 2]] |
| 190 | + |
| 191 | + scalars_df, scalars_pandas_df = scalars_dfs |
| 192 | + |
| 193 | + bf_int64_col = scalars_df["int64_too"] |
| 194 | + bf_result = bf_int64_col.apply(featurize).to_pandas() |
| 195 | + |
| 196 | + pd_int64_col = scalars_pandas_df["int64_too"] |
| 197 | + pd_result = pd_int64_col.apply(featurize) |
| 198 | + |
| 199 | + # Ignore any dtype disparity. |
| 200 | + pandas.testing.assert_series_equal(pd_result, bf_result, check_dtype=False) |
| 201 | + |
| 202 | + finally: |
| 203 | + # Clean up the gcp assets created for the managed function. |
| 204 | + cleanup_function_assets( |
| 205 | + featurize, session.bqclient, session.cloudfunctionsclient |
| 206 | + ) |
| 207 | + |
| 208 | + |
| 209 | +@pytest.mark.skipif( |
| 210 | + get_python_version() not in bff_session._MANAGED_FUNC_PYTHON_VERSIONS, |
| 211 | + reason=f"Supported version: {bff_session._MANAGED_FUNC_PYTHON_VERSIONS}", |
| 212 | +) |
| 213 | +def test_managed_function_binop_array_output(session, scalars_dfs, dataset_id): |
| 214 | + try: |
| 215 | + |
| 216 | + def func(x, y): |
| 217 | + return [len(x), abs(y % 4)] |
| 218 | + |
| 219 | + managed_func = session.udf( |
| 220 | + input_types=[str, int], |
| 221 | + output_type=list[int], |
| 222 | + dataset=dataset_id, |
| 223 | + )(func) |
| 224 | + |
| 225 | + scalars_df, scalars_pandas_df = scalars_dfs |
| 226 | + |
| 227 | + scalars_df = scalars_df.dropna() |
| 228 | + scalars_pandas_df = scalars_pandas_df.dropna() |
| 229 | + bf_result = ( |
| 230 | + scalars_df["string_col"] |
| 231 | + .combine(scalars_df["int64_col"], managed_func) |
| 232 | + .to_pandas() |
| 233 | + ) |
| 234 | + pd_result = scalars_pandas_df["string_col"].combine( |
| 235 | + scalars_pandas_df["int64_col"], func |
| 236 | + ) |
| 237 | + pandas.testing.assert_series_equal(bf_result, pd_result, check_dtype=False) |
| 238 | + finally: |
| 239 | + # Clean up the gcp assets created for the managed function. |
| 240 | + cleanup_function_assets( |
| 241 | + managed_func, session.bqclient, session.cloudfunctionsclient |
| 242 | + ) |
| 243 | + |
| 244 | + |
| 245 | +@pytest.mark.skipif( |
| 246 | + get_python_version() not in bff_session._MANAGED_FUNC_PYTHON_VERSIONS, |
| 247 | + reason=f"Supported version: {bff_session._MANAGED_FUNC_PYTHON_VERSIONS}", |
| 248 | +) |
| 249 | +def test_manage_function_df_apply_axis_1_array_output(session): |
| 250 | + bf_df = bigframes.dataframe.DataFrame( |
| 251 | + { |
| 252 | + "Id": [1, 2, 3], |
| 253 | + "Age": [22.5, 23, 23.5], |
| 254 | + "Name": ["alpha", "beta", "gamma"], |
| 255 | + } |
| 256 | + ) |
| 257 | + |
| 258 | + expected_dtypes = ( |
| 259 | + bigframes.dtypes.INT_DTYPE, |
| 260 | + bigframes.dtypes.FLOAT_DTYPE, |
| 261 | + bigframes.dtypes.STRING_DTYPE, |
| 262 | + ) |
| 263 | + |
| 264 | + # Assert the dataframe dtypes. |
| 265 | + assert tuple(bf_df.dtypes) == expected_dtypes |
| 266 | + |
| 267 | + try: |
| 268 | + |
| 269 | + @session.udf(input_types=[int, float, str], output_type=list[str]) |
| 270 | + def foo(x, y, z): |
| 271 | + return [str(x), str(y), z] |
| 272 | + |
| 273 | + assert getattr(foo, "is_row_processor") is False |
| 274 | + assert getattr(foo, "input_dtypes") == expected_dtypes |
| 275 | + assert getattr(foo, "output_dtype") == pandas.ArrowDtype( |
| 276 | + pyarrow.list_( |
| 277 | + bigframes.dtypes.bigframes_dtype_to_arrow_dtype( |
| 278 | + bigframes.dtypes.STRING_DTYPE |
| 279 | + ) |
| 280 | + ) |
| 281 | + ) |
| 282 | + assert getattr(foo, "output_dtype") == getattr( |
| 283 | + foo, "bigframes_bigquery_function_output_dtype" |
| 284 | + ) |
| 285 | + |
| 286 | + # Fails to apply on dataframe with incompatible number of columns. |
| 287 | + with pytest.raises( |
| 288 | + ValueError, |
| 289 | + match="^BigFrames BigQuery function takes 3 arguments but DataFrame has 2 columns\\.$", |
| 290 | + ): |
| 291 | + bf_df[["Id", "Age"]].apply(foo, axis=1) |
| 292 | + |
| 293 | + with pytest.raises( |
| 294 | + ValueError, |
| 295 | + match="^BigFrames BigQuery function takes 3 arguments but DataFrame has 4 columns\\.$", |
| 296 | + ): |
| 297 | + bf_df.assign(Country="lalaland").apply(foo, axis=1) |
| 298 | + |
| 299 | + # Fails to apply on dataframe with incompatible column datatypes. |
| 300 | + with pytest.raises( |
| 301 | + ValueError, |
| 302 | + match="^BigFrames BigQuery function takes arguments of types .* but DataFrame dtypes are .*", |
| 303 | + ): |
| 304 | + bf_df.assign(Age=bf_df["Age"].astype("Int64")).apply(foo, axis=1) |
| 305 | + |
| 306 | + # Successfully applies to dataframe with matching number of columns. |
| 307 | + # and their datatypes. |
| 308 | + bf_result = bf_df.apply(foo, axis=1).to_pandas() |
| 309 | + |
| 310 | + # Since this scenario is not pandas-like, let's handcraft the |
| 311 | + # expected result. |
| 312 | + expected_result = pandas.Series( |
| 313 | + [ |
| 314 | + ["1", "22.5", "alpha"], |
| 315 | + ["2", "23.0", "beta"], |
| 316 | + ["3", "23.5", "gamma"], |
| 317 | + ] |
| 318 | + ) |
| 319 | + |
| 320 | + pandas.testing.assert_series_equal( |
| 321 | + expected_result, bf_result, check_dtype=False, check_index_type=False |
| 322 | + ) |
| 323 | + |
| 324 | + finally: |
| 325 | + # Clean up the gcp assets created for the managed function. |
| 326 | + cleanup_function_assets(foo, session.bqclient, session.cloudfunctionsclient) |
0 commit comments