Skip to content

Commit 7ee0272

Browse files
committed
Add more examples to Series.resample
Add more resample examples Improve Series examples Series resample fixes Fixed breaking docstring issue
1 parent 5a9a9da commit 7ee0272

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
@@ -3274,7 +3274,107 @@ def resample(self, rule, how=None, axis=0, fill_method=None,
32743274
For frequencies that evenly subdivide 1 day, the "origin" of the
32753275
aggregated intervals. For example, for '5min' frequency, base could
32763276
range from 0 through 4. Defaults to 0
3277+
3278+
3279+
Examples
3280+
--------
3281+
3282+
Start by creating a series with 9 one minute timestamps.
3283+
3284+
>>> index = pd.date_range('1/1/2000', periods=9, freq='T')
3285+
>>> series = pd.Series(range(9), index=index)
3286+
>>> series
3287+
2000-01-01 00:00:00 0
3288+
2000-01-01 00:01:00 1
3289+
2000-01-01 00:02:00 2
3290+
2000-01-01 00:03:00 3
3291+
2000-01-01 00:04:00 4
3292+
2000-01-01 00:05:00 5
3293+
2000-01-01 00:06:00 6
3294+
2000-01-01 00:07:00 7
3295+
2000-01-01 00:08:00 8
3296+
Freq: T, dtype: int64
3297+
3298+
Downsample the series into 3 minute bins and sum the values
3299+
of the timestamps falling into a bin.
3300+
3301+
>>> series.resample('3T', how='sum')
3302+
2000-01-01 00:00:00 3
3303+
2000-01-01 00:03:00 12
3304+
2000-01-01 00:06:00 21
3305+
Freq: 3T, dtype: int64
3306+
3307+
Downsample the series into 3 minute bins as above, but label each
3308+
bin using the right edge instead of the left. Please note that the
3309+
value in the bucket used as the label is not included in the bucket,
3310+
which it labels. For example, in the original series the
3311+
bucket ``2000-01-01 00:03:00`` contains the value 3, but the summed
3312+
value in the resampled bucket with the label``2000-01-01 00:03:00``
3313+
does not include 3 (if it did, the summed value would be 6, not 3).
3314+
To include this value close the right side of the bin interval as
3315+
illustrated in the example below this one.
3316+
3317+
>>> series.resample('3T', how='sum', label='right')
3318+
2000-01-01 00:03:00 3
3319+
2000-01-01 00:06:00 12
3320+
2000-01-01 00:09:00 21
3321+
Freq: 3T, dtype: int64
3322+
3323+
Downsample the series into 3 minute bins as above, but close the right
3324+
side of the bin interval.
3325+
3326+
>>> series.resample('3T', how='sum', label='right', closed='right')
3327+
2000-01-01 00:00:00 0
3328+
2000-01-01 00:03:00 6
3329+
2000-01-01 00:06:00 15
3330+
2000-01-01 00:09:00 15
3331+
Freq: 3T, dtype: int64
3332+
3333+
Upsample the series into 30 second bins.
3334+
3335+
>>> series.resample('30S')[0:5] #select first 5 rows
3336+
2000-01-01 00:00:00 0
3337+
2000-01-01 00:00:30 NaN
3338+
2000-01-01 00:01:00 1
3339+
2000-01-01 00:01:30 NaN
3340+
2000-01-01 00:02:00 2
3341+
Freq: 30S, dtype: float64
3342+
3343+
Upsample the series into 30 second bins and fill the ``NaN``
3344+
values using the ``pad`` method.
3345+
3346+
>>> series.resample('30S', fill_method='pad')[0:5]
3347+
2000-01-01 00:00:00 0
3348+
2000-01-01 00:00:30 0
3349+
2000-01-01 00:01:00 1
3350+
2000-01-01 00:01:30 1
3351+
2000-01-01 00:02:00 2
3352+
Freq: 30S, dtype: int64
3353+
3354+
Upsample the series into 30 second bins and fill the
3355+
``NaN`` values using the ``bfill`` method.
3356+
3357+
>>> series.resample('30S', fill_method='bfill')[0:5]
3358+
2000-01-01 00:00:00 0
3359+
2000-01-01 00:00:30 1
3360+
2000-01-01 00:01:00 1
3361+
2000-01-01 00:01:30 2
3362+
2000-01-01 00:02:00 2
3363+
Freq: 30S, dtype: int64
3364+
3365+
Pass a custom function to ``how``.
3366+
3367+
>>> def custom_resampler(array_like):
3368+
... return np.sum(array_like)+5
3369+
3370+
>>> series.resample('3T', how=custom_resampler)
3371+
2000-01-01 00:00:00 8
3372+
2000-01-01 00:03:00 17
3373+
2000-01-01 00:06:00 26
3374+
Freq: 3T, dtype: int64
3375+
32773376
"""
3377+
32783378
from pandas.tseries.resample import TimeGrouper
32793379
axis = self._get_axis_number(axis)
32803380
sampler = TimeGrouper(rule, label=label, closed=closed, how=how,

0 commit comments

Comments
 (0)