Skip to content

BUG: Issue with pd.cut on Series with duplicate index #42448

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 9 commits into from
Jul 28, 2021
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ Groupby/resample/rolling
Reshaping
^^^^^^^^^
- :func:`concat` creating :class:`MultiIndex` with duplicate level entries when concatenating a :class:`DataFrame` with duplicates in :class:`Index` and multiple keys (:issue:`42651`)
- Bug in :meth:`pandas.cut` on :class:`Series` with duplicate indices (:issue:`42185`) and non-exact :meth:`pandas.CategoricalIndex` (:issue:`42425`)
-

Sparse
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def qcut(

def _bins_to_cuts(
x,
bins,
bins: np.ndarray,
right: bool = True,
labels=None,
precision: int = 3,
Expand Down Expand Up @@ -421,7 +421,7 @@ def _bins_to_cuts(
ids = ensure_platform_int(bins.searchsorted(x, side=side))

if include_lowest:
ids[x == bins[0]] = 1
ids[np.asarray(x) == bins[0]] = 1

na_mask = isna(x) | (ids == len(bins)) | (ids == 0)
has_nas = na_mask.any()
Expand Down
45 changes: 45 additions & 0 deletions pandas/tests/reshape/test_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,3 +691,48 @@ def test_cut_no_warnings():
labels = [f"{i} - {i + 9}" for i in range(0, 100, 10)]
with tm.assert_produces_warning(False):
df["group"] = cut(df.value, range(0, 105, 10), right=False, labels=labels)


def test_cut_with_duplicated_index_lowest_included():
# GH 42185
expected = Series(
[Interval(-0.001, 2, closed="right")] * 3
+ [Interval(2, 4, closed="right"), Interval(-0.001, 2, closed="right")],
index=[0, 1, 2, 3, 0],
dtype="category",
).cat.as_ordered()

s = Series([0, 1, 2, 3, 0], index=[0, 1, 2, 3, 0])
result = cut(s, bins=[0, 2, 4], include_lowest=True)
tm.assert_series_equal(result, expected)


def test_cut_with_nonexact_categorical_indices():
# GH 42424

ser = Series(range(0, 100))
ser1 = cut(ser, 10).value_counts().head(5)
ser2 = cut(ser, 10).value_counts().tail(5)
result = DataFrame({"1": ser1, "2": ser2})

index = pd.CategoricalIndex(
[
Interval(-0.099, 9.9, closed="right"),
Interval(9.9, 19.8, closed="right"),
Interval(19.8, 29.7, closed="right"),
Interval(29.7, 39.6, closed="right"),
Interval(39.6, 49.5, closed="right"),
Interval(49.5, 59.4, closed="right"),
Interval(59.4, 69.3, closed="right"),
Interval(69.3, 79.2, closed="right"),
Interval(79.2, 89.1, closed="right"),
Interval(89.1, 99, closed="right"),
],
ordered=True,
)

expected = DataFrame(
{"1": [10] * 5 + [np.nan] * 5, "2": [np.nan] * 5 + [10] * 5}, index=index
)

tm.assert_frame_equal(expected, result)