-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API: Expanded resample #13961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API: Expanded resample #13961
Changes from 9 commits
def74de
c4db0e7
b55309a
7f9add4
5fd97d9
c7b299e
384026b
e203fcf
10c7280
b8dd114
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4038,10 +4038,12 @@ def between_time(self, start_time, end_time, include_start=True, | |
|
||
def resample(self, rule, how=None, axis=0, fill_method=None, closed=None, | ||
label=None, convention='start', kind=None, loffset=None, | ||
limit=None, base=0): | ||
limit=None, base=0, on=None, level=None): | ||
""" | ||
Convenience method for frequency conversion and resampling of regular | ||
time-series data. | ||
Convenience method for frequency conversion and resampling of time | ||
series. Object must have a datetime-like index (DatetimeIndex, | ||
PeriodIndex, or TimedeltaIndex), or pass datetime-like values | ||
to the on or level keyword. | ||
|
||
Parameters | ||
---------- | ||
|
@@ -4059,7 +4061,17 @@ def resample(self, rule, how=None, axis=0, fill_method=None, closed=None, | |
For frequencies that evenly subdivide 1 day, the "origin" of the | ||
aggregated intervals. For example, for '5min' frequency, base could | ||
range from 0 through 4. Defaults to 0 | ||
on : string, optional | ||
For a DataFrame, column to use instead of index for resampling. | ||
Column must be datetime-like. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add versionadded tags |
||
|
||
.. versionadded:: 0.19.0 | ||
|
||
level : string or int, optional | ||
For a MultiIndex, level (name or number) to use for | ||
resampling. Level must be datetime-like. | ||
|
||
.. versionadded:: 0.19.0 | ||
|
||
To learn more about the offset strings, please see `this link | ||
<http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases>`__. | ||
|
@@ -4164,12 +4176,11 @@ def resample(self, rule, how=None, axis=0, fill_method=None, closed=None, | |
""" | ||
from pandas.tseries.resample import (resample, | ||
_maybe_process_deprecations) | ||
|
||
axis = self._get_axis_number(axis) | ||
r = resample(self, freq=rule, label=label, closed=closed, | ||
axis=axis, kind=kind, loffset=loffset, | ||
convention=convention, | ||
base=base) | ||
base=base, key=on, level=level) | ||
return _maybe_process_deprecations(r, | ||
how=how, | ||
fill_method=fill_method, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -255,7 +255,8 @@ def _set_grouper(self, obj, sort=False): | |
Parameters | ||
---------- | ||
obj : the subject object | ||
|
||
sort : bool, default False | ||
whether the resulting grouper should be sorted | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was missing I guess? |
||
""" | ||
|
||
if self.key is not None and self.level is not None: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,7 @@ class Resampler(_GroupBy): | |
'binner', 'grouper', 'groupby', | ||
'sort', 'kind', 'squeeze', 'keys', | ||
'group_keys', 'as_index', 'exclusions', | ||
'_groupby'] | ||
'_groupby', '_from_selection'] | ||
|
||
# don't raise deprecation warning on attributes starting with these | ||
# patterns - prevents warnings caused by IPython introspection | ||
|
@@ -85,8 +85,14 @@ def __init__(self, obj, groupby=None, axis=0, kind=None, **kwargs): | |
self.exclusions = set() | ||
self.binner = None | ||
self.grouper = None | ||
self._from_selection = False | ||
|
||
if self.groupby is not None: | ||
# upsampling and PeriodIndex resampling do not work | ||
# if resampling on a column or mi level | ||
# this state used to catch and raise an error | ||
self._from_selection = (self.groupby.key is not None or | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant make this a property
|
||
self.groupby.level is not None) | ||
self.groupby._set_grouper(self._convert_obj(obj), sort=True) | ||
|
||
def __unicode__(self): | ||
|
@@ -207,6 +213,10 @@ def _convert_obj(self, obj): | |
Parameters | ||
---------- | ||
obj : the object to be resampled | ||
|
||
Returns | ||
------- | ||
obj : converted object | ||
""" | ||
obj = obj.consolidate() | ||
return obj | ||
|
@@ -706,6 +716,11 @@ def _upsample(self, method, limit=None): | |
self._set_binner() | ||
if self.axis: | ||
raise AssertionError('axis must be 0') | ||
if self._from_selection: | ||
raise ValueError("Upsampling from level= or on= selection" | ||
" is not supported, use .set_index(...)" | ||
" to explicitly set index to" | ||
" datetime-like") | ||
|
||
ax = self.ax | ||
obj = self._selected_obj | ||
|
@@ -763,7 +778,15 @@ def _convert_obj(self, obj): | |
|
||
# convert to timestamp | ||
if not (self.kind is None or self.kind == 'period'): | ||
obj = obj.to_timestamp(how=self.convention) | ||
if self._from_selection: | ||
# see GH 14008, GH 12871 | ||
msg = ("Resampling from level= or on= selection" | ||
" with a PeriodIndex is not currently supported," | ||
" use .set_index(...) to explicitly set index") | ||
raise NotImplementedError(msg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is fine (as at some point can implement) |
||
else: | ||
obj = obj.to_timestamp(how=self.convention) | ||
|
||
return obj | ||
|
||
def aggregate(self, arg, *args, **kwargs): | ||
|
@@ -840,6 +863,11 @@ def _upsample(self, method, limit=None): | |
.fillna | ||
|
||
""" | ||
if self._from_selection: | ||
raise ValueError("Upsampling from level= or on= selection" | ||
" is not supported, use .set_index(...)" | ||
" to explicitly set index to" | ||
" datetime-like") | ||
# we may need to actually resample as if we are timestamps | ||
if self.kind == 'timestamp': | ||
return super(PeriodIndexResampler, self)._upsample(method, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would add to the main docs a similar example