Skip to content

Commit

Permalink
DOC: resample warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-b1 committed Jul 19, 2016
1 parent 506520b commit c58c70c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
51 changes: 30 additions & 21 deletions pandas/tseries/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ class Resampler(_GroupBy):
'loffset', 'base', 'kind']

# API compat of allowed attributes
_deprecated_valids = _attributes + ['_ipython_display_', '__doc__',
'_cache', '_attributes', 'binner',
'grouper', 'groupby', 'keys',
'sort', 'kind', 'squeeze',
'group_keys', 'as_index',
'exclusions', '_groupby']
_deprecated_valids = _attributes + ['__doc__', '_cache', '_attributes',
'binner', 'grouper', 'groupby',
'sort', 'kind', 'squeeze', 'keys',
'group_keys', 'as_index', 'exclusions',
'_groupby']
# don't raise deprecation warning on attributes starting with these
# patterns - prevents warnings caused by IPython introspection
_deprecated_valid_patterns = ['_ipython', '_repr']

# API compat of disallowed attributes
_deprecated_invalids = ['iloc', 'loc', 'ix', 'iat', 'at']
Expand Down Expand Up @@ -109,30 +111,33 @@ def _typ(self):
return 'series'
return 'dataframe'

def _deprecated(self):
warnings.warn(".resample() is now a deferred operation\n"
"use .resample(...).mean() instead of .resample(...)",
def _deprecated(self, op):
warnings.warn(("\n.resample() is now a deferred operation\n"
"You called {op}(...) on this deferred object "
"which materialized it into a {klass}\nby implicitly "
"taking the mean. Use .resample(...).mean() "
"instead").format(op=op, klass=self._typ),
FutureWarning, stacklevel=3)
return self.mean()

def _make_deprecated_binop(op):
# op is a string

def _evaluate_numeric_binop(self, other):
result = self._deprecated()
result = self._deprecated(op)
return getattr(result, op)(other)
return _evaluate_numeric_binop

def _make_deprecated_unary(op):
def _make_deprecated_unary(op, name):
# op is a callable

def _evaluate_numeric_unary(self):
result = self._deprecated()
result = self._deprecated(name)
return op(result)
return _evaluate_numeric_unary

def __array__(self):
return self._deprecated().__array__()
return self._deprecated('__array__').__array__()

__gt__ = _make_deprecated_binop('__gt__')
__ge__ = _make_deprecated_binop('__ge__')
Expand All @@ -148,10 +153,10 @@ def __array__(self):
__truediv__ = __rtruediv__ = _make_deprecated_binop('__truediv__')
if not compat.PY3:
__div__ = __rdiv__ = _make_deprecated_binop('__div__')
__neg__ = _make_deprecated_unary(lambda x: -x)
__pos__ = _make_deprecated_unary(lambda x: x)
__abs__ = _make_deprecated_unary(lambda x: np.abs(x))
__inv__ = _make_deprecated_unary(lambda x: -x)
__neg__ = _make_deprecated_unary(lambda x: -x, '__neg__')
__pos__ = _make_deprecated_unary(lambda x: x, '__pos__')
__abs__ = _make_deprecated_unary(lambda x: np.abs(x), '__abs__')
__inv__ = _make_deprecated_unary(lambda x: -x, '__inv__')

def __getattr__(self, attr):
if attr in self._internal_names_set:
Expand All @@ -165,8 +170,12 @@ def __getattr__(self, attr):
raise ValueError(".resample() is now a deferred operation\n"
"\tuse .resample(...).mean() instead of "
".resample(...)")
if attr not in self._deprecated_valids:
self = self._deprecated()

matches_pattern = any(attr.startswith(x) for x
in self._deprecated_valid_patterns)
if not matches_pattern and attr not in self._deprecated_valids:
self = self._deprecated(attr)

return object.__getattribute__(self, attr)

def __setattr__(self, attr, value):
Expand All @@ -182,7 +191,7 @@ def __getitem__(self, key):

# compat for deprecated
if isinstance(self.obj, com.ABCSeries):
return self._deprecated()[key]
return self._deprecated('__getitem__')[key]

raise

Expand Down Expand Up @@ -230,7 +239,7 @@ def _assure_grouper(self):
def plot(self, *args, **kwargs):
# for compat with prior versions, we want to
# have the warnings shown here and just have this work
return self._deprecated().plot(*args, **kwargs)
return self._deprecated('plot').plot(*args, **kwargs)

def aggregate(self, arg, *args, **kwargs):
"""
Expand Down
7 changes: 7 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ def f():
check_stacklevel=False):
self.assertIsInstance(getattr(r, op)(2), pd.Series)

# IPython introspection shouldn't trigger warning GH 13618
for op in ['_repr_json', '_repr_latex',
'_ipython_canary_method_should_not_exist_']:
r = self.series.resample('H')
with tm.assert_produces_warning(None):
getattr(r, op, None)

# getitem compat
df = self.series.to_frame('foo')

Expand Down

0 comments on commit c58c70c

Please sign in to comment.