Skip to content

Commit 8466669

Browse files
DOC: Improved links between expanding and cum* (pandas-dev#12651)
Modified the auto-generating function so that the name of actual method [min/max/sum/prod] is available for link.
1 parent 30164f3 commit 8466669

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

doc/source/basics.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,9 @@ standard deviation 1), very concisely:
460460
xs_stand.std(1)
461461
462462
Note that methods like :meth:`~DataFrame.cumsum` and :meth:`~DataFrame.cumprod`
463-
preserve the location of NA values:
463+
preserve the location of ``NaN`` values. This is somewhat different from
464+
:meth:`~DataFrame.expanding` and :meth:`~DataFrame.rolling`.
465+
For more details please see :ref:`this note <stats.moments.expanding.note>`.
464466

465467
.. ipython:: python
466468

doc/source/computation.rst

+25-6
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,8 @@ Method Summary
686686
:meth:`~Expanding.cov`, Unbiased covariance (binary)
687687
:meth:`~Expanding.corr`, Correlation (binary)
688688

689+
.. currentmodule:: pandas
690+
689691
Aside from not having a ``window`` parameter, these functions have the same
690692
interfaces as their ``.rolling`` counterparts. Like above, the parameters they
691693
all accept are:
@@ -695,18 +697,34 @@ all accept are:
695697
``min_periods`` non-null data points have been seen.
696698
- ``center``: boolean, whether to set the labels at the center (default is False)
697699

700+
.. _stats.moments.expanding.note:
698701
.. note::
699702

700703
The output of the ``.rolling`` and ``.expanding`` methods do not return a
701704
``NaN`` if there are at least ``min_periods`` non-null values in the current
702-
window. This differs from ``cumsum``, ``cumprod``, ``cummax``, and
703-
``cummin``, which return ``NaN`` in the output wherever a ``NaN`` is
704-
encountered in the input.
705+
window. This differs from :meth:`~DataFrame.cumsum`, :meth:`~DataFrame.cumprod`,
706+
:meth:`~DataFrame.cummax`, and :meth:`~DataFrame.cummin`,
707+
which return ``NaN`` in the output wherever a ``NaN``
708+
is encountered in the input.
709+
710+
Please see the example below. In order to match the output of ``cumsum``
711+
with ``expanding``, use :meth:`~DataFrame.fillna`.
712+
713+
.. ipython:: python
714+
715+
sn = pd.Series([1,2,np.nan,3,np.nan,4])
716+
717+
sn.expanding().sum()
718+
719+
sn.cumsum()
720+
721+
sn.cumsum().fillna(method='ffill')
722+
705723
706724
An expanding window statistic will be more stable (and less responsive) than
707725
its rolling window counterpart as the increasing window size decreases the
708726
relative impact of an individual data point. As an example, here is the
709-
:meth:`~Expanding.mean` output for the previous time series dataset:
727+
:meth:`~core.window.Expanding.mean` output for the previous time series dataset:
710728

711729
.. ipython:: python
712730
:suppress:
@@ -726,13 +744,14 @@ relative impact of an individual data point. As an example, here is the
726744
Exponentially Weighted Windows
727745
------------------------------
728746

747+
.. currentmodule:: pandas.core.window
748+
729749
A related set of functions are exponentially weighted versions of several of
730750
the above statistics. A similar interface to ``.rolling`` and ``.expanding`` is accessed
731-
thru the ``.ewm`` method to receive an :class:`~pandas.core.window.EWM` object.
751+
through the ``.ewm`` method to receive an :class:`~EWM` object.
732752
A number of expanding EW (exponentially weighted)
733753
methods are provided:
734754

735-
.. currentmodule:: pandas.core.window
736755

737756
.. csv-table::
738757
:header: "Function", "Description"

pandas/core/generic.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -5363,16 +5363,16 @@ def compound(self, axis=None, skipna=None, level=None):
53635363

53645364
cls.cummin = _make_cum_function(
53655365
cls, 'cummin', name, name2, axis_descr, "cumulative minimum",
5366-
lambda y, axis: np.minimum.accumulate(y, axis), np.inf, np.nan)
5366+
lambda y, axis: np.minimum.accumulate(y, axis), "min", np.inf, np.nan)
53675367
cls.cumsum = _make_cum_function(
53685368
cls, 'cumsum', name, name2, axis_descr, "cumulative sum",
5369-
lambda y, axis: y.cumsum(axis), 0., np.nan)
5369+
lambda y, axis: y.cumsum(axis), "sum", 0., np.nan)
53705370
cls.cumprod = _make_cum_function(
53715371
cls, 'cumprod', name, name2, axis_descr, "cumulative product",
5372-
lambda y, axis: y.cumprod(axis), 1., np.nan)
5372+
lambda y, axis: y.cumprod(axis), "prod", 1., np.nan)
53735373
cls.cummax = _make_cum_function(
53745374
cls, 'cummax', name, name2, axis_descr, "cumulative max",
5375-
lambda y, axis: np.maximum.accumulate(y, axis), -np.inf, np.nan)
5375+
lambda y, axis: np.maximum.accumulate(y, axis), "max", -np.inf, np.nan)
53765376

53775377
cls.sum = _make_stat_function(
53785378
cls, 'sum', name, name2, axis_descr,
@@ -5560,7 +5560,15 @@ def _doc_parms(cls):
55605560
55615561
Returns
55625562
-------
5563-
%(outname)s : %(name1)s\n"""
5563+
%(outname)s : %(name1)s\n
5564+
5565+
5566+
See also
5567+
--------
5568+
pandas.core.window.Expanding.%(accum_func_name)s : Similar functionality
5569+
but ignores ``NaN`` values.
5570+
5571+
"""
55645572

55655573

55665574
def _make_stat_function(cls, name, name1, name2, axis_descr, desc, f):
@@ -5603,10 +5611,10 @@ def stat_func(self, axis=None, skipna=None, level=None, ddof=1,
56035611
return set_function_name(stat_func, name, cls)
56045612

56055613

5606-
def _make_cum_function(cls, name, name1, name2, axis_descr, desc, accum_func,
5607-
mask_a, mask_b):
5614+
def _make_cum_function(cls, name, name1, name2, axis_descr, desc,
5615+
accum_func, accum_func_name, mask_a, mask_b):
56085616
@Substitution(outname=name, desc=desc, name1=name1, name2=name2,
5609-
axis_descr=axis_descr)
5617+
axis_descr=axis_descr, accum_func_name=accum_func_name)
56105618
@Appender("Return {0} over requested axis.".format(desc) +
56115619
_cnum_doc)
56125620
def cum_func(self, axis=None, skipna=True, *args, **kwargs):

0 commit comments

Comments
 (0)