Skip to content

Commit 9e19dc1

Browse files
committed
Update categorical.py
1 parent 34a1048 commit 9e19dc1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

pandas/core/arrays/categorical.py

+24
Original file line numberDiff line numberDiff line change
@@ -2216,6 +2216,30 @@ def equals(self, other: object) -> bool:
22162216
return np.array_equal(self._codes, other._codes)
22172217
return False
22182218

2219+
def _accumulate(self, name: str, skipna: bool = True, **kwargs):
2220+
if self.ordered:
2221+
if name == "cummin":
2222+
func = np.minimum.accumulate
2223+
elif name == "cummax":
2224+
func = np.maximum.accumulate
2225+
else:
2226+
raise NotImplementedError(f"cannot perform {name} with type {self.dtype}")
2227+
2228+
codes = self.codes.copy()
2229+
mask = self.codes == -1
2230+
codes[mask] = self.max()
2231+
result_codes = func(codes)
2232+
2233+
# Restore the original NaN values
2234+
result_codes[mask] = -1
2235+
2236+
return pd.Categorical.from_codes(
2237+
result_codes, categories=self.categories, ordered=self.ordered
2238+
)
2239+
else:
2240+
raise NotImplementedError(f"cannot perform {name} with type {self.dtype}")
2241+
2242+
22192243
@classmethod
22202244
def _concat_same_type(cls, to_concat: Sequence[Self], axis: AxisInt = 0) -> Self:
22212245
from pandas.core.dtypes.concat import union_categoricals

0 commit comments

Comments
 (0)