Skip to content

Commit 30f672c

Browse files
committed
Merge pull request #10628 from Winterflower/pandas-doc-5023
DOC Add more examples to Series.Resample docstring
2 parents 01ff345 + 7ee0272 commit 30f672c

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

pandas/core/generic.py

+100
Original file line numberDiff line numberDiff line change
@@ -3376,7 +3376,107 @@ def resample(self, rule, how=None, axis=0, fill_method=None,
33763376
For frequencies that evenly subdivide 1 day, the "origin" of the
33773377
aggregated intervals. For example, for '5min' frequency, base could
33783378
range from 0 through 4. Defaults to 0
3379+
3380+
3381+
Examples
3382+
--------
3383+
3384+
Start by creating a series with 9 one minute timestamps.
3385+
3386+
>>> index = pd.date_range('1/1/2000', periods=9, freq='T')
3387+
>>> series = pd.Series(range(9), index=index)
3388+
>>> series
3389+
2000-01-01 00:00:00 0
3390+
2000-01-01 00:01:00 1
3391+
2000-01-01 00:02:00 2
3392+
2000-01-01 00:03:00 3
3393+
2000-01-01 00:04:00 4
3394+
2000-01-01 00:05:00 5
3395+
2000-01-01 00:06:00 6
3396+
2000-01-01 00:07:00 7
3397+
2000-01-01 00:08:00 8
3398+
Freq: T, dtype: int64
3399+
3400+
Downsample the series into 3 minute bins and sum the values
3401+
of the timestamps falling into a bin.
3402+
3403+
>>> series.resample('3T', how='sum')
3404+
2000-01-01 00:00:00 3
3405+
2000-01-01 00:03:00 12
3406+
2000-01-01 00:06:00 21
3407+
Freq: 3T, dtype: int64
3408+
3409+
Downsample the series into 3 minute bins as above, but label each
3410+
bin using the right edge instead of the left. Please note that the
3411+
value in the bucket used as the label is not included in the bucket,
3412+
which it labels. For example, in the original series the
3413+
bucket ``2000-01-01 00:03:00`` contains the value 3, but the summed
3414+
value in the resampled bucket with the label``2000-01-01 00:03:00``
3415+
does not include 3 (if it did, the summed value would be 6, not 3).
3416+
To include this value close the right side of the bin interval as
3417+
illustrated in the example below this one.
3418+
3419+
>>> series.resample('3T', how='sum', label='right')
3420+
2000-01-01 00:03:00 3
3421+
2000-01-01 00:06:00 12
3422+
2000-01-01 00:09:00 21
3423+
Freq: 3T, dtype: int64
3424+
3425+
Downsample the series into 3 minute bins as above, but close the right
3426+
side of the bin interval.
3427+
3428+
>>> series.resample('3T', how='sum', label='right', closed='right')
3429+
2000-01-01 00:00:00 0
3430+
2000-01-01 00:03:00 6
3431+
2000-01-01 00:06:00 15
3432+
2000-01-01 00:09:00 15
3433+
Freq: 3T, dtype: int64
3434+
3435+
Upsample the series into 30 second bins.
3436+
3437+
>>> series.resample('30S')[0:5] #select first 5 rows
3438+
2000-01-01 00:00:00 0
3439+
2000-01-01 00:00:30 NaN
3440+
2000-01-01 00:01:00 1
3441+
2000-01-01 00:01:30 NaN
3442+
2000-01-01 00:02:00 2
3443+
Freq: 30S, dtype: float64
3444+
3445+
Upsample the series into 30 second bins and fill the ``NaN``
3446+
values using the ``pad`` method.
3447+
3448+
>>> series.resample('30S', fill_method='pad')[0:5]
3449+
2000-01-01 00:00:00 0
3450+
2000-01-01 00:00:30 0
3451+
2000-01-01 00:01:00 1
3452+
2000-01-01 00:01:30 1
3453+
2000-01-01 00:02:00 2
3454+
Freq: 30S, dtype: int64
3455+
3456+
Upsample the series into 30 second bins and fill the
3457+
``NaN`` values using the ``bfill`` method.
3458+
3459+
>>> series.resample('30S', fill_method='bfill')[0:5]
3460+
2000-01-01 00:00:00 0
3461+
2000-01-01 00:00:30 1
3462+
2000-01-01 00:01:00 1
3463+
2000-01-01 00:01:30 2
3464+
2000-01-01 00:02:00 2
3465+
Freq: 30S, dtype: int64
3466+
3467+
Pass a custom function to ``how``.
3468+
3469+
>>> def custom_resampler(array_like):
3470+
... return np.sum(array_like)+5
3471+
3472+
>>> series.resample('3T', how=custom_resampler)
3473+
2000-01-01 00:00:00 8
3474+
2000-01-01 00:03:00 17
3475+
2000-01-01 00:06:00 26
3476+
Freq: 3T, dtype: int64
3477+
33793478
"""
3479+
33803480
from pandas.tseries.resample import TimeGrouper
33813481
axis = self._get_axis_number(axis)
33823482
sampler = TimeGrouper(rule, label=label, closed=closed, how=how,

0 commit comments

Comments
 (0)