Skip to content

Commit c58c70c

Browse files
committed
DOC: resample warnings
1 parent 506520b commit c58c70c

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

pandas/tseries/resample.py

+30-21
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ class Resampler(_GroupBy):
6060
'loffset', 'base', 'kind']
6161

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

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

112-
def _deprecated(self):
113-
warnings.warn(".resample() is now a deferred operation\n"
114-
"use .resample(...).mean() instead of .resample(...)",
114+
def _deprecated(self, op):
115+
warnings.warn(("\n.resample() is now a deferred operation\n"
116+
"You called {op}(...) on this deferred object "
117+
"which materialized it into a {klass}\nby implicitly "
118+
"taking the mean. Use .resample(...).mean() "
119+
"instead").format(op=op, klass=self._typ),
115120
FutureWarning, stacklevel=3)
116121
return self.mean()
117122

118123
def _make_deprecated_binop(op):
119124
# op is a string
120125

121126
def _evaluate_numeric_binop(self, other):
122-
result = self._deprecated()
127+
result = self._deprecated(op)
123128
return getattr(result, op)(other)
124129
return _evaluate_numeric_binop
125130

126-
def _make_deprecated_unary(op):
131+
def _make_deprecated_unary(op, name):
127132
# op is a callable
128133

129134
def _evaluate_numeric_unary(self):
130-
result = self._deprecated()
135+
result = self._deprecated(name)
131136
return op(result)
132137
return _evaluate_numeric_unary
133138

134139
def __array__(self):
135-
return self._deprecated().__array__()
140+
return self._deprecated('__array__').__array__()
136141

137142
__gt__ = _make_deprecated_binop('__gt__')
138143
__ge__ = _make_deprecated_binop('__ge__')
@@ -148,10 +153,10 @@ def __array__(self):
148153
__truediv__ = __rtruediv__ = _make_deprecated_binop('__truediv__')
149154
if not compat.PY3:
150155
__div__ = __rdiv__ = _make_deprecated_binop('__div__')
151-
__neg__ = _make_deprecated_unary(lambda x: -x)
152-
__pos__ = _make_deprecated_unary(lambda x: x)
153-
__abs__ = _make_deprecated_unary(lambda x: np.abs(x))
154-
__inv__ = _make_deprecated_unary(lambda x: -x)
156+
__neg__ = _make_deprecated_unary(lambda x: -x, '__neg__')
157+
__pos__ = _make_deprecated_unary(lambda x: x, '__pos__')
158+
__abs__ = _make_deprecated_unary(lambda x: np.abs(x), '__abs__')
159+
__inv__ = _make_deprecated_unary(lambda x: -x, '__inv__')
155160

156161
def __getattr__(self, attr):
157162
if attr in self._internal_names_set:
@@ -165,8 +170,12 @@ def __getattr__(self, attr):
165170
raise ValueError(".resample() is now a deferred operation\n"
166171
"\tuse .resample(...).mean() instead of "
167172
".resample(...)")
168-
if attr not in self._deprecated_valids:
169-
self = self._deprecated()
173+
174+
matches_pattern = any(attr.startswith(x) for x
175+
in self._deprecated_valid_patterns)
176+
if not matches_pattern and attr not in self._deprecated_valids:
177+
self = self._deprecated(attr)
178+
170179
return object.__getattribute__(self, attr)
171180

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

183192
# compat for deprecated
184193
if isinstance(self.obj, com.ABCSeries):
185-
return self._deprecated()[key]
194+
return self._deprecated('__getitem__')[key]
186195

187196
raise
188197

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

235244
def aggregate(self, arg, *args, **kwargs):
236245
"""

pandas/tseries/tests/test_resample.py

+7
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ def f():
168168
check_stacklevel=False):
169169
self.assertIsInstance(getattr(r, op)(2), pd.Series)
170170

171+
# IPython introspection shouldn't trigger warning GH 13618
172+
for op in ['_repr_json', '_repr_latex',
173+
'_ipython_canary_method_should_not_exist_']:
174+
r = self.series.resample('H')
175+
with tm.assert_produces_warning(None):
176+
getattr(r, op, None)
177+
171178
# getitem compat
172179
df = self.series.to_frame('foo')
173180

0 commit comments

Comments
 (0)