Skip to content

Commit da034bf

Browse files
carlosdanielcsantosjreback
authored andcommitted
Commiting progress
1 parent 34f1309 commit da034bf

File tree

4 files changed

+114
-49
lines changed

4 files changed

+114
-49
lines changed

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5962,12 +5962,12 @@ def _add_series_or_dataframe_operations(cls):
59625962

59635963
@Appender(rwindow.rolling.__doc__)
59645964
def rolling(self, window, min_periods=None, freq=None, center=False,
5965-
win_type=None, on=None, axis=0):
5965+
win_type=None, on=None, axis=0, closed='right'):
59665966
axis = self._get_axis_number(axis)
59675967
return rwindow.rolling(self, window=window,
59685968
min_periods=min_periods, freq=freq,
59695969
center=center, win_type=win_type,
5970-
on=on, axis=axis)
5970+
on=on, axis=axis, closed=closed)
59715971

59725972
cls.rolling = rolling
59735973

pandas/core/window.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@
5656

5757
class _Window(PandasObject, SelectionMixin):
5858
_attributes = ['window', 'min_periods', 'freq', 'center', 'win_type',
59-
'axis', 'on']
59+
'axis', 'on', 'closed']
6060
exclusions = set()
6161

6262
def __init__(self, obj, window=None, min_periods=None, freq=None,
63-
center=False, win_type=None, axis=0, on=None, **kwargs):
63+
center=False, win_type=None, axis=0, on=None, closed='right',
64+
**kwargs):
6465

6566
if freq is not None:
6667
warnings.warn("The freq kw is deprecated and will be removed in a "
@@ -71,6 +72,7 @@ def __init__(self, obj, window=None, min_periods=None, freq=None,
7172
self.blocks = []
7273
self.obj = obj
7374
self.on = on
75+
self.closed = closed
7476
self.window = window
7577
self.min_periods = min_periods
7678
self.freq = freq
@@ -101,6 +103,9 @@ def validate(self):
101103
if self.min_periods is not None and not \
102104
is_integer(self.min_periods):
103105
raise ValueError("min_periods must be an integer")
106+
if self.closed not in ['right', 'both', 'left', 'neither']:
107+
raise ValueError("closed must be 'right', 'left', 'both' or "
108+
"'neither'")
104109

105110
def _convert_freq(self, how=None):
106111
""" resample according to the how, return a new object """
@@ -374,8 +379,12 @@ class Window(_Window):
374379
on : string, optional
375380
For a DataFrame, column on which to calculate
376381
the rolling window, rather than the index
382+
closed : 'right', 'left', 'both', 'neither'
383+
For offset-based windows, make the interval closed on the right, left,
384+
or on both endpoints. Can also make the interval open on both endpoints
385+
(neither).
377386
378-
.. versionadded:: 0.19.0
387+
.. versionadded:: 0.20.0
379388
380389
axis : int or string, default 0
381390
@@ -717,12 +726,12 @@ def _apply(self, func, name=None, window=None, center=None,
717726
raise ValueError("we do not support this function "
718727
"in _window.{0}".format(func))
719728

720-
def func(arg, window, min_periods=None):
729+
def func(arg, window, min_periods=None, closed=None):
721730
minp = check_minp(min_periods, window)
722731
# ensure we are only rolling on floats
723732
arg = _ensure_float64(arg)
724733
return cfunc(arg,
725-
window, minp, indexi, **kwargs)
734+
window, minp, indexi, closed, **kwargs)
726735

727736
# calculation function
728737
if center:
@@ -731,11 +740,13 @@ def func(arg, window, min_periods=None):
731740

732741
def calc(x):
733742
return func(np.concatenate((x, additional_nans)),
734-
window, min_periods=self.min_periods)
743+
window, min_periods=self.min_periods,
744+
closed=self.closed)
735745
else:
736746

737747
def calc(x):
738-
return func(x, window, min_periods=self.min_periods)
748+
return func(x, window, min_periods=self.min_periods,
749+
closed=self.closed)
739750

740751
with np.errstate(all='ignore'):
741752
if values.ndim > 1:
@@ -788,12 +799,13 @@ def apply(self, func, args=(), kwargs={}):
788799
window = self._get_window()
789800
offset = _offset(window, self.center)
790801
index, indexi = self._get_index()
802+
closed = self.closed
791803

792804
def f(arg, window, min_periods):
793805
minp = _use_window(min_periods, window)
794806
return _window.roll_generic(arg, window, minp, indexi,
795807
offset, func, args,
796-
kwargs)
808+
kwargs, closed)
797809

798810
return self._apply(f, func, args=args, kwargs=kwargs,
799811
center=False)

0 commit comments

Comments
 (0)