Skip to content

ENH: Use find_stack_level #44416

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

Merged
merged 1 commit into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from pandas._libs.missing import is_matching_na
import pandas._libs.testing as _testing
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import (
is_bool,
Expand Down Expand Up @@ -106,7 +107,7 @@ def assert_almost_equal(
"is deprecated and will be removed in a future version. "
"You can stop passing 'check_less_precise' to silence this warning.",
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(),
)
# https://github.com/python/mypy/issues/7642
# error: Argument 1 to "_get_tol_from_less_precise" has incompatible
Expand Down Expand Up @@ -340,7 +341,7 @@ def _get_ilevel_values(index, level):
"is deprecated and will be removed in a future version. "
"You can stop passing 'check_less_precise' to silence this warning.",
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(),
)
# https://github.com/python/mypy/issues/7642
# error: Argument 1 to "_get_tol_from_less_precise" has incompatible
Expand Down Expand Up @@ -818,7 +819,7 @@ def assert_extension_array_equal(
"is deprecated and will be removed in a future version. "
"You can stop passing 'check_less_precise' to silence this warning.",
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(),
)
rtol = atol = _get_tol_from_less_precise(check_less_precise)

Expand Down Expand Up @@ -964,7 +965,7 @@ def assert_series_equal(
"is deprecated and will be removed in a future version. "
"You can stop passing 'check_less_precise' to silence this warning.",
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(),
)
rtol = atol = _get_tol_from_less_precise(check_less_precise)

Expand Down Expand Up @@ -1247,7 +1248,7 @@ def assert_frame_equal(
"is deprecated and will be removed in a future version. "
"You can stop passing 'check_less_precise' to silence this warning.",
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(),
)
rtol = atol = _get_tol_from_less_precise(check_less_precise)

Expand Down
3 changes: 2 additions & 1 deletion pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import_lzma,
)
from pandas.compat._optional import import_optional_dependency
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import is_file_like

Expand Down Expand Up @@ -270,7 +271,7 @@ def _get_filepath_or_buffer(
warnings.warn(
"compression has no effect when passing a non-binary object as input.",
RuntimeWarning,
stacklevel=2,
stacklevel=find_stack_level(),
)
compression_method = None

Expand Down
9 changes: 5 additions & 4 deletions pandas/io/date_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np

from pandas._libs.tslibs import parsing
from pandas.util._exceptions import find_stack_level


def parse_date_time(date_col, time_col):
Expand All @@ -18,7 +19,7 @@ def parse_date_time(date_col, time_col):
Use pd.to_datetime(date_col + " " + time_col).to_pydatetime() instead to get a Numpy array.
""", # noqa: E501
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(),
)
date_col = _maybe_cast(date_col)
time_col = _maybe_cast(time_col)
Expand All @@ -38,7 +39,7 @@ def parse_date_fields(year_col, month_col, day_col):
np.array([s.to_pydatetime() for s in ser]) instead to get a Numpy array.
""", # noqa: E501
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(),
)

year_col = _maybe_cast(year_col)
Expand All @@ -63,7 +64,7 @@ def parse_all_fields(year_col, month_col, day_col, hour_col, minute_col, second_
np.array([s.to_pydatetime() for s in ser]) instead to get a Numpy array.
""", # noqa: E501
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(),
)

year_col = _maybe_cast(year_col)
Expand All @@ -89,7 +90,7 @@ def generic_parser(parse_func, *cols):
Use pd.to_datetime instead.
""",
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(),
)

N = _check_columns(cols)
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ def __new__(
warnings.warn(
"Use of **kwargs is deprecated, use engine_kwargs instead.",
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(),
)

# only switch class if generic(ExcelWriter)
Expand Down Expand Up @@ -868,7 +868,7 @@ def __new__(
"deprecated and will also raise a warning, it can "
"be globally set and the warning suppressed.",
FutureWarning,
stacklevel=4,
stacklevel=find_stack_level(),
)

cls = get_writer(engine)
Expand Down
5 changes: 3 additions & 2 deletions pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
ParserError,
ParserWarning,
)
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.cast import astype_nansafe
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -558,7 +559,7 @@ def _convert_to_ndarrays(
f"for column {c} - only the converter will be used."
),
ParserWarning,
stacklevel=7,
stacklevel=find_stack_level(),
)

try:
Expand Down Expand Up @@ -830,7 +831,7 @@ def _check_data_length(self, columns: list[str], data: list[ArrayLike]) -> None:
"Length of header or names does not match length of data. This leads "
"to a loss of data with index_col=False.",
ParserWarning,
stacklevel=6,
stacklevel=find_stack_level(),
)

def _evaluate_usecols(self, usecols, names):
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/parsers/c_parser_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
FilePathOrBuffer,
)
from pandas.errors import DtypeWarning
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import (
is_categorical_dtype,
Expand Down Expand Up @@ -387,7 +388,7 @@ def _concatenate_chunks(chunks: list[dict[int, ArrayLike]]) -> dict:
f"Specify dtype option on import or set low_memory=False."
]
)
warnings.warn(warning_message, DtypeWarning, stacklevel=8)
warnings.warn(warning_message, DtypeWarning, stacklevel=find_stack_level())
return result


Expand Down
3 changes: 2 additions & 1 deletion pandas/io/parsers/python_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
EmptyDataError,
ParserError,
)
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import is_integer
from pandas.core.dtypes.inference import is_dict_like
Expand Down Expand Up @@ -555,7 +556,7 @@ def _handle_usecols(
"Defining usecols with out of bounds indices is deprecated "
"and will raise a ParserError in a future version.",
FutureWarning,
stacklevel=8,
stacklevel=find_stack_level(),
)
col_indices = self.usecols

Expand Down
6 changes: 4 additions & 2 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ def _clean_options(self, options, engine):
"engine='python'."
),
ParserWarning,
stacklevel=5,
stacklevel=find_stack_level(),
)

index_col = options["index_col"]
Expand Down Expand Up @@ -1573,7 +1573,9 @@ def _merge_with_dialect_properties(
conflict_msgs.append(msg)

if conflict_msgs:
warnings.warn("\n\n".join(conflict_msgs), ParserWarning, stacklevel=2)
warnings.warn(
"\n\n".join(conflict_msgs), ParserWarning, stacklevel=find_stack_level()
)
kwds[param] = dialect_val
return kwds

Expand Down
7 changes: 5 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from pandas.compat.pickle_compat import patch_pickle
from pandas.errors import PerformanceWarning
from pandas.util._decorators import cache_readonly
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import (
ensure_object,
Expand Down Expand Up @@ -2190,7 +2191,9 @@ def update_info(self, info):
# frequency/name just warn
if key in ["freq", "index_name"]:
ws = attribute_conflict_doc % (key, existing_value, value)
warnings.warn(ws, AttributeConflictWarning, stacklevel=6)
warnings.warn(
ws, AttributeConflictWarning, stacklevel=find_stack_level()
)

# reset
idx[key] = None
Expand Down Expand Up @@ -3080,7 +3083,7 @@ def write_array(
pass
else:
ws = performance_doc % (inferred_type, key, items)
warnings.warn(ws, PerformanceWarning, stacklevel=7)
warnings.warn(ws, PerformanceWarning, stacklevel=find_stack_level())

vlarr = self._handle.create_vlarray(self.group, key, _tables().ObjectAtom())
vlarr.append(value)
Expand Down
7 changes: 4 additions & 3 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from pandas._typing import DtypeArg
from pandas.compat._optional import import_optional_dependency
from pandas.errors import AbstractMethodError
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import (
is_datetime64tz_dtype,
Expand Down Expand Up @@ -1159,7 +1160,7 @@ def _sqlalchemy_type(self, col):
"the 'timedelta' type is not supported, and will be "
"written as integer values (ns frequency) to the database.",
UserWarning,
stacklevel=8,
stacklevel=find_stack_level(),
)
return BigInteger
elif col_type == "floating":
Expand Down Expand Up @@ -1886,7 +1887,7 @@ def _create_table_setup(self):
pat = re.compile(r"\s+")
column_names = [col_name for col_name, _, _ in column_names_and_types]
if any(map(pat.search, column_names)):
warnings.warn(_SAFE_NAMES_WARNING, stacklevel=6)
warnings.warn(_SAFE_NAMES_WARNING, stacklevel=find_stack_level())

escape = _get_valid_sqlite_name

Expand Down Expand Up @@ -1948,7 +1949,7 @@ def _sql_type_name(self, col):
"the 'timedelta' type is not supported, and will be "
"written as integer values (ns frequency) to the database.",
UserWarning,
stacklevel=8,
stacklevel=find_stack_level(),
)
col_type = "integer"

Expand Down
6 changes: 4 additions & 2 deletions pandas/plotting/_matplotlib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import matplotlib.ticker as ticker
import numpy as np

from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import is_list_like
from pandas.core.dtypes.generic import (
ABCDataFrame,
Expand Down Expand Up @@ -233,7 +235,7 @@ def create_subplots(
"When passing multiple axes, sharex and sharey "
"are ignored. These settings must be specified when creating axes.",
UserWarning,
stacklevel=4,
stacklevel=find_stack_level(),
)
if ax.size == naxes:
fig = ax.flat[0].get_figure()
Expand All @@ -256,7 +258,7 @@ def create_subplots(
"To output multiple subplots, the figure containing "
"the passed axes is being cleared.",
UserWarning,
stacklevel=4,
stacklevel=find_stack_level(),
)
fig.clear()

Expand Down
3 changes: 2 additions & 1 deletion pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from pandas._libs.tslibs.parsing import get_rule_month
from pandas._typing import npt
from pandas.util._decorators import cache_readonly
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import (
is_datetime64_dtype,
Expand Down Expand Up @@ -116,7 +117,7 @@ def get_offset(name: str) -> DateOffset:
"get_offset is deprecated and will be removed in a future version, "
"use to_offset instead.",
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(),
)
return _get_offset(name)

Expand Down
4 changes: 3 additions & 1 deletion pandas/util/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import numpy as np

from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import (
is_bool,
is_integer,
Expand Down Expand Up @@ -339,7 +341,7 @@ def validate_axis_style_args(data, args, kwargs, arg_name, method_name):
"positional arguments for 'index' or 'columns' will raise "
"a 'TypeError'."
)
warnings.warn(msg, FutureWarning, stacklevel=4)
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
out[data._get_axis_name(0)] = args[0]
out[data._get_axis_name(1)] = args[1]
else:
Expand Down
4 changes: 3 additions & 1 deletion pandas/util/testing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import warnings

from pandas.util._exceptions import find_stack_level

from pandas._testing import * # noqa

warnings.warn(
Expand All @@ -8,5 +10,5 @@
"public API at pandas.testing instead."
),
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(),
)