Skip to content

Commit b5f6e59

Browse files
authored
BUG: Fix ValueError when grouping by read-only category (#33410) (#33446)
1 parent 0c69615 commit b5f6e59

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ Groupby/resample/rolling
536536
- Bug in :meth:`SeriesGroupBy.first`, :meth:`SeriesGroupBy.last`, :meth:`SeriesGroupBy.min`, and :meth:`SeriesGroupBy.max` returning floats when applied to nullable Booleans (:issue:`33071`)
537537
- Bug in :meth:`DataFrameGroupBy.agg` with dictionary input losing ``ExtensionArray`` dtypes (:issue:`32194`)
538538
- 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`)
539+
- 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`)
539540

540541
Reshaping
541542
^^^^^^^^^

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

+12
Original file line numberDiff line numberDiff line change
@@ -1380,3 +1380,15 @@ 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+
{"a": [1, 3, 5, 7], "b": Categorical([1, 1, 2, 2], categories=Index(cats))}
1391+
)
1392+
expected = DataFrame(data={"a": [2, 6]}, index=CategoricalIndex([1, 2], name="b"))
1393+
result = df.groupby("b", sort=False).mean()
1394+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)