Skip to content

Commit 306b9f7

Browse files
carlosdanielcsantosjreback
authored andcommitted
Commiting progress on default=None. Still not tested
Adding computation.rst section (still not written)
1 parent ec4bbc7 commit 306b9f7

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

doc/source/computation.rst

+21
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,27 @@ default of the index) in a DataFrame.
459459
dft
460460
dft.rolling('2s', on='foo').sum()
461461
462+
.. _stats.rolling_window.endpoints:
463+
464+
Rolling window endpoint inclusion
465+
~~~~~~~~~~~~~~~~~~
466+
467+
.. versionadded:: 0.20.0
468+
469+
New in version 0.19.0 are the ability to pass an offset (or convertible) to a ``.rolling()`` method and have it produce
470+
variable sized windows based on the passed time window. For each time point, this includes all preceding values occurring
471+
within the indicated time delta.
472+
473+
This can be particularly useful for a non-regular time frequency index.
474+
475+
.. ipython:: python
476+
477+
dft = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]},
478+
index=pd.date_range('20130101 09:00:00', periods=5, freq='s'))
479+
dft
480+
481+
This is a regular frequency index. Using an integer window parameter works to roll along the window frequency.
482+
462483
.. _stats.moments.ts-versus-resampling:
463484

464485
Time-aware Rolling vs. Resampling

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ To convert a ``SparseDataFrame`` back to sparse SciPy matrix in COO format, you
319319
Other Enhancements
320320
^^^^^^^^^^^^^^^^^^
321321

322-
- ``DataFrame.rolling()`` now accepts the parameter ``closed='right'|'left'|'both'|'neither'`` to choose the rolling window endpoint closedness. (:issue:`13965`)
322+
- ``DataFrame.rolling()`` now accepts the parameter ``closed='right'|'left'|'both'|'neither'`` to choose the rolling window endpoint closedness. See :ref:`documentation <stats.rolling_window.endpoints>` (:issue:`13965`)
323323
- Integration with the ``feather-format``, including a new top-level ``pd.read_feather()`` and ``DataFrame.to_feather()`` method, see :ref:`here <io.feather>`.
324324
- ``Series.str.replace()`` now accepts a callable, as replacement, which is passed to ``re.sub`` (:issue:`15055`)
325325
- ``Series.str.replace()`` now accepts a compiled regular expression as a pattern (:issue:`15446`)

pandas/core/window.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class _Window(PandasObject, SelectionMixin):
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, closed='right',
63+
center=False, win_type=None, axis=0, on=None, closed=None,
6464
**kwargs):
6565

6666
if freq is not None:
@@ -379,10 +379,12 @@ class Window(_Window):
379379
on : string, optional
380380
For a DataFrame, column on which to calculate
381381
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).
382+
closed : string, default None
383+
Make the interval closed on the 'right', 'left', 'both' or
384+
'neither' endpoints.
385+
For offset-based windows, it defaults to 'right'.
386+
For fixed windows, defaults to 'both'. Remaining cases not implemented
387+
for fixed windows.
386388
387389
.. versionadded:: 0.20.0
388390

pandas/core/window.pyx

+9-5
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ cdef class MockFixedWindowIndexer(WindowIndexer):
165165
166166
"""
167167
def __init__(self, ndarray input, int64_t win, int64_t minp,
168-
object index=None, object floor=None,
169-
bint left_closed=False, bint right_closed=True):
168+
bint left_closed, bint right_closed,
169+
object index=None, object floor=None):
170170

171171
assert index is None
172172
self.is_variable = 0
@@ -203,8 +203,8 @@ cdef class FixedWindowIndexer(WindowIndexer):
203203
204204
"""
205205
def __init__(self, ndarray input, int64_t win, int64_t minp,
206-
object index=None, object floor=None,
207-
bint left_closed=False, bint right_closed=True):
206+
bint left_closed, bint right_closed,
207+
object index=None, object floor=None):
208208
cdef ndarray start_s, start_e, end_s, end_e
209209

210210
assert index is None
@@ -352,7 +352,11 @@ def get_window_indexer(input, win, minp, index, closed,
352352
bint left_closed = False
353353
bint right_closed = False
354354

355-
assert closed in ['right', 'left', 'both', 'neither']
355+
assert closed is None or closed in ['right', 'left', 'both', 'neither']
356+
357+
# if windows is variable, default is 'right', otherwise default is 'both'
358+
if closed is None:
359+
closed = 'right' if index is not None else 'both'
356360

357361
if closed in ['right', 'both']:
358362
right_closed = True

pandas/tests/test_window.py

+4
Original file line numberDiff line numberDiff line change
@@ -3405,6 +3405,10 @@ def test_closed(self):
34053405
result = df.rolling('2s', closed='right').sum()
34063406
tm.assert_frame_equal(result, expected)
34073407

3408+
# default should be 'right'
3409+
result = df.rolling('2s').sum()
3410+
tm.assert_frame_equal(result, expected)
3411+
34083412
expected = df.copy()
34093413
expected["A"] = [1.0, 2, 3, 3, 2]
34103414
result = df.rolling('2s', closed='both').sum()

0 commit comments

Comments
 (0)