Skip to content

Commit de675b7

Browse files
Dr-Irvramvikrams
andauthored
Fix nightly tests (#436)
* for issue 429 * update * update * update * update * Update test_indexes.py * update * update * update * update * update * update * add tests to catch exceptions * avoid pyright 1.1.280 * pin pyright to 1.1.279 * change warning for 1.5.2 with crosstab test Co-authored-by: ramvikrams <[email protected]>
1 parent e0f0fe1 commit de675b7

File tree

6 files changed

+109
-29
lines changed

6 files changed

+109
-29
lines changed

tests/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from contextlib import (
44
AbstractContextManager,
55
nullcontext,
6+
suppress,
67
)
78
import os
89
import platform
@@ -56,6 +57,7 @@ def pytest_warns_bounded(
5657
lower: str | None = None,
5758
upper: str | None = None,
5859
version_str: str | None = None,
60+
upper_exception: type[Exception] | None = None,
5961
) -> AbstractContextManager:
6062
"""
6163
Version conditional pytest.warns context manager
@@ -77,6 +79,9 @@ def pytest_warns_bounded(
7779
version_str: str, optional
7880
The version string to use. If None, then uses the pandas version.
7981
Can be used to check a python version as well
82+
upper_exception: Exception, optional
83+
Exception to catch if the pandas version is greater than or equal to
84+
the upper bound
8085
8186
Notes
8287
-----
@@ -108,6 +113,15 @@ def pytest_warns_bounded(
108113
# Python version 3.11 and above will raise an error
109114
# if the warning is not issued
110115
pass
116+
117+
with pytest_warns_bounded(
118+
UserWarning, match="foo", lower="1.2.99", upper="1.5.99",
119+
upper_exception=AttributeError
120+
):
121+
# Versions between 1.3.x and 1.5.x will raise an error
122+
# Above 1.5.x, we expect an `AttributeError` to be raised
123+
pass
124+
111125
"""
112126
lb = Version("0.0.0") if lower is None else Version(lower)
113127
ub = Version("9999.0.0") if upper is None else Version(upper)
@@ -118,4 +132,7 @@ def pytest_warns_bounded(
118132
if lb < current < ub:
119133
return pytest.warns(warning, match=match)
120134
else:
121-
return nullcontext()
135+
if upper_exception is None:
136+
return nullcontext()
137+
else:
138+
return suppress(upper_exception)

tests/test_frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ def test_groupby_series_methods() -> None:
13001300
gb.min().loc[2]
13011301
gb.nlargest().loc[2]
13021302
gb.nsmallest().loc[2]
1303-
gb.nth(0).loc[2]
1303+
gb.nth(0).loc[1]
13041304

13051305

13061306
def test_indexslice_setitem():

tests/test_indexes.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
from numpy import typing as npt
88
import pandas as pd
99
from pandas.core.indexes.numeric import NumericIndex
10-
import pytest
1110
from typing_extensions import assert_type
1211

13-
from tests import check
12+
from tests import (
13+
check,
14+
pytest_warns_bounded,
15+
)
1416

1517

1618
def test_index_unique() -> None:
@@ -166,7 +168,12 @@ def test_index_relops() -> None:
166168

167169

168170
def test_range_index_union():
169-
with pytest.warns(FutureWarning, match="pandas.Int64Index"):
171+
with pytest_warns_bounded(
172+
FutureWarning,
173+
match="pandas.Int64Index",
174+
upper="1.5.99",
175+
upper_exception=AttributeError,
176+
):
170177
check(
171178
assert_type(
172179
pd.RangeIndex(0, 10).union(pd.RangeIndex(10, 20)),

tests/test_pandas.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
from pandas._typing import Scalar
2222

2323
from tests import (
24+
PD_LTE_15,
2425
TYPE_CHECKING_INVALID_USAGE,
2526
check,
27+
pytest_warns_bounded,
2628
)
2729

2830

@@ -752,33 +754,49 @@ def test_factorize() -> None:
752754
def test_index_unqiue() -> None:
753755
ci = pd.CategoricalIndex(["a", "b", "a", "c"])
754756
dti = pd.DatetimeIndex([pd.Timestamp(2000, 1, 1)])
755-
with pytest.warns(FutureWarning, match="pandas.Float64Index is deprecated"):
757+
with pytest_warns_bounded(
758+
FutureWarning,
759+
match="pandas.Float64Index is deprecated",
760+
upper="1.5.99",
761+
upper_exception=AttributeError,
762+
):
756763
fi = pd.Float64Index([1.0, 2.0])
757764
i = pd.Index(["a", "b", "c", "a"])
758-
with pytest.warns(FutureWarning, match="pandas.Int64Index is deprecated"):
765+
with pytest_warns_bounded(
766+
FutureWarning,
767+
match="pandas.Int64Index is deprecated",
768+
upper="1.5.99",
769+
upper_exception=AttributeError,
770+
):
759771
i64i = pd.Int64Index([1, 2, 3, 4])
760772
pi = pd.period_range("2000Q1", periods=2, freq="Q")
761773
ri = pd.RangeIndex(0, 10)
762-
with pytest.warns(FutureWarning, match="pandas.UInt64Index is deprecated"):
774+
with pytest_warns_bounded(
775+
FutureWarning,
776+
match="pandas.UInt64Index is deprecated",
777+
upper="1.5.99",
778+
upper_exception=AttributeError,
779+
):
763780
ui = pd.UInt64Index([0, 1, 2, 3, 5])
764781
tdi = pd.timedelta_range("1 day", "10 days", periods=10)
765782
mi = pd.MultiIndex.from_product([["a", "b"], ["apple", "banana"]])
766783
interval_i = pd.interval_range(1, 10, periods=10)
767784

768785
check(assert_type(pd.unique(ci), pd.CategoricalIndex), pd.CategoricalIndex)
769786
check(assert_type(pd.unique(dti), np.ndarray), np.ndarray)
770-
check(assert_type(pd.unique(fi), np.ndarray), np.ndarray)
771787
check(assert_type(pd.unique(i), np.ndarray), np.ndarray)
772-
check(assert_type(pd.unique(i64i), np.ndarray), np.ndarray)
773788
check(assert_type(pd.unique(pi), pd.PeriodIndex), pd.PeriodIndex)
774789
check(assert_type(pd.unique(ri), np.ndarray), np.ndarray)
775-
check(assert_type(pd.unique(ui), np.ndarray), np.ndarray)
776790
check(assert_type(pd.unique(tdi), np.ndarray), np.ndarray)
777791
check(assert_type(pd.unique(mi), np.ndarray), np.ndarray)
778792
check(
779793
assert_type(pd.unique(interval_i), "pd.IntervalIndex[pd.Interval[int]]"),
780794
pd.IntervalIndex,
781795
)
796+
if PD_LTE_15:
797+
check(assert_type(pd.unique(fi), np.ndarray), np.ndarray)
798+
check(assert_type(pd.unique(i64i), np.ndarray), np.ndarray)
799+
check(assert_type(pd.unique(ui), np.ndarray), np.ndarray)
782800

783801

784802
def test_cut() -> None:
@@ -1422,7 +1440,12 @@ def test_crosstab_args() -> None:
14221440
),
14231441
pd.DataFrame,
14241442
)
1425-
with pytest.warns(FutureWarning):
1443+
with pytest_warns_bounded(
1444+
FutureWarning,
1445+
"pivot_table dropped a column because",
1446+
upper="1.5.99",
1447+
upper_exception=TypeError,
1448+
):
14261449
check(
14271450
assert_type(
14281451
pd.crosstab(a, b, values=pd.Categorical(values), aggfunc=np.sum),

tests/test_scalars.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import numpy as np
1414
from numpy import typing as npt
1515
import pandas as pd
16-
import pytest
1716
import pytz
1817
from typing_extensions import (
1918
TypeAlias,
@@ -29,6 +28,7 @@
2928
from tests import (
3029
TYPE_CHECKING_INVALID_USAGE,
3130
check,
31+
pytest_warns_bounded,
3232
)
3333

3434
from pandas.tseries.offsets import Day
@@ -671,9 +671,14 @@ def test_timedelta_add_sub() -> None:
671671
def test_timedelta_mul_div() -> None:
672672
td = pd.Timedelta("1 day")
673673

674-
with pytest.warns(FutureWarning):
675-
i_idx = cast(pd.Int64Index, pd.Index([1, 2, 3], dtype=int))
676-
f_idx = cast(pd.Float64Index, pd.Index([1.2, 2.2, 3.4], dtype=float))
674+
i_idx = pd.Index([1, 2, 3], dtype=int)
675+
f_idx = pd.Index([1.2, 2.2, 3.4], dtype=float)
676+
677+
with pytest_warns_bounded(
678+
FutureWarning, upper="1.5.99", match="", upper_exception=AttributeError
679+
):
680+
i_idx = cast(pd.Int64Index, i_idx)
681+
f_idx = cast(pd.Float64Index, f_idx)
677682

678683
np_intp_arr: npt.NDArray[np.integer] = np.array([1, 2, 3])
679684
np_float_arr: npt.NDArray[np.floating] = np.array([1.2, 2.2, 3.4])
@@ -788,9 +793,14 @@ def test_timedelta_mul_div() -> None:
788793
def test_timedelta_mod_abs_unary() -> None:
789794
td = pd.Timedelta("1 day")
790795

791-
with pytest.warns(FutureWarning):
792-
i_idx = cast(pd.Int64Index, pd.Index([1, 2, 3], dtype=int))
793-
f_idx = cast(pd.Float64Index, pd.Index([1.2, 2.2, 3.4], dtype=float))
796+
i_idx = pd.Index([1, 2, 3], dtype=int)
797+
f_idx = pd.Index([1.2, 2.2, 3.4], dtype=float)
798+
799+
with pytest_warns_bounded(
800+
FutureWarning, upper="1.5.99", match="", upper_exception=AttributeError
801+
):
802+
i_idx = cast(pd.Int64Index, i_idx)
803+
f_idx = cast(pd.Float64Index, f_idx)
794804

795805
check(assert_type(td % 3, pd.Timedelta), pd.Timedelta)
796806
check(assert_type(td % 3.5, pd.Timedelta), pd.Timedelta)

tests/test_series.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
PD_LTE_15,
3737
TYPE_CHECKING_INVALID_USAGE,
3838
check,
39+
pytest_warns_bounded,
3940
)
4041

4142
if TYPE_CHECKING:
@@ -259,16 +260,30 @@ def test_types_shift() -> None:
259260

260261

261262
def test_types_rank() -> None:
262-
s = pd.Series([1, 1, 2, 5, 6, np.nan, "milion"])
263-
with pytest.warns(FutureWarning, match="Dropping of nuisance columns"):
263+
s = pd.Series([1, 1, 2, 5, 6, np.nan])
264+
if PD_LTE_15:
265+
s[6] = "milion"
266+
with pytest_warns_bounded(
267+
FutureWarning,
268+
match="Dropping of nuisance columns",
269+
upper="1.5.99",
270+
):
264271
s.rank()
265-
with pytest.warns(FutureWarning, match="Dropping of nuisance columns"):
272+
with pytest_warns_bounded(
273+
FutureWarning, match="Dropping of nuisance columns", upper="1.5.99"
274+
):
266275
s.rank(axis=0, na_option="bottom")
267-
with pytest.warns(FutureWarning, match="Dropping of nuisance columns"):
276+
with pytest_warns_bounded(
277+
FutureWarning, match="Dropping of nuisance columns", upper="1.5.99"
278+
):
268279
s.rank(method="min", pct=True)
269-
with pytest.warns(FutureWarning, match="Dropping of nuisance columns"):
280+
with pytest_warns_bounded(
281+
FutureWarning, match="Dropping of nuisance columns", upper="1.5.99"
282+
):
270283
s.rank(method="dense", ascending=True)
271-
with pytest.warns(FutureWarning, match="Calling Series.rank with numeric_only"):
284+
with pytest_warns_bounded(
285+
FutureWarning, match="Calling Series.rank with numeric_only", upper="1.5.99"
286+
):
272287
s.rank(method="first", numeric_only=True)
273288
s2 = pd.Series([1, 1, 2, 5, 6, np.nan])
274289
s2.rank(method="first", numeric_only=True)
@@ -643,17 +658,25 @@ def test_types_transform() -> None:
643658

644659
def test_types_describe() -> None:
645660
s = pd.Series([1, 2, 3, np.datetime64("2000-01-01")])
646-
with pytest.warns(DeprecationWarning, match="elementwise comparison failed"):
661+
with pytest_warns_bounded(
662+
DeprecationWarning, match="elementwise comparison failed", upper="1.5.99"
663+
):
647664
s.describe()
648-
with pytest.warns(DeprecationWarning, match="elementwise comparison failed"):
665+
with pytest_warns_bounded(
666+
DeprecationWarning, match="elementwise comparison failed", upper="1.5.99"
667+
):
649668
s.describe(percentiles=[0.5], include="all")
650-
with pytest.warns(DeprecationWarning, match="elementwise comparison failed"):
669+
with pytest_warns_bounded(
670+
DeprecationWarning, match="elementwise comparison failed", upper="1.5.99"
671+
):
651672
s.describe(exclude=np.number)
652673
if PD_LTE_15:
653674
# datetime_is_numeric param added in 1.1.0
654675
# https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
655676
# Remove in 2.0.0
656-
with pytest.warns(DeprecationWarning, match="elementwise comparison failed"):
677+
with pytest_warns_bounded(
678+
DeprecationWarning, match="elementwise comparison failed", upper="1.5.99"
679+
):
657680
s.describe(datetime_is_numeric=True)
658681

659682

0 commit comments

Comments
 (0)