Skip to content

Commit efa182a

Browse files
committed
feat: support pd.cut() for array-like type
1 parent 913de1b commit efa182a

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

bigframes/core/reshape/tile.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import bigframes_vendored.pandas.core.reshape.tile as vendored_pandas_tile
2121
import pandas as pd
2222

23+
import bigframes
2324
import bigframes.constants
2425
import bigframes.core.expression as ex
2526
import bigframes.core.ordering as order
@@ -60,9 +61,12 @@ def cut(
6061
f"but found {type(list(labels)[0])}. {constants.FEEDBACK_LINK}"
6162
)
6263

63-
if x.size == 0:
64+
if len(x) == 0:
6465
raise ValueError("Cannot cut empty array.")
6566

67+
if not isinstance(x, bigframes.series.Series):
68+
x = bigframes.series.Series(x)
69+
6670
if isinstance(bins, int):
6771
if bins <= 0:
6872
raise ValueError("`bins` should be a positive integer.")

tests/system/small/test_pandas.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,18 @@ def _convert_pandas_category(pd_s: pd.Series):
520520
)
521521

522522

523+
def test_cut_for_array():
524+
"""Avoid regressions for internal issue 329866195"""
525+
sc=[30,80,40,90,60,45,95,75,55,100,65,85]
526+
x=[20,40,60,80,100]
527+
528+
pd_result = pd.cut(sc, x)
529+
bf_result = bpd.cut(sc, x)
530+
531+
pd_result = _convert_pandas_category(pd.Series(pd_result))
532+
pd.testing.assert_series_equal(bf_result.to_pandas(), pd_result)
533+
534+
523535
@pytest.mark.parametrize(
524536
("right", "labels"),
525537
[

third_party/bigframes_vendored/pandas/core/reshape/tile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
def cut(
15-
x: series.Series,
15+
x,
1616
bins: typing.Union[
1717
int,
1818
pd.IntervalIndex,
@@ -113,7 +113,7 @@ def cut(
113113
dtype: struct<left_inclusive: int64, right_exclusive: int64>[pyarrow]
114114
115115
Args:
116-
x (bigframes.pandas.Series):
116+
x (array-like):
117117
The input Series to be binned. Must be 1-dimensional.
118118
bins (int, pd.IntervalIndex, Iterable):
119119
The criteria to bin by.

0 commit comments

Comments
 (0)