We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 436ada9 commit 9ac5373Copy full SHA for 9ac5373
pandas/tests/groupby/test_categorical.py
@@ -697,6 +697,27 @@ def test_preserve_categorical_dtype():
697
tm.assert_frame_equal(result2, expected)
698
699
700
+@pytest.mark.parametrize(
701
+ 'func, values',
702
+ [('first', ['second', 'first']),
703
+ ('last', ['fourth', 'third']),
704
+ ('min', ['fourth', 'first']),
705
+ ('max', ['second', 'third'])])
706
+def test_preserve_on_ordered_ops(func, values):
707
+ # gh-18502
708
+ # preserve the categoricals on ops
709
+ c = pd.Categorical(['first', 'second', 'third', 'fourth'], ordered=True)
710
+ df = pd.DataFrame(
711
+ {'payload': [-1, -2, -1, -2],
712
+ 'col': c})
713
+ g = df.groupby('payload')
714
+ result = getattr(g, func)()
715
+ expected = pd.DataFrame(
716
+ {'payload': [-2, -1],
717
+ 'col': pd.Series(values, dtype=c.dtype)}).set_index('payload')
718
+ tm.assert_frame_equal(result, expected)
719
+
720
721
def test_categorical_no_compress():
722
data = Series(np.random.randn(9))
723
0 commit comments