Skip to content

Commit 4c9ae94

Browse files
chris-b1jreback
authored andcommitted
DOC: resample warnings
closes #13618 closes #13520 Author: Chris <[email protected]> Closes #13675 from chris-b1/resample-warning and squashes the following commits: 2185c1f [Chris] whatsnew note c58c70c [Chris] DOC: resample warnings
1 parent 31c2e5f commit 4c9ae94

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

doc/source/whatsnew/v0.19.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ Bug Fixes
639639
- Bug in ``pd.to_datetime()`` when passing invalid datatypes (e.g. bool); will now respect the ``errors`` keyword (:issue:`13176`)
640640
- Bug in ``pd.to_datetime()`` which overflowed on ``int8``, and ``int16`` dtypes (:issue:`13451`)
641641
- Bug in extension dtype creation where the created types were not is/identical (:issue:`13285`)
642-
642+
- Bug in ``.resample(..)`` where incorrect warnings were triggered by IPython introspection (:issue:`13618`)
643643
- Bug in ``NaT`` - ``Period`` raises ``AttributeError`` (:issue:`13071`)
644644
- Bug in ``Series`` comparison may output incorrect result if rhs contains ``NaT`` (:issue:`9005`)
645645
- Bug in ``Series`` and ``Index`` comparison may output incorrect result if it contains ``NaT`` with ``object`` dtype (:issue:`13592`)

pandas/tseries/resample.py

+31-21
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ 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+
69+
# don't raise deprecation warning on attributes starting with these
70+
# patterns - prevents warnings caused by IPython introspection
71+
_deprecated_valid_patterns = ['_ipython', '_repr']
6972

7073
# API compat of disallowed attributes
7174
_deprecated_invalids = ['iloc', 'loc', 'ix', 'iat', 'at']
@@ -109,30 +112,33 @@ def _typ(self):
109112
return 'series'
110113
return 'dataframe'
111114

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

118124
def _make_deprecated_binop(op):
119125
# op is a string
120126

121127
def _evaluate_numeric_binop(self, other):
122-
result = self._deprecated()
128+
result = self._deprecated(op)
123129
return getattr(result, op)(other)
124130
return _evaluate_numeric_binop
125131

126-
def _make_deprecated_unary(op):
132+
def _make_deprecated_unary(op, name):
127133
# op is a callable
128134

129135
def _evaluate_numeric_unary(self):
130-
result = self._deprecated()
136+
result = self._deprecated(name)
131137
return op(result)
132138
return _evaluate_numeric_unary
133139

134140
def __array__(self):
135-
return self._deprecated().__array__()
141+
return self._deprecated('__array__').__array__()
136142

137143
__gt__ = _make_deprecated_binop('__gt__')
138144
__ge__ = _make_deprecated_binop('__ge__')
@@ -148,10 +154,10 @@ def __array__(self):
148154
__truediv__ = __rtruediv__ = _make_deprecated_binop('__truediv__')
149155
if not compat.PY3:
150156
__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)
157+
__neg__ = _make_deprecated_unary(lambda x: -x, '__neg__')
158+
__pos__ = _make_deprecated_unary(lambda x: x, '__pos__')
159+
__abs__ = _make_deprecated_unary(lambda x: np.abs(x), '__abs__')
160+
__inv__ = _make_deprecated_unary(lambda x: -x, '__inv__')
155161

156162
def __getattr__(self, attr):
157163
if attr in self._internal_names_set:
@@ -165,8 +171,12 @@ def __getattr__(self, attr):
165171
raise ValueError(".resample() is now a deferred operation\n"
166172
"\tuse .resample(...).mean() instead of "
167173
".resample(...)")
168-
if attr not in self._deprecated_valids:
169-
self = self._deprecated()
174+
175+
matches_pattern = any(attr.startswith(x) for x
176+
in self._deprecated_valid_patterns)
177+
if not matches_pattern and attr not in self._deprecated_valids:
178+
self = self._deprecated(attr)
179+
170180
return object.__getattribute__(self, attr)
171181

172182
def __setattr__(self, attr, value):
@@ -182,7 +192,7 @@ def __getitem__(self, key):
182192

183193
# compat for deprecated
184194
if isinstance(self.obj, com.ABCSeries):
185-
return self._deprecated()[key]
195+
return self._deprecated('__getitem__')[key]
186196

187197
raise
188198

@@ -230,7 +240,7 @@ def _assure_grouper(self):
230240
def plot(self, *args, **kwargs):
231241
# for compat with prior versions, we want to
232242
# have the warnings shown here and just have this work
233-
return self._deprecated().plot(*args, **kwargs)
243+
return self._deprecated('plot').plot(*args, **kwargs)
234244

235245
def aggregate(self, arg, *args, **kwargs):
236246
"""

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)