Skip to content

MAINT: Fix for future change #409

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 7 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
65 changes: 65 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
from __future__ import annotations

from contextlib import (
AbstractContextManager,
nullcontext,
)
import os
import platform
from typing import (
TYPE_CHECKING,
Final,
)

from packaging.version import parse
import pandas as pd
import pytest

from pandas._typing import T

TYPE_CHECKING_INVALID_USAGE: Final = TYPE_CHECKING
WINDOWS = os.name == "nt" or "cygwin" in platform.system().lower()
PD_LTE_15 = parse(pd.__version__) < parse("1.5.999")


def check(actual: T, klass: type, dtype: type | None = None, attr: str = "left") -> T:
Expand All @@ -28,3 +37,59 @@ def check(actual: T, klass: type, dtype: type | None = None, attr: str = "left")
if not isinstance(value, dtype):
raise RuntimeError(f"Expected type '{dtype}' but got '{type(value)}'")
return actual # type: ignore[return-value]


def pytest_warns_bounded(
warning: type[Warning],
match: str,
lower: str | None = None,
upper: str | None = None,
) -> AbstractContextManager:
"""
Version conditional pytet.warns context manager

Returns a context manager that will raise an error if
the warning is not issued when pandas version is
between the lower and upper version given.

Parameters
----------
warning : type[Warning]
The warning class to check for.
match : str
The string to match in the warning message.
lower : str, optional
The lower bound of the version to check for the warning.
upper : str, optional
The upper bound of the version to check for the warning.

Notes
-----
The lower and upper bounds are exclusive so that a pytest.warns context
manager is returned if lower < pd.__version__ < upper.

Examples
--------
with pytest_warns_bounded(UserWarning, match="foo", lower="1.2.99"):
# Versions 1.3.0 and above will raise an error
# if the warning is not issued
pass

with pytest_warns_bounded(UserWarning, match="foo", upper="1.5.99"):
# Versions 1.6.0 and below will raise an error
# if the warning is not issued
pass

with pytest_warns_bounded(
UserWarning, match="foo", lower="1.2.99", upper="1.5.99"
):
# Versions between 1.3.x and 1.5.x will raise an error
pass
"""
lb = parse("0.0.0") if lower is None else parse(lower)
ub = parse("9999.0.0") if upper is None else parse(upper)
current = parse(pd.__version__)
if lb < current < ub:
return pytest.warns(warning, match=match)
else:
return nullcontext()
14 changes: 10 additions & 4 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
from pandas._typing import Scalar

from tests import (
PD_LTE_15,
TYPE_CHECKING_INVALID_USAGE,
check,
pytest_warns_bounded,
)

from pandas.io.formats.style import Styler
Expand Down Expand Up @@ -933,12 +935,16 @@ def test_types_describe() -> None:
}
)
df.describe()
with pytest.warns(FutureWarning, match="Treating datetime data as categorical"):
with pytest_warns_bounded(
FutureWarning, match="Treating datetime data as categorical", upper="1.5.999"
):
df.describe(percentiles=[0.5], include="all")
with pytest.warns(FutureWarning, match="Treating datetime data as categorical"):
df.describe(exclude=[np.number])
# datetime_is_numeric param added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
df.describe(datetime_is_numeric=True)
if PD_LTE_15:
# datetime_is_numeric param added in 1.1.0
# https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
# Remove in 2.0.0
df.describe(datetime_is_numeric=True)


def test_types_to_string() -> None:
Expand Down
10 changes: 7 additions & 3 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
)

from tests import (
PD_LTE_15,
TYPE_CHECKING_INVALID_USAGE,
check,
)
Expand Down Expand Up @@ -648,9 +649,12 @@ def test_types_describe() -> None:
s.describe(percentiles=[0.5], include="all")
with pytest.warns(DeprecationWarning, match="elementwise comparison failed"):
s.describe(exclude=np.number)
# datetime_is_numeric param added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
with pytest.warns(DeprecationWarning, match="elementwise comparison failed"):
s.describe(datetime_is_numeric=True)
if PD_LTE_15:
# datetime_is_numeric param added in 1.1.0
# https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
# Remove in 2.0.0
with pytest.warns(DeprecationWarning, match="elementwise comparison failed"):
s.describe(datetime_is_numeric=True)


def test_types_resample() -> None:
Expand Down