Skip to content

BUG: Handle IntegerArray in pd.cut #31290

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

Merged
merged 4 commits into from
Jan 28, 2020
Merged
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
8 changes: 8 additions & 0 deletions pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
is_datetime64_dtype,
is_datetime64tz_dtype,
is_datetime_or_timedelta_dtype,
is_extension_array_dtype,
is_integer,
is_integer_dtype,
is_list_like,
is_scalar,
is_timedelta64_dtype,
Expand Down Expand Up @@ -205,6 +207,12 @@ def cut(
x = _preprocess_for_cut(x)
x, dtype = _coerce_to_type(x)

# To support cut(IntegerArray), we convert to object dtype with NaN
# Will properly support in the future.
# https://github.com/pandas-dev/pandas/pull/31290
if is_extension_array_dtype(x.dtype) and is_integer_dtype(x.dtype):
x = x.to_numpy(dtype=object, na_value=np.nan)

if not np.iterable(bins):
if is_scalar(bins) and bins < 1:
raise ValueError("`bins` should be a positive integer.")
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/arrays/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,19 @@ def test_value_counts_na():
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize("bins", [3, [0, 5, 15]])
@pytest.mark.parametrize("right", [True, False])
@pytest.mark.parametrize("include_lowest", [True, False])
def test_cut(bins, right, include_lowest):
a = np.random.randint(0, 10, size=50).astype(object)
a[::2] = np.nan
result = pd.cut(
pd.array(a, dtype="Int64"), bins, right=right, include_lowest=include_lowest
)
expected = pd.cut(a, bins, right=right, include_lowest=include_lowest)
tm.assert_categorical_equal(result, expected)


# TODO(jreback) - these need testing / are broken

# shift
Expand Down