Skip to content

Commit a928f8d

Browse files
DEPR: add stacklevel to FutureWarnings (GH9584)
1 parent 4b9606b commit a928f8d

File tree

12 files changed

+44
-38
lines changed

12 files changed

+44
-38
lines changed

pandas/core/algorithms.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ def factorize(values, sort=False, order=None, na_sentinel=-1, size_hint=None):
125125
note: an array of Periods will ignore sort as it returns an always sorted PeriodIndex
126126
"""
127127
if order is not None:
128-
warn("order is deprecated."
129-
"See https://github.com/pydata/pandas/issues/6926", FutureWarning)
128+
msg = "order is deprecated. See https://github.com/pydata/pandas/issues/6926"
129+
warn(msg, FutureWarning, stacklevel=2)
130130

131131
from pandas.core.index import Index
132132
from pandas.core.series import Series

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,7 @@ def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
14681468

14691469
if colSpace is not None: # pragma: no cover
14701470
warnings.warn("colSpace is deprecated, use col_space",
1471-
FutureWarning)
1471+
FutureWarning, stacklevel=2)
14721472
col_space = colSpace
14731473

14741474
formatter = fmt.DataFrameFormatter(self, buf=buf, columns=columns,
@@ -1517,7 +1517,7 @@ def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None,
15171517

15181518
if colSpace is not None: # pragma: no cover
15191519
warnings.warn("colSpace is deprecated, use col_space",
1520-
FutureWarning)
1520+
FutureWarning, stacklevel=2)
15211521
col_space = colSpace
15221522

15231523
formatter = fmt.DataFrameFormatter(self, buf=buf, columns=columns,

pandas/core/generic.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -3376,11 +3376,11 @@ def resample(self, rule, how=None, axis=0, fill_method=None,
33763376
For frequencies that evenly subdivide 1 day, the "origin" of the
33773377
aggregated intervals. For example, for '5min' frequency, base could
33783378
range from 0 through 4. Defaults to 0
3379-
3379+
33803380
33813381
Examples
33823382
--------
3383-
3383+
33843384
Start by creating a series with 9 one minute timestamps.
33853385
33863386
>>> index = pd.date_range('1/1/2000', periods=9, freq='T')
@@ -3409,11 +3409,11 @@ def resample(self, rule, how=None, axis=0, fill_method=None,
34093409
Downsample the series into 3 minute bins as above, but label each
34103410
bin using the right edge instead of the left. Please note that the
34113411
value in the bucket used as the label is not included in the bucket,
3412-
which it labels. For example, in the original series the
3412+
which it labels. For example, in the original series the
34133413
bucket ``2000-01-01 00:03:00`` contains the value 3, but the summed
3414-
value in the resampled bucket with the label``2000-01-01 00:03:00``
3414+
value in the resampled bucket with the label``2000-01-01 00:03:00``
34153415
does not include 3 (if it did, the summed value would be 6, not 3).
3416-
To include this value close the right side of the bin interval as
3416+
To include this value close the right side of the bin interval as
34173417
illustrated in the example below this one.
34183418
34193419
>>> series.resample('3T', how='sum', label='right')
@@ -3424,7 +3424,7 @@ def resample(self, rule, how=None, axis=0, fill_method=None,
34243424
34253425
Downsample the series into 3 minute bins as above, but close the right
34263426
side of the bin interval.
3427-
3427+
34283428
>>> series.resample('3T', how='sum', label='right', closed='right')
34293429
2000-01-01 00:00:00 0
34303430
2000-01-01 00:03:00 6
@@ -3453,7 +3453,7 @@ def resample(self, rule, how=None, axis=0, fill_method=None,
34533453
2000-01-01 00:02:00 2
34543454
Freq: 30S, dtype: int64
34553455
3456-
Upsample the series into 30 second bins and fill the
3456+
Upsample the series into 30 second bins and fill the
34573457
``NaN`` values using the ``bfill`` method.
34583458
34593459
>>> series.resample('30S', fill_method='bfill')[0:5]
@@ -3468,7 +3468,7 @@ def resample(self, rule, how=None, axis=0, fill_method=None,
34683468
34693469
>>> def custom_resampler(array_like):
34703470
... return np.sum(array_like)+5
3471-
3471+
34723472
>>> series.resample('3T', how=custom_resampler)
34733473
2000-01-01 00:00:00 8
34743474
2000-01-01 00:03:00 17

pandas/core/index.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -842,14 +842,14 @@ def to_int():
842842
elif is_float(key):
843843
key = to_int()
844844
warnings.warn("scalar indexers for index type {0} should be integers and not floating point".format(
845-
type(self).__name__),FutureWarning, stacklevel=8)
845+
type(self).__name__),FutureWarning, stacklevel=2)
846846
return key
847847
return self._invalid_indexer('label', key)
848848

849849
if is_float(key):
850850
if not self.is_floating():
851851
warnings.warn("scalar indexers for index type {0} should be integers and not floating point".format(
852-
type(self).__name__),FutureWarning, stacklevel=8)
852+
type(self).__name__),FutureWarning, stacklevel=2)
853853
return to_int()
854854

855855
return key
@@ -887,7 +887,7 @@ def f(c):
887887
# warn if it's a convertible float
888888
if v == int(v):
889889
warnings.warn("slice indexers when using iloc should be integers "
890-
"and not floating point",FutureWarning)
890+
"and not floating point",FutureWarning, stacklevel=2)
891891
return int(v)
892892

893893
self._invalid_indexer('slice {0} value'.format(c), v)
@@ -1415,22 +1415,22 @@ def argsort(self, *args, **kwargs):
14151415
def __add__(self, other):
14161416
if com.is_list_like(other):
14171417
warnings.warn("using '+' to provide set union with Indexes is deprecated, "
1418-
"use '|' or .union()", FutureWarning)
1418+
"use '|' or .union()", FutureWarning, stacklevel=2)
14191419
if isinstance(other, Index):
14201420
return self.union(other)
14211421
return Index(np.array(self) + other)
14221422

14231423
def __radd__(self, other):
14241424
if com.is_list_like(other):
14251425
warnings.warn("using '+' to provide set union with Indexes is deprecated, "
1426-
"use '|' or .union()", FutureWarning)
1426+
"use '|' or .union()", FutureWarning, stacklevel=2)
14271427
return Index(other + np.array(self))
14281428

14291429
__iadd__ = __add__
14301430

14311431
def __sub__(self, other):
14321432
warnings.warn("using '-' to provide set differences with Indexes is deprecated, "
1433-
"use .difference()",FutureWarning)
1433+
"use .difference()",FutureWarning, stacklevel=2)
14341434
return self.difference(other)
14351435

14361436
def __and__(self, other):

pandas/io/data.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ def __init__(self, symbol, data_source=None):
608608
self.symbol = symbol.upper()
609609
if data_source is None:
610610
warnings.warn("Options(symbol) is deprecated, use Options(symbol,"
611-
" data_source) instead", FutureWarning)
611+
" data_source) instead", FutureWarning, stacklevel=2)
612612
data_source = "yahoo"
613613
if data_source != "yahoo":
614614
raise NotImplementedError("currently only yahoo supported")
@@ -1072,7 +1072,8 @@ def get_forward_data(self, months, call=True, put=False, near=False,
10721072
Note: Format of returned data frame is dependent on Yahoo and may change.
10731073
10741074
"""
1075-
warnings.warn("get_forward_data() is deprecated", FutureWarning)
1075+
warnings.warn("get_forward_data() is deprecated", FutureWarning,
1076+
stacklevel=2)
10761077
end_date = dt.date.today() + MonthEnd(months)
10771078
dates = (date for date in self.expiry_dates if date <= end_date.date())
10781079
data = self._get_data_in_date_range(dates, call=call, put=put)

pandas/io/sql.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def tquery(sql, con=None, cur=None, retry=True):
200200
warnings.warn(
201201
"tquery is deprecated, and will be removed in future versions. "
202202
"You can use ``execute(...).fetchall()`` instead.",
203-
FutureWarning)
203+
FutureWarning, stacklevel=2)
204204

205205
cur = execute(sql, con, cur=cur)
206206
result = _safe_fetch(cur)
@@ -255,7 +255,7 @@ def uquery(sql, con=None, cur=None, retry=True, params=None):
255255
warnings.warn(
256256
"uquery is deprecated, and will be removed in future versions. "
257257
"You can use ``execute(...).rowcount`` instead.",
258-
FutureWarning)
258+
FutureWarning, stacklevel=2)
259259

260260
cur = execute(sql, con, cur=cur, params=params)
261261

@@ -618,7 +618,7 @@ def pandasSQL_builder(con, flavor=None, schema=None, meta=None,
618618
return SQLDatabase(con, schema=schema, meta=meta)
619619
else:
620620
if flavor == 'mysql':
621-
warnings.warn(_MYSQL_WARNING, FutureWarning)
621+
warnings.warn(_MYSQL_WARNING, FutureWarning, stacklevel=2)
622622
return SQLiteDatabase(con, flavor, is_cursor=is_cursor)
623623

624624

@@ -1672,15 +1672,17 @@ def get_schema(frame, name, flavor='sqlite', keys=None, con=None, dtype=None):
16721672
def read_frame(*args, **kwargs):
16731673
"""DEPRECATED - use read_sql
16741674
"""
1675-
warnings.warn("read_frame is deprecated, use read_sql", FutureWarning)
1675+
warnings.warn("read_frame is deprecated, use read_sql", FutureWarning,
1676+
stacklevel=2)
16761677
return read_sql(*args, **kwargs)
16771678

16781679

16791680
@Appender(read_sql.__doc__, join='\n')
16801681
def frame_query(*args, **kwargs):
16811682
"""DEPRECATED - use read_sql
16821683
"""
1683-
warnings.warn("frame_query is deprecated, use read_sql", FutureWarning)
1684+
warnings.warn("frame_query is deprecated, use read_sql", FutureWarning,
1685+
stacklevel=2)
16841686
return read_sql(*args, **kwargs)
16851687

16861688

@@ -1718,7 +1720,8 @@ def write_frame(frame, name, con, flavor='sqlite', if_exists='fail', **kwargs):
17181720
pandas.DataFrame.to_sql
17191721
17201722
"""
1721-
warnings.warn("write_frame is deprecated, use to_sql", FutureWarning)
1723+
warnings.warn("write_frame is deprecated, use to_sql", FutureWarning,
1724+
stacklevel=2)
17221725

17231726
# for backwards compatibility, set index=False when not specified
17241727
index = kwargs.pop('index', False)

pandas/rpy/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"like rpy2. "
99
"\nSee here for a guide on how to port your code to rpy2: "
1010
"http://pandas.pydata.org/pandas-docs/stable/r_interface.html",
11-
FutureWarning)
11+
FutureWarning, stacklevel=2)
1212

1313
try:
1414
from .common import importr, r, load_data

pandas/stats/moments.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
123123
When ignore_na is True (reproducing pre-0.15.0 behavior), weights are based on
124124
relative positions. For example, the weights of x and y used in calculating
125-
the final weighted average of [x, None, y] are 1-alpha and 1 (if adjust is
125+
the final weighted average of [x, None, y] are 1-alpha and 1 (if adjust is
126126
True), and 1-alpha and alpha (if adjust is False).
127127
"""
128128

@@ -344,7 +344,8 @@ def dataframe_from_int_dict(data, frame_template):
344344
def rolling_corr_pairwise(df1, df2=None, window=None, min_periods=None,
345345
freq=None, center=False):
346346
import warnings
347-
warnings.warn("rolling_corr_pairwise is deprecated, use rolling_corr(..., pairwise=True)", FutureWarning)
347+
msg = "rolling_corr_pairwise is deprecated, use rolling_corr(..., pairwise=True)"
348+
warnings.warn(msg, FutureWarning, stacklevel=2)
348349
return rolling_corr(df1, df2, window=window, min_periods=min_periods,
349350
freq=freq, center=center,
350351
pairwise=True)
@@ -399,7 +400,7 @@ def _rolling_moment(arg, window, func, minp, axis=0, freq=None, center=False,
399400

400401
if center:
401402
result = _center_window(result, window, axis)
402-
403+
403404
return return_hook(result)
404405

405406

@@ -998,7 +999,8 @@ def expanding_corr(arg1, arg2=None, min_periods=1, freq=None, pairwise=None):
998999
@Appender(_doc_template)
9991000
def expanding_corr_pairwise(df1, df2=None, min_periods=1, freq=None):
10001001
import warnings
1001-
warnings.warn("expanding_corr_pairwise is deprecated, use expanding_corr(..., pairwise=True)", FutureWarning)
1002+
msg = "expanding_corr_pairwise is deprecated, use expanding_corr(..., pairwise=True)"
1003+
warnings.warn(msg, FutureWarning, stacklevel=2)
10021004
return expanding_corr(df1, df2, min_periods=min_periods,
10031005
freq=freq, pairwise=True)
10041006

pandas/tools/plotting.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2624,7 +2624,7 @@ def plot_group(keys, values, ax):
26242624
"now, set return_type='axes'.\n To keep the previous "
26252625
"behavior and silence this warning, set "
26262626
"return_type='dict'.")
2627-
warnings.warn(msg, FutureWarning)
2627+
warnings.warn(msg, FutureWarning, stacklevel=2)
26282628
return_type = 'dict'
26292629
if ax is None:
26302630
ax = _gca()
@@ -2972,7 +2972,7 @@ def _grouped_plot(plotf, data, column=None, by=None, numeric_only=True,
29722972
if figsize == 'default':
29732973
# allowed to specify mpl default with 'default'
29742974
warnings.warn("figsize='default' is deprecated. Specify figure"
2975-
"size by tuple instead", FutureWarning)
2975+
"size by tuple instead", FutureWarning, stacklevel=2)
29762976
figsize = None
29772977

29782978
grouped = data.groupby(by)

pandas/tools/rplot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"like seaborn for similar but more refined functionality. \n\n"
1818
"See our docs http://pandas.pydata.org/pandas-docs/stable/visualization.html#rplot "
1919
"for some example how to convert your existing code to these "
20-
"packages.", FutureWarning)
20+
"packages.", FutureWarning, stacklevel=2)
2121

2222

2323
class Scale:

pandas/tseries/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def __add__(self, other):
398398
raise TypeError("cannot add TimedeltaIndex and {typ}".format(typ=type(other)))
399399
elif isinstance(other, Index):
400400
warnings.warn("using '+' to provide set union with datetimelike Indexes is deprecated, "
401-
"use .union()",FutureWarning)
401+
"use .union()",FutureWarning, stacklevel=2)
402402
return self.union(other)
403403
elif isinstance(other, (DateOffset, timedelta, np.timedelta64, tslib.Timedelta)):
404404
return self._add_delta(other)
@@ -423,7 +423,7 @@ def __sub__(self, other):
423423
return self._add_delta(-other)
424424
elif isinstance(other, Index):
425425
warnings.warn("using '-' to provide set differences with datetimelike Indexes is deprecated, "
426-
"use .difference()",FutureWarning)
426+
"use .difference()",FutureWarning, stacklevel=2)
427427
return self.difference(other)
428428
elif isinstance(other, (DateOffset, timedelta, np.timedelta64, tslib.Timedelta)):
429429
return self._add_delta(-other)

pandas/util/decorators.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def deprecate(name, alternative, alt_name=None):
1010

1111
def wrapper(*args, **kwargs):
1212
warnings.warn("%s is deprecated. Use %s instead" % (name, alt_name),
13-
FutureWarning)
13+
FutureWarning, stacklevel=2)
1414
return alternative(*args, **kwargs)
1515
return wrapper
1616

@@ -79,7 +79,7 @@ def wrapper(*args, **kwargs):
7979
msg = "the '%s' keyword is deprecated, " \
8080
"use '%s' instead" % (old_arg_name, new_arg_name)
8181

82-
warnings.warn(msg, FutureWarning)
82+
warnings.warn(msg, FutureWarning, stacklevel=2)
8383
if kwargs.get(new_arg_name, None) is not None:
8484
msg = "Can only specify '%s' or '%s', not both" % \
8585
(old_arg_name, new_arg_name)

0 commit comments

Comments
 (0)