Skip to content

Commit 3669013

Browse files
authored
Fix pandas-nightly (#654)
* Mostly fix pandas-nightly * pd.PeriodDtype(freq=CustomBusinessDay)
1 parent f780a14 commit 3669013

File tree

5 files changed

+141
-77
lines changed

5 files changed

+141
-77
lines changed

tests/test_api_types.py

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
from pandas._typing import DtypeObj
1010

11-
from tests import check
11+
from tests import (
12+
check,
13+
pytest_warns_bounded,
14+
)
1215

1316
nparr = np.array([1, 2, 3])
1417
arr = pd.Series([1, 2, 3])
@@ -52,15 +55,20 @@ def test_is_bool_dtype() -> None:
5255

5356

5457
def test_is_categorical_dtype() -> None:
55-
check(assert_type(api.is_categorical_dtype(arr), bool), bool)
56-
check(assert_type(api.is_categorical_dtype(nparr), bool), bool)
57-
check(assert_type(api.is_categorical_dtype(dtylike), bool), bool)
58-
check(
59-
assert_type(api.is_categorical_dtype(dframe), bool),
60-
bool,
61-
)
62-
check(assert_type(api.is_categorical_dtype(ind), bool), bool)
63-
check(assert_type(api.is_categorical_dtype(ExtensionDtype), bool), bool)
58+
with pytest_warns_bounded(
59+
FutureWarning,
60+
match="is_categorical_dtype is deprecated and will be removed in a future version",
61+
lower="2.0.99",
62+
):
63+
check(assert_type(api.is_categorical_dtype(arr), bool), bool)
64+
check(assert_type(api.is_categorical_dtype(nparr), bool), bool)
65+
check(assert_type(api.is_categorical_dtype(dtylike), bool), bool)
66+
check(
67+
assert_type(api.is_categorical_dtype(dframe), bool),
68+
bool,
69+
)
70+
check(assert_type(api.is_categorical_dtype(ind), bool), bool)
71+
check(assert_type(api.is_categorical_dtype(ExtensionDtype), bool), bool)
6472

6573

6674
def test_is_complex() -> None:
@@ -124,15 +132,20 @@ def test_is_datetime64_ns_dtype() -> None:
124132

125133

126134
def test_is_datetime64tz_dtype() -> None:
127-
check(assert_type(api.is_datetime64tz_dtype(arr), bool), bool)
128-
check(assert_type(api.is_datetime64tz_dtype(nparr), bool), bool)
129-
check(assert_type(api.is_datetime64tz_dtype(dtylike), bool), bool)
130-
check(
131-
assert_type(api.is_datetime64tz_dtype(dframe), bool),
132-
bool,
133-
)
134-
check(assert_type(api.is_datetime64tz_dtype(ind), bool), bool)
135-
check(assert_type(api.is_datetime64tz_dtype(ExtensionDtype), bool), bool)
135+
with pytest_warns_bounded(
136+
FutureWarning,
137+
match="is_datetime64tz_dtype is deprecated and will be removed in a future version",
138+
lower="2.0.99",
139+
):
140+
check(assert_type(api.is_datetime64tz_dtype(arr), bool), bool)
141+
check(assert_type(api.is_datetime64tz_dtype(nparr), bool), bool)
142+
check(assert_type(api.is_datetime64tz_dtype(dtylike), bool), bool)
143+
check(
144+
assert_type(api.is_datetime64tz_dtype(dframe), bool),
145+
bool,
146+
)
147+
check(assert_type(api.is_datetime64tz_dtype(ind), bool), bool)
148+
check(assert_type(api.is_datetime64tz_dtype(ExtensionDtype), bool), bool)
136149

137150

138151
def test_is_dict_like() -> None:
@@ -209,15 +222,20 @@ def test_is_hashable() -> None:
209222

210223

211224
def test_is_int64_dtype() -> None:
212-
check(assert_type(api.is_int64_dtype(arr), bool), bool)
213-
check(assert_type(api.is_int64_dtype(nparr), bool), bool)
214-
check(assert_type(api.is_int64_dtype(dtylike), bool), bool)
215-
check(
216-
assert_type(api.is_int64_dtype(dframe), bool),
217-
bool,
218-
)
219-
check(assert_type(api.is_int64_dtype(ind), bool), bool)
220-
# check(assert_type(api.is_int64_dtype(ExtensionDtype), bool), bool) pandas GH 50923
225+
with pytest_warns_bounded(
226+
FutureWarning,
227+
match="is_int64_dtype is deprecated and will be removed in a future version",
228+
lower="2.0.99",
229+
):
230+
check(assert_type(api.is_int64_dtype(arr), bool), bool)
231+
check(assert_type(api.is_int64_dtype(nparr), bool), bool)
232+
check(assert_type(api.is_int64_dtype(dtylike), bool), bool)
233+
check(
234+
assert_type(api.is_int64_dtype(dframe), bool),
235+
bool,
236+
)
237+
check(assert_type(api.is_int64_dtype(ind), bool), bool)
238+
# check(assert_type(api.is_int64_dtype(ExtensionDtype), bool), bool) pandas GH 50923
221239

222240

223241
def test_is_integer() -> None:
@@ -257,16 +275,21 @@ def test_is_interval() -> None:
257275

258276

259277
def test_is_interval_dtype() -> None:
260-
check(assert_type(api.is_interval_dtype(obj), bool), bool)
261-
check(assert_type(api.is_interval_dtype(nparr), bool), bool)
262-
check(assert_type(api.is_interval_dtype(dtylike), bool), bool)
263-
check(assert_type(api.is_interval_dtype(arr), bool), bool)
264-
check(
265-
assert_type(api.is_interval_dtype(dframe), bool),
266-
bool,
267-
)
268-
check(assert_type(api.is_interval_dtype(ind), bool), bool)
269-
check(assert_type(api.is_interval_dtype(ExtensionDtype), bool), bool)
278+
with pytest_warns_bounded(
279+
FutureWarning,
280+
match="is_interval_dtype is deprecated and will be removed in a future version",
281+
lower="2.0.99",
282+
):
283+
check(assert_type(api.is_interval_dtype(obj), bool), bool)
284+
check(assert_type(api.is_interval_dtype(nparr), bool), bool)
285+
check(assert_type(api.is_interval_dtype(dtylike), bool), bool)
286+
check(assert_type(api.is_interval_dtype(arr), bool), bool)
287+
check(
288+
assert_type(api.is_interval_dtype(dframe), bool),
289+
bool,
290+
)
291+
check(assert_type(api.is_interval_dtype(ind), bool), bool)
292+
check(assert_type(api.is_interval_dtype(ExtensionDtype), bool), bool)
270293

271294

272295
def test_is_iterator() -> None:

tests/test_dtypes.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
from pandas._libs.missing import NAType
2323
from pandas._typing import Scalar
2424

25-
from tests import check
25+
from tests import (
26+
TYPE_CHECKING_INVALID_USAGE,
27+
check,
28+
)
2629

2730
from pandas.tseries.offsets import (
2831
BusinessDay,
@@ -54,10 +57,8 @@ def test_period_dtype() -> None:
5457
check(
5558
assert_type(pd.PeriodDtype(freq=BusinessDay()), pd.PeriodDtype), pd.PeriodDtype
5659
)
57-
check(
58-
assert_type(pd.PeriodDtype(freq=CustomBusinessDay()), pd.PeriodDtype),
59-
pd.PeriodDtype,
60-
)
60+
if TYPE_CHECKING_INVALID_USAGE:
61+
pd.PeriodDtype(freq=CustomBusinessDay()) # TODO(raises on 2.1)
6162
check(
6263
assert_type(p_dt.freq, pd.tseries.offsets.BaseOffset),
6364
pd.tseries.offsets.DateOffset,

tests/test_frame.py

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
PD_LTE_20,
4444
TYPE_CHECKING_INVALID_USAGE,
4545
check,
46+
pytest_warns_bounded,
4647
)
4748

4849
from pandas.io.formats.style import Styler
@@ -726,12 +727,17 @@ def gethead(s: pd.Series, y: int) -> pd.Series:
726727

727728
def test_types_applymap() -> None:
728729
df = pd.DataFrame(data={"col1": [2, 1], "col2": [3, 4]})
729-
df.applymap(lambda x: x**2)
730-
df.applymap(np.exp)
731-
df.applymap(str)
732-
# na_action parameter was added in 1.2.0 https://pandas.pydata.org/docs/whatsnew/v1.2.0.html
733-
df.applymap(np.exp, na_action="ignore")
734-
df.applymap(str, na_action=None)
730+
with pytest_warns_bounded(
731+
FutureWarning,
732+
match="DataFrame.applymap has been deprecated. Use DataFrame.map instead.",
733+
lower="2.0.99",
734+
):
735+
df.applymap(lambda x: x**2)
736+
df.applymap(np.exp)
737+
df.applymap(str)
738+
# na_action parameter was added in 1.2.0 https://pandas.pydata.org/docs/whatsnew/v1.2.0.html
739+
df.applymap(np.exp, na_action="ignore")
740+
df.applymap(str, na_action=None)
735741

736742

737743
def test_types_element_wise_arithmetic() -> None:
@@ -875,7 +881,12 @@ def test_types_groupby() -> None:
875881
df4: pd.DataFrame = df.groupby(by=["col1", "col2"]).count()
876882
df5: pd.DataFrame = df.groupby(by=["col1", "col2"]).filter(lambda x: x["col1"] > 0)
877883
df6: pd.DataFrame = df.groupby(by=["col1", "col2"]).nunique()
878-
df7: pd.DataFrame = df.groupby(by="col1").apply(sum)
884+
with pytest_warns_bounded(
885+
FutureWarning,
886+
match="DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated",
887+
lower="2.0.99",
888+
):
889+
df7: pd.DataFrame = df.groupby(by="col1").apply(sum)
879890
df8: pd.DataFrame = df.groupby("col1").transform("sum")
880891
s1: pd.Series = df.set_index("col1")["col2"]
881892
s2: pd.Series = s1.groupby("col1").transform("sum")
@@ -1454,9 +1465,19 @@ def test_types_regressions() -> None:
14541465

14551466
# https://github.com/microsoft/python-type-stubs/issues/115
14561467
df = pd.DataFrame({"A": [1, 2, 3], "B": [5, 6, 7]})
1457-
pd.DatetimeIndex(
1458-
data=df["A"], tz=None, normalize=False, closed=None, ambiguous="NaT", copy=True
1459-
)
1468+
with pytest_warns_bounded(
1469+
FutureWarning,
1470+
match="The 'closed' keyword in DatetimeIndex construction is deprecated",
1471+
lower="2.0.99",
1472+
):
1473+
pd.DatetimeIndex(
1474+
data=df["A"],
1475+
tz=None,
1476+
normalize=False,
1477+
closed=None,
1478+
ambiguous="NaT",
1479+
copy=True,
1480+
)
14601481

14611482

14621483
def test_read_csv() -> None:
@@ -2022,35 +2043,41 @@ def test_groupby_apply() -> None:
20222043
def sum_mean(x: pd.DataFrame) -> float:
20232044
return x.sum().mean()
20242045

2025-
check(assert_type(df.groupby("col1").apply(sum_mean), pd.Series), pd.Series)
2046+
with pytest_warns_bounded(
2047+
FutureWarning,
2048+
match="DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated",
2049+
lower="2.0.99",
2050+
):
2051+
check(assert_type(df.groupby("col1").apply(sum_mean), pd.Series), pd.Series)
20262052

2027-
lfunc: Callable[[pd.DataFrame], float] = lambda x: x.sum().mean()
2028-
check(
2029-
assert_type(df.groupby("col1").apply(lfunc), pd.Series),
2030-
pd.Series,
2031-
)
2053+
lfunc: Callable[[pd.DataFrame], float] = lambda x: x.sum().mean()
2054+
check(
2055+
assert_type(df.groupby("col1").apply(lfunc), pd.Series),
2056+
pd.Series,
2057+
)
20322058

2033-
def sum_to_list(x: pd.DataFrame) -> list:
2034-
return x.sum().tolist()
2059+
def sum_to_list(x: pd.DataFrame) -> list:
2060+
return x.sum().tolist()
20352061

2036-
check(assert_type(df.groupby("col1").apply(sum_to_list), pd.Series), pd.Series)
2062+
check(assert_type(df.groupby("col1").apply(sum_to_list), pd.Series), pd.Series)
20372063

2038-
def sum_to_series(x: pd.DataFrame) -> pd.Series:
2039-
return x.sum()
2064+
def sum_to_series(x: pd.DataFrame) -> pd.Series:
2065+
return x.sum()
20402066

2041-
check(
2042-
assert_type(df.groupby("col1").apply(sum_to_series), pd.DataFrame), pd.DataFrame
2043-
)
2067+
check(
2068+
assert_type(df.groupby("col1").apply(sum_to_series), pd.DataFrame),
2069+
pd.DataFrame,
2070+
)
20442071

2045-
def sample_to_df(x: pd.DataFrame) -> pd.DataFrame:
2046-
return x.sample()
2072+
def sample_to_df(x: pd.DataFrame) -> pd.DataFrame:
2073+
return x.sample()
20472074

2048-
check(
2049-
assert_type(
2050-
df.groupby("col1", group_keys=False).apply(sample_to_df), pd.DataFrame
2051-
),
2052-
pd.DataFrame,
2053-
)
2075+
check(
2076+
assert_type(
2077+
df.groupby("col1", group_keys=False).apply(sample_to_df), pd.DataFrame
2078+
),
2079+
pd.DataFrame,
2080+
)
20542081

20552082

20562083
def test_resample() -> None:

tests/test_series.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,12 @@ def square(x: float) -> float:
450450
def makeseries(x: float) -> pd.Series:
451451
return pd.Series([x, 2 * x])
452452

453-
check(assert_type(s.apply(makeseries), pd.DataFrame), pd.DataFrame)
453+
with pytest_warns_bounded(
454+
FutureWarning,
455+
match="Returning a DataFrame from Series.apply when the supplied functionreturns a Series is deprecated",
456+
lower="2.0.99",
457+
):
458+
check(assert_type(s.apply(makeseries), pd.DataFrame), pd.DataFrame)
454459

455460
# GH 293
456461

tests/test_timefuncs.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from pandas._libs.tslibs import BaseOffset
2323
from pandas._libs.tslibs.offsets import DateOffset
2424

25+
from tests import pytest_warns_bounded
26+
2527
if TYPE_CHECKING:
2628
from pandas._typing import FulldatetimeDict
2729
else:
@@ -361,7 +363,13 @@ def test_series_dt_accessors() -> None:
361363
check(assert_type(s0.dt.freq, Optional[str]), str)
362364
check(assert_type(s0.dt.isocalendar(), pd.DataFrame), pd.DataFrame)
363365
check(assert_type(s0.dt.to_period("D"), "PeriodSeries"), pd.Series, pd.Period)
364-
check(assert_type(s0.dt.to_pydatetime(), np.ndarray), np.ndarray, dt.datetime)
366+
367+
with pytest_warns_bounded(
368+
FutureWarning,
369+
match="The behavior of DatetimeProperties.to_pydatetime is deprecated",
370+
lower="2.0.99",
371+
):
372+
check(assert_type(s0.dt.to_pydatetime(), np.ndarray), np.ndarray, dt.datetime)
365373
s0_local = s0.dt.tz_localize("UTC")
366374
check(
367375
assert_type(s0_local, "TimestampSeries"),

0 commit comments

Comments
 (0)