Skip to content

Commit dc636d8

Browse files
GH1089 Fix pandas nightly and migrate tests to new framework (#1088)
* GH1089 Fix pandas nightly and migrate tests to new framework * GH1089 Migrate timefuncs tests to new framework
1 parent d410f2e commit dc636d8

7 files changed

+269
-80
lines changed

tests/test_frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -2958,6 +2958,7 @@ def sum_mean(x: pd.DataFrame) -> float:
29582958
with pytest_warns_bounded(
29592959
DeprecationWarning,
29602960
"DataFrameGroupBy.apply operated on the grouping columns.",
2961+
upper="2.99",
29612962
):
29622963
check(
29632964
assert_type(df.groupby("col1").apply(sum_mean), pd.Series),
@@ -2968,6 +2969,7 @@ def sum_mean(x: pd.DataFrame) -> float:
29682969
with pytest_warns_bounded(
29692970
DeprecationWarning,
29702971
"DataFrameGroupBy.apply operated on the grouping columns.",
2972+
upper="2.99",
29712973
):
29722974
check(assert_type(df.groupby("col1").apply(lfunc), pd.Series), pd.Series)
29732975

@@ -2977,6 +2979,7 @@ def sum_to_list(x: pd.DataFrame) -> list:
29772979
with pytest_warns_bounded(
29782980
DeprecationWarning,
29792981
"DataFrameGroupBy.apply operated on the grouping columns.",
2982+
upper="2.99",
29802983
):
29812984
check(assert_type(df.groupby("col1").apply(sum_to_list), pd.Series), pd.Series)
29822985

@@ -2986,6 +2989,7 @@ def sum_to_series(x: pd.DataFrame) -> pd.Series:
29862989
with pytest_warns_bounded(
29872990
DeprecationWarning,
29882991
"DataFrameGroupBy.apply operated on the grouping columns.",
2992+
upper="2.99",
29892993
):
29902994
check(
29912995
assert_type(df.groupby("col1").apply(sum_to_series), pd.DataFrame),
@@ -2998,6 +3002,7 @@ def sample_to_df(x: pd.DataFrame) -> pd.DataFrame:
29983002
with pytest_warns_bounded(
29993003
DeprecationWarning,
30003004
"DataFrameGroupBy.apply operated on the grouping columns.",
3005+
upper="2.99",
30013006
):
30023007
check(
30033008
assert_type(

tests/test_groupby.py

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def test_frame_groupby_resample() -> None:
8080
with pytest_warns_bounded(
8181
DeprecationWarning,
8282
"DataFrameGroupBy.(apply|resample) operated on the grouping columns",
83+
upper="2.99",
8384
):
8485
check(assert_type(GB_DF.resample("ME").sum(), DataFrame), DataFrame)
8586
check(assert_type(GB_DF.resample("ME").prod(), DataFrame), DataFrame)
@@ -126,6 +127,7 @@ def test_frame_groupby_resample() -> None:
126127
with pytest_warns_bounded(
127128
DeprecationWarning,
128129
"DataFrameGroupBy.(apply|resample) operated on the grouping columns",
130+
upper="2.99",
129131
):
130132
with pytest_warns_bounded(
131133
FutureWarning,
@@ -173,6 +175,7 @@ def f(val: DataFrame) -> Series:
173175
with pytest_warns_bounded(
174176
DeprecationWarning,
175177
"DataFrameGroupBy.(apply|resample) operated on the grouping columns",
178+
upper="2.99",
176179
):
177180
check(assert_type(GB_DF.resample("ME").aggregate(f), DataFrame), DataFrame)
178181

@@ -189,6 +192,7 @@ def df2scalar(val: DataFrame) -> float:
189192
with pytest_warns_bounded(
190193
DeprecationWarning,
191194
"DataFrameGroupBy.(apply|resample) operated on the grouping columns",
195+
upper="2.99",
192196
):
193197
with pytest_warns_bounded(
194198
FutureWarning,

tests/test_indexes.py

+3
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,13 @@ def test_column_contains() -> None:
8484
df = pd.DataFrame({"A": [1, 2], "B": ["c", "d"], "E": [3, 4]})
8585

8686
collist = [column for column in df.columns]
87+
check(assert_type(collist, list[str]), list, str)
8788

8889
collist2 = [column for column in df.columns[df.columns.str.contains("A|B")]]
90+
check(assert_type(collist2, list[str]), list, str)
8991

9092
length = len(df.columns[df.columns.str.contains("A|B")])
93+
check(assert_type(length, int), int)
9194

9295

9396
def test_column_sequence() -> None:

tests/test_interval.py

+47-14
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,61 @@
1212

1313

1414
def test_interval_init() -> None:
15-
i1: pd.Interval = pd.Interval(1, 2, closed="both")
16-
i2: pd.Interval = pd.Interval(1, right=2, closed="right")
17-
i3: pd.Interval = pd.Interval(left=1, right=2, closed="left")
15+
check(
16+
assert_type(pd.Interval(1, 2, closed="both"), "pd.Interval[int]"),
17+
pd.Interval,
18+
int,
19+
)
20+
check(
21+
assert_type(pd.Interval(1, right=2, closed="right"), "pd.Interval[int]"),
22+
pd.Interval,
23+
int,
24+
)
25+
check(
26+
assert_type(pd.Interval(left=1, right=2, closed="left"), "pd.Interval[int]"),
27+
pd.Interval,
28+
int,
29+
)
1830

1931

2032
def test_interval_arithmetic() -> None:
21-
i1: pd.Interval = pd.Interval(1, 2, closed="both")
22-
i2: pd.Interval = pd.Interval(1, right=2, closed="right")
33+
i1 = pd.Interval(1, 2, closed="both")
34+
check(assert_type(i1, "pd.Interval[int]"), pd.Interval, int)
35+
check(
36+
assert_type(pd.Interval(1, right=2, closed="right"), "pd.Interval[int]"),
37+
pd.Interval,
38+
int,
39+
)
2340

24-
i3: pd.Interval = i1 + 1
25-
i4: pd.Interval = i1 - 1
26-
i5: pd.Interval = i1 * 2
27-
i6: pd.Interval = i1 / 2
28-
i7: pd.Interval = i1 // 2
41+
check(assert_type(i1 + 1, "pd.Interval[int]"), pd.Interval, int)
42+
check(assert_type(i1 - 1, "pd.Interval[int]"), pd.Interval, int)
43+
check(assert_type(i1 * 2, "pd.Interval[int]"), pd.Interval, int)
44+
check(assert_type(i1 / 2, "pd.Interval[float]"), pd.Interval, float)
45+
check(assert_type(i1 // 2, "pd.Interval[int]"), pd.Interval, int)
2946

3047

3148
def test_max_intervals() -> None:
32-
i1 = pd.Interval(
33-
pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-02"), closed="both"
49+
check(
50+
assert_type(
51+
pd.Interval(
52+
pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-02"), closed="both"
53+
),
54+
"pd.Interval[pd.Timestamp]",
55+
),
56+
pd.Interval,
57+
pd.Timestamp,
3458
)
35-
i2 = pd.Interval(
36-
pd.Timestamp("2000-01-01T12:00:00"), pd.Timestamp("2000-01-02"), closed="both"
59+
check(
60+
assert_type(
61+
pd.Interval(
62+
pd.Timestamp("2000-01-01T12:00:00"),
63+
pd.Timestamp("2000-01-02"),
64+
closed="both",
65+
),
66+
"pd.Interval[pd.Timestamp]",
67+
),
68+
pd.Interval,
69+
pd.Timestamp,
3770
)
3871

3972

tests/test_interval_index.py

+47-11
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,63 @@
99

1010

1111
def test_from_breaks() -> None:
12-
ind1: pd.IntervalIndex = pd.IntervalIndex.from_breaks([0, 1, 2, 3], name="test")
13-
ind2: pd.IntervalIndex = pd.IntervalIndex.from_breaks(
14-
[0, 1, 2, 3], closed="right", name=123
12+
check(
13+
assert_type(
14+
pd.IntervalIndex.from_breaks([0, 1, 2, 3], name="test"),
15+
"pd.IntervalIndex[pd.Interval[int]]",
16+
),
17+
pd.IntervalIndex,
18+
pd.Interval,
19+
)
20+
check(
21+
assert_type(
22+
pd.IntervalIndex.from_breaks([0, 1, 2, 3], closed="right", name=123),
23+
"pd.IntervalIndex[pd.Interval[int]]",
24+
),
25+
pd.IntervalIndex,
26+
pd.Interval,
1527
)
1628

1729

1830
def test_from_arrays() -> None:
19-
ind1: pd.IntervalIndex = pd.IntervalIndex.from_arrays(
20-
[0, 1, 2], [1, 2, 3], name="test"
31+
check(
32+
assert_type(
33+
pd.IntervalIndex.from_arrays([0, 1, 2], [1, 2, 3], name="test"),
34+
"pd.IntervalIndex[pd.Interval[int]]",
35+
),
36+
pd.IntervalIndex,
37+
pd.Interval,
2138
)
22-
ind2: pd.IntervalIndex = pd.IntervalIndex.from_arrays(
23-
[0, 1, 2], [1, 2, 3], closed="right", name=123
39+
check(
40+
assert_type(
41+
pd.IntervalIndex.from_arrays(
42+
[0, 1, 2], [1, 2, 3], closed="right", name=123
43+
),
44+
"pd.IntervalIndex[pd.Interval[int]]",
45+
),
46+
pd.IntervalIndex,
47+
pd.Interval,
2448
)
2549

2650

2751
def test_from_tuples() -> None:
28-
ind1: pd.IntervalIndex = pd.IntervalIndex.from_tuples(
29-
[(0, 1), (1, 2), (2, 3)], name="test"
52+
check(
53+
assert_type(
54+
pd.IntervalIndex.from_tuples([(0, 1), (1, 2), (2, 3)], name="test"),
55+
"pd.IntervalIndex[pd.Interval[int]]",
56+
),
57+
pd.IntervalIndex,
58+
pd.Interval,
3059
)
31-
ind2: pd.IntervalIndex = pd.IntervalIndex.from_tuples(
32-
[(0, 1), (1, 2), (2, 3)], closed="right", name=123
60+
check(
61+
assert_type(
62+
pd.IntervalIndex.from_tuples(
63+
[(0, 1), (1, 2), (2, 3)], closed="right", name=123
64+
),
65+
"pd.IntervalIndex[pd.Interval[int]]",
66+
),
67+
pd.IntervalIndex,
68+
pd.Interval,
3369
)
3470

3571

tests/test_pandas.py

+80-18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import pytest
2020
from typing_extensions import (
2121
Never,
22+
TypeAlias,
2223
assert_type,
2324
)
2425

@@ -33,23 +34,66 @@
3334
pytest_warns_bounded,
3435
)
3536

37+
if TYPE_CHECKING:
38+
from pandas.core.series import TimestampSeries
39+
else:
40+
TimestampSeries: TypeAlias = pd.Series
41+
3642

3743
def test_types_to_datetime() -> None:
3844
df = pd.DataFrame({"year": [2015, 2016], "month": [2, 3], "day": [4, 5]})
39-
r1: pd.Series = pd.to_datetime(df)
45+
check(assert_type(pd.to_datetime(df), TimestampSeries), pd.Series, pd.Timestamp)
4046

41-
r2: pd.Series = pd.to_datetime(df, unit="s", origin="unix")
42-
r3: pd.Series = pd.to_datetime(
43-
df, unit="ns", dayfirst=True, utc=False, format="%M:%D", exact=False
47+
check(
48+
assert_type(pd.to_datetime(df, unit="s", origin="unix"), TimestampSeries),
49+
pd.Series,
50+
pd.Timestamp,
51+
)
52+
check(
53+
assert_type(
54+
pd.to_datetime(
55+
df, unit="ns", dayfirst=True, utc=False, format="%M:%D", exact=False
56+
),
57+
TimestampSeries,
58+
),
59+
pd.Series,
60+
pd.Timestamp,
61+
)
62+
check(
63+
assert_type(
64+
pd.to_datetime([1, 2], unit="D", origin=pd.Timestamp("01/01/2000")),
65+
pd.DatetimeIndex,
66+
),
67+
pd.DatetimeIndex,
68+
)
69+
check(
70+
assert_type(
71+
pd.to_datetime([1, 2], unit="D", origin=3),
72+
pd.DatetimeIndex,
73+
),
74+
pd.DatetimeIndex,
75+
)
76+
check(
77+
assert_type(
78+
pd.to_datetime(["2022-01-03", "2022-02-22"]),
79+
pd.DatetimeIndex,
80+
),
81+
pd.DatetimeIndex,
4482
)
45-
r4: pd.DatetimeIndex = pd.to_datetime(
46-
[1, 2], unit="D", origin=pd.Timestamp("01/01/2000")
83+
check(
84+
assert_type(
85+
pd.to_datetime(pd.Index(["2022-01-03", "2022-02-22"])),
86+
pd.DatetimeIndex,
87+
),
88+
pd.DatetimeIndex,
4789
)
48-
r5: pd.DatetimeIndex = pd.to_datetime([1, 2], unit="D", origin=3)
49-
r6: pd.DatetimeIndex = pd.to_datetime(["2022-01-03", "2022-02-22"])
50-
r7: pd.DatetimeIndex = pd.to_datetime(pd.Index(["2022-01-03", "2022-02-22"]))
51-
r8: pd.Series = pd.to_datetime(
52-
{"year": [2015, 2016], "month": [2, 3], "day": [4, 5]}
90+
check(
91+
assert_type(
92+
pd.to_datetime({"year": [2015, 2016], "month": [2, 3], "day": [4, 5]}),
93+
TimestampSeries,
94+
),
95+
pd.Series,
96+
pd.Timestamp,
5397
)
5498

5599

@@ -179,7 +223,7 @@ def test_types_concat() -> None:
179223

180224
check(
181225
assert_type(
182-
pd.concat(map(lambda x: s2, ["some_value", 3]), axis=1), pd.DataFrame
226+
pd.concat(map(lambda _: s2, ["some_value", 3]), axis=1), pd.DataFrame
183227
),
184228
pd.DataFrame,
185229
)
@@ -306,14 +350,32 @@ def test_types_json_normalize() -> None:
306350
{"name": {"given": "Mose", "family": "Regner"}},
307351
{"id": 2, "name": "Faye Raker"},
308352
]
309-
df1: pd.DataFrame = pd.json_normalize(data=data1)
310-
df2: pd.DataFrame = pd.json_normalize(data=data1, max_level=0, sep=";")
311-
df3: pd.DataFrame = pd.json_normalize(
312-
data=data1, meta_prefix="id", record_prefix="name", errors="raise"
353+
check(assert_type(pd.json_normalize(data=data1), pd.DataFrame), pd.DataFrame)
354+
check(
355+
assert_type(pd.json_normalize(data=data1, max_level=0, sep=";"), pd.DataFrame),
356+
pd.DataFrame,
357+
)
358+
check(
359+
assert_type(
360+
pd.json_normalize(
361+
data=data1, meta_prefix="id", record_prefix="name", errors="raise"
362+
),
363+
pd.DataFrame,
364+
),
365+
pd.DataFrame,
366+
)
367+
check(
368+
assert_type(
369+
pd.json_normalize(data=data1, record_path=None, meta="id"), pd.DataFrame
370+
),
371+
pd.DataFrame,
313372
)
314-
df4: pd.DataFrame = pd.json_normalize(data=data1, record_path=None, meta="id")
315373
data2: dict[str, Any] = {"name": {"given": "Mose", "family": "Regner"}}
316-
df5: pd.DataFrame = pd.json_normalize(data=data2)
374+
check(
375+
assert_type(data2, dict[str, Any]),
376+
dict,
377+
)
378+
check(assert_type(pd.json_normalize(data=data2), pd.DataFrame), pd.DataFrame)
317379

318380

319381
def test_isna() -> None:

0 commit comments

Comments
 (0)