Skip to content

Commit 4d3a29e

Browse files
committed
BUG: Fix ValueError when grouping by read-only category with sort=False (pandas-dev#33410)
1 parent b7e786e commit 4d3a29e

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ Groupby/resample/rolling
517517
- Bug in :meth:`SeriesGroupBy.first`, :meth:`SeriesGroupBy.last`, :meth:`SeriesGroupBy.min`, and :meth:`SeriesGroupBy.max` returning floats when applied to nullable Booleans (:issue:`33071`)
518518
- Bug in :meth:`DataFrameGroupBy.agg` with dictionary input losing ``ExtensionArray`` dtypes (:issue:`32194`)
519519
- Bug in :meth:`DataFrame.resample` where an ``AmbiguousTimeError`` would be raised when the resulting timezone aware :class:`DatetimeIndex` had a DST transition at midnight (:issue:`25758`)
520+
- Bug in :meth:`DataFrame.groupby` where a ``ValueError`` would be raised when grouping by a categorical column with read-only categories and ``sort=False`` (:issue:`33410`)
520521

521522
Reshaping
522523
^^^^^^^^^

pandas/_libs/hashtable_func_helper.pxi.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def duplicated_{{dtype}}({{c_type}}[:] values, object keep='first'):
206206
{{if dtype == 'object'}}
207207
def ismember_{{dtype}}(ndarray[{{c_type}}] arr, ndarray[{{c_type}}] values):
208208
{{else}}
209-
def ismember_{{dtype}}({{c_type}}[:] arr, {{c_type}}[:] values):
209+
def ismember_{{dtype}}(const {{c_type}}[:] arr, {{c_type}}[:] values):
210210
{{endif}}
211211
"""
212212
Return boolean of values in arr on an

pandas/tests/groupby/test_categorical.py

+18
Original file line numberDiff line numberDiff line change
@@ -1380,3 +1380,21 @@ def test_groupby_agg_non_numeric():
13801380

13811381
result = df.groupby([1, 2, 1]).nunique()
13821382
tm.assert_frame_equal(result, expected)
1383+
1384+
1385+
def test_read_only_category_no_sort():
1386+
# GH33410
1387+
cats = np.array([1, 2])
1388+
cats.flags.writeable = False
1389+
df = DataFrame(
1390+
{
1391+
"a": [1, 3, 5, 7],
1392+
"b": Categorical([1, 1, 2, 2], categories=Index(cats))
1393+
}
1394+
)
1395+
expected = DataFrame(
1396+
data={"a": [2, 6]},
1397+
index=CategoricalIndex([1, 2], name="b")
1398+
)
1399+
result = df.groupby("b", sort=False).mean()
1400+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)