Skip to content

BUG: Fix qcut for nullable integers #31440

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 21 commits into from
Feb 2, 2020
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ ExtensionArray
^^^^^^^^^^^^^^

- Bug in dtype being lost in ``__invert__`` (``~`` operator) for extension-array backed ``Series`` and ``DataFrame`` (:issue:`23087`)
- Bug where :meth:`qcut` would raise when passed a nullable integer. (:issue:`31389`)
-


Expand Down
3 changes: 3 additions & 0 deletions pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ def qcut(
x = _preprocess_for_cut(x)
x, dtype = _coerce_to_type(x)

if is_extension_array_dtype(x.dtype) and is_integer_dtype(x.dtype):
x = x.to_numpy(dtype=np.float64, na_value=np.nan)

if is_integer(q):
quantiles = np.linspace(0, 1, q + 1)
else:
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/arrays/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,17 @@ def test_cut(bins, right, include_lowest):
tm.assert_categorical_equal(result, expected)


@pytest.mark.parametrize("q", [2, 5, 10])
def test_qcut_nullable_integer(q, any_nullable_int_dtype):
arr = pd.array(np.arange(100), dtype=any_nullable_int_dtype)
arr[::2] = pd.NA

result = pd.qcut(arr, q)
expected = pd.qcut(arr.astype(float), q)

tm.assert_categorical_equal(result, expected)


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

# shift
Expand Down