From 0baae11bc1430195be3254983abd312303ba1dbd Mon Sep 17 00:00:00 2001 From: Paul Reidy Date: Sat, 21 Oct 2017 22:18:33 +0100 Subject: [PATCH 1/2] DOC: Clarify use of resample bins (#14888) --- doc/source/timeseries.rst | 19 ++++++++++++++++++- pandas/core/generic.py | 10 +++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/doc/source/timeseries.rst b/doc/source/timeseries.rst index 36ffe8806f373..54a31fe79b61a 100644 --- a/doc/source/timeseries.rst +++ b/doc/source/timeseries.rst @@ -1485,12 +1485,29 @@ labels. .. ipython:: python - ts.resample('5Min').mean() # by default label='right' + ts.resample('5Min').mean() # by default label='left' ts.resample('5Min', label='left').mean() ts.resample('5Min', label='left', loffset='1s').mean() +It is important to note that the default values for ``label`` and ``closed`` +is 'left' for all frequency offsets except for 'M', 'A', 'Q', 'BM', +'BA', 'BQ', and 'W' which all have a default of 'right'. + +.. ipython:: python + + rng2 = pd.date_range('1/1/2012', end='3/31/2012', freq='D') + ts2 = pd.Series(range(len(rng2)), index=rng2) + + # default: label='right', closed='right' + ts2.resample('M').max() + + # default: label='left', closed='left' + ts2.resample('SM').max() + + ts2.resample('SM', label='right', closed='right').max() + The ``axis`` parameter can be set to 0 or 1 and allows you to resample the specified axis for a ``DataFrame``. diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 6e35e730d676e..a288c264edd81 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5362,9 +5362,13 @@ def resample(self, rule, how=None, axis=0, fill_method=None, closed=None, the offset string or object representing target conversion axis : int, optional, default 0 closed : {'right', 'left'} - Which side of bin interval is closed + Which side of bin interval is closed. The default is 'left' + for all frequency offsets except for 'M', 'A', 'Q', 'BM', + 'BA', 'BQ', and 'W' which all have a default of 'right'. label : {'right', 'left'} - Which bin edge label to label bucket with + Which bin edge label to label bucket with. The default is 'left' + for all frequency offsets except for 'M', 'A', 'Q', 'BM', + 'BA', 'BQ', and 'W' which all have a default of 'right'. convention : {'start', 'end', 's', 'e'} For PeriodIndex only, controls whether to use the start or end of `rule` @@ -5424,7 +5428,7 @@ def resample(self, rule, how=None, axis=0, fill_method=None, closed=None, value in the bucket used as the label is not included in the bucket, which it labels. For example, in the original series the bucket ``2000-01-01 00:03:00`` contains the value 3, but the summed - value in the resampled bucket with the label``2000-01-01 00:03:00`` + value in the resampled bucket with the label ``2000-01-01 00:03:00`` does not include 3 (if it did, the summed value would be 6, not 3). To include this value close the right side of the bin interval as illustrated in the example below this one. From 520ad6dea6010fffca8d8c9e31a6ce8fe6481af1 Mon Sep 17 00:00:00 2001 From: Paul Reidy Date: Sun, 22 Oct 2017 17:50:13 +0100 Subject: [PATCH 2/2] Changing example to a note:: --- doc/source/timeseries.rst | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/doc/source/timeseries.rst b/doc/source/timeseries.rst index 54a31fe79b61a..fbb2947d8c957 100644 --- a/doc/source/timeseries.rst +++ b/doc/source/timeseries.rst @@ -1491,22 +1491,24 @@ labels. ts.resample('5Min', label='left', loffset='1s').mean() -It is important to note that the default values for ``label`` and ``closed`` -is 'left' for all frequency offsets except for 'M', 'A', 'Q', 'BM', -'BA', 'BQ', and 'W' which all have a default of 'right'. +.. note:: -.. ipython:: python + The default values for ``label`` and ``closed`` is 'left' for all + frequency offsets except for 'M', 'A', 'Q', 'BM', 'BA', 'BQ', and 'W' + which all have a default of 'right'. + + .. ipython:: python - rng2 = pd.date_range('1/1/2012', end='3/31/2012', freq='D') - ts2 = pd.Series(range(len(rng2)), index=rng2) + rng2 = pd.date_range('1/1/2012', end='3/31/2012', freq='D') + ts2 = pd.Series(range(len(rng2)), index=rng2) - # default: label='right', closed='right' - ts2.resample('M').max() + # default: label='right', closed='right' + ts2.resample('M').max() - # default: label='left', closed='left' - ts2.resample('SM').max() + # default: label='left', closed='left' + ts2.resample('SM').max() - ts2.resample('SM', label='right', closed='right').max() + ts2.resample('SM', label='right', closed='right').max() The ``axis`` parameter can be set to 0 or 1 and allows you to resample the specified axis for a ``DataFrame``.