Skip to content

Commit 830eb42

Browse files
Fabrizio Pollastriadamklein
Fabrizio Pollastri
authored andcommitted
* Added cummax and cummin methods for Series And DataFrame.
1 parent 3ce01cf commit 830eb42

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

pandas/core/generic.py

+72
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,78 @@ def cumprod(self, axis=None, skipna=True):
435435
result = y.cumprod(axis)
436436
return self._wrap_array(result, self.axes, copy=False)
437437

438+
def cummax(self, axis=None, skipna=True):
439+
"""
440+
Return DataFrame of cumulative max over requested axis.
441+
442+
Parameters
443+
----------
444+
axis : {0, 1}
445+
0 for row-wise, 1 for column-wise
446+
skipna : boolean, default True
447+
Exclude NA/null values. If an entire row/column is NA, the result
448+
will be NA
449+
450+
Returns
451+
-------
452+
y : DataFrame
453+
"""
454+
if axis is None:
455+
axis = self._default_stat_axis
456+
else:
457+
axis = self._get_axis_number(axis)
458+
459+
y = self.values.copy()
460+
if not issubclass(y.dtype.type, np.integer):
461+
mask = np.isnan(self.values)
462+
463+
if skipna:
464+
np.putmask(y, mask, -np.inf)
465+
466+
result = np.maximum.accumulate(y,axis)
467+
468+
if skipna:
469+
np.putmask(result, mask, np.nan)
470+
else:
471+
result = np.maximum.accumulate(y,axis)
472+
return self._wrap_array(result, self.axes, copy=False)
473+
474+
def cummin(self, axis=None, skipna=True):
475+
"""
476+
Return DataFrame of cumulative min over requested axis.
477+
478+
Parameters
479+
----------
480+
axis : {0, 1}
481+
0 for row-wise, 1 for column-wise
482+
skipna : boolean, default True
483+
Exclude NA/null values. If an entire row/column is NA, the result
484+
will be NA
485+
486+
Returns
487+
-------
488+
y : DataFrame
489+
"""
490+
if axis is None:
491+
axis = self._default_stat_axis
492+
else:
493+
axis = self._get_axis_number(axis)
494+
495+
y = self.values.copy()
496+
if not issubclass(y.dtype.type, np.integer):
497+
mask = np.isnan(self.values)
498+
499+
if skipna:
500+
np.putmask(y, mask, np.inf)
501+
502+
result = np.minimum.accumulate(y,axis)
503+
504+
if skipna:
505+
np.putmask(result, mask, np.nan)
506+
else:
507+
result = np.minimum.accumulate(y,axis)
508+
return self._wrap_array(result, self.axes, copy=False)
509+
438510
def copy(self, deep=True):
439511
"""
440512
Make a copy of this object

pandas/core/series.py

+58
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,64 @@ def cumprod(self, axis=0, dtype=None, out=None, skipna=True):
989989

990990
return Series(result, index=self.index)
991991

992+
def cummax(self, axis=0, dtype=None, out=None, skipna=True):
993+
"""
994+
Cumulative max of values. Preserves locations of NaN values
995+
996+
Extra parameters are to preserve ndarray interface.
997+
998+
Parameters
999+
----------
1000+
skipna : boolean, default True
1001+
Exclude NA/null values
1002+
1003+
Returns
1004+
-------
1005+
cummax : Series
1006+
"""
1007+
arr = self.values.copy()
1008+
1009+
do_mask = skipna and not issubclass(self.dtype.type, np.integer)
1010+
if do_mask:
1011+
mask = isnull(arr)
1012+
np.putmask(arr, mask, -np.inf)
1013+
1014+
result = np.maximum.accumulate(arr)
1015+
1016+
if do_mask:
1017+
np.putmask(result, mask, np.nan)
1018+
1019+
return Series(result, index=self.index)
1020+
1021+
def cummin(self, axis=0, dtype=None, out=None, skipna=True):
1022+
"""
1023+
Cumulative min of values. Preserves locations of NaN values
1024+
1025+
Extra parameters are to preserve ndarray interface.
1026+
1027+
Parameters
1028+
----------
1029+
skipna : boolean, default True
1030+
Exclude NA/null values
1031+
1032+
Returns
1033+
-------
1034+
cummin : Series
1035+
"""
1036+
arr = self.values.copy()
1037+
1038+
do_mask = skipna and not issubclass(self.dtype.type, np.integer)
1039+
if do_mask:
1040+
mask = isnull(arr)
1041+
np.putmask(arr, mask, np.inf)
1042+
1043+
result = np.minimum.accumulate(arr)
1044+
1045+
if do_mask:
1046+
np.putmask(result, mask, np.nan)
1047+
1048+
return Series(result, index=self.index)
1049+
9921050
@Appender(np.ndarray.round.__doc__)
9931051
def round(self, decimals=0, out=None):
9941052
"""

0 commit comments

Comments
 (0)