From 3669013db3c5a62fc5d540219e60990b0151a560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Thu, 20 Apr 2023 15:53:42 -0400 Subject: [PATCH] Fix pandas-nightly (#654) * Mostly fix pandas-nightly * pd.PeriodDtype(freq=CustomBusinessDay) --- tests/test_api_types.py | 99 +++++++++++++++++++++++++---------------- tests/test_dtypes.py | 11 ++--- tests/test_frame.py | 91 ++++++++++++++++++++++++------------- tests/test_series.py | 7 ++- tests/test_timefuncs.py | 10 ++++- 5 files changed, 141 insertions(+), 77 deletions(-) diff --git a/tests/test_api_types.py b/tests/test_api_types.py index a0956870..30ac31f4 100644 --- a/tests/test_api_types.py +++ b/tests/test_api_types.py @@ -8,7 +8,10 @@ from pandas._typing import DtypeObj -from tests import check +from tests import ( + check, + pytest_warns_bounded, +) nparr = np.array([1, 2, 3]) arr = pd.Series([1, 2, 3]) @@ -52,15 +55,20 @@ def test_is_bool_dtype() -> None: def test_is_categorical_dtype() -> None: - check(assert_type(api.is_categorical_dtype(arr), bool), bool) - check(assert_type(api.is_categorical_dtype(nparr), bool), bool) - check(assert_type(api.is_categorical_dtype(dtylike), bool), bool) - check( - assert_type(api.is_categorical_dtype(dframe), bool), - bool, - ) - check(assert_type(api.is_categorical_dtype(ind), bool), bool) - check(assert_type(api.is_categorical_dtype(ExtensionDtype), bool), bool) + with pytest_warns_bounded( + FutureWarning, + match="is_categorical_dtype is deprecated and will be removed in a future version", + lower="2.0.99", + ): + check(assert_type(api.is_categorical_dtype(arr), bool), bool) + check(assert_type(api.is_categorical_dtype(nparr), bool), bool) + check(assert_type(api.is_categorical_dtype(dtylike), bool), bool) + check( + assert_type(api.is_categorical_dtype(dframe), bool), + bool, + ) + check(assert_type(api.is_categorical_dtype(ind), bool), bool) + check(assert_type(api.is_categorical_dtype(ExtensionDtype), bool), bool) def test_is_complex() -> None: @@ -124,15 +132,20 @@ def test_is_datetime64_ns_dtype() -> None: def test_is_datetime64tz_dtype() -> None: - check(assert_type(api.is_datetime64tz_dtype(arr), bool), bool) - check(assert_type(api.is_datetime64tz_dtype(nparr), bool), bool) - check(assert_type(api.is_datetime64tz_dtype(dtylike), bool), bool) - check( - assert_type(api.is_datetime64tz_dtype(dframe), bool), - bool, - ) - check(assert_type(api.is_datetime64tz_dtype(ind), bool), bool) - check(assert_type(api.is_datetime64tz_dtype(ExtensionDtype), bool), bool) + with pytest_warns_bounded( + FutureWarning, + match="is_datetime64tz_dtype is deprecated and will be removed in a future version", + lower="2.0.99", + ): + check(assert_type(api.is_datetime64tz_dtype(arr), bool), bool) + check(assert_type(api.is_datetime64tz_dtype(nparr), bool), bool) + check(assert_type(api.is_datetime64tz_dtype(dtylike), bool), bool) + check( + assert_type(api.is_datetime64tz_dtype(dframe), bool), + bool, + ) + check(assert_type(api.is_datetime64tz_dtype(ind), bool), bool) + check(assert_type(api.is_datetime64tz_dtype(ExtensionDtype), bool), bool) def test_is_dict_like() -> None: @@ -209,15 +222,20 @@ def test_is_hashable() -> None: def test_is_int64_dtype() -> None: - check(assert_type(api.is_int64_dtype(arr), bool), bool) - check(assert_type(api.is_int64_dtype(nparr), bool), bool) - check(assert_type(api.is_int64_dtype(dtylike), bool), bool) - check( - assert_type(api.is_int64_dtype(dframe), bool), - bool, - ) - check(assert_type(api.is_int64_dtype(ind), bool), bool) - # check(assert_type(api.is_int64_dtype(ExtensionDtype), bool), bool) pandas GH 50923 + with pytest_warns_bounded( + FutureWarning, + match="is_int64_dtype is deprecated and will be removed in a future version", + lower="2.0.99", + ): + check(assert_type(api.is_int64_dtype(arr), bool), bool) + check(assert_type(api.is_int64_dtype(nparr), bool), bool) + check(assert_type(api.is_int64_dtype(dtylike), bool), bool) + check( + assert_type(api.is_int64_dtype(dframe), bool), + bool, + ) + check(assert_type(api.is_int64_dtype(ind), bool), bool) + # check(assert_type(api.is_int64_dtype(ExtensionDtype), bool), bool) pandas GH 50923 def test_is_integer() -> None: @@ -257,16 +275,21 @@ def test_is_interval() -> None: def test_is_interval_dtype() -> None: - check(assert_type(api.is_interval_dtype(obj), bool), bool) - check(assert_type(api.is_interval_dtype(nparr), bool), bool) - check(assert_type(api.is_interval_dtype(dtylike), bool), bool) - check(assert_type(api.is_interval_dtype(arr), bool), bool) - check( - assert_type(api.is_interval_dtype(dframe), bool), - bool, - ) - check(assert_type(api.is_interval_dtype(ind), bool), bool) - check(assert_type(api.is_interval_dtype(ExtensionDtype), bool), bool) + with pytest_warns_bounded( + FutureWarning, + match="is_interval_dtype is deprecated and will be removed in a future version", + lower="2.0.99", + ): + check(assert_type(api.is_interval_dtype(obj), bool), bool) + check(assert_type(api.is_interval_dtype(nparr), bool), bool) + check(assert_type(api.is_interval_dtype(dtylike), bool), bool) + check(assert_type(api.is_interval_dtype(arr), bool), bool) + check( + assert_type(api.is_interval_dtype(dframe), bool), + bool, + ) + check(assert_type(api.is_interval_dtype(ind), bool), bool) + check(assert_type(api.is_interval_dtype(ExtensionDtype), bool), bool) def test_is_iterator() -> None: diff --git a/tests/test_dtypes.py b/tests/test_dtypes.py index 7fc8da84..53c8d81e 100644 --- a/tests/test_dtypes.py +++ b/tests/test_dtypes.py @@ -22,7 +22,10 @@ from pandas._libs.missing import NAType from pandas._typing import Scalar -from tests import check +from tests import ( + TYPE_CHECKING_INVALID_USAGE, + check, +) from pandas.tseries.offsets import ( BusinessDay, @@ -54,10 +57,8 @@ def test_period_dtype() -> None: check( assert_type(pd.PeriodDtype(freq=BusinessDay()), pd.PeriodDtype), pd.PeriodDtype ) - check( - assert_type(pd.PeriodDtype(freq=CustomBusinessDay()), pd.PeriodDtype), - pd.PeriodDtype, - ) + if TYPE_CHECKING_INVALID_USAGE: + pd.PeriodDtype(freq=CustomBusinessDay()) # TODO(raises on 2.1) check( assert_type(p_dt.freq, pd.tseries.offsets.BaseOffset), pd.tseries.offsets.DateOffset, diff --git a/tests/test_frame.py b/tests/test_frame.py index 893eb46f..dc58f861 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -43,6 +43,7 @@ PD_LTE_20, TYPE_CHECKING_INVALID_USAGE, check, + pytest_warns_bounded, ) from pandas.io.formats.style import Styler @@ -726,12 +727,17 @@ def gethead(s: pd.Series, y: int) -> pd.Series: def test_types_applymap() -> None: df = pd.DataFrame(data={"col1": [2, 1], "col2": [3, 4]}) - df.applymap(lambda x: x**2) - df.applymap(np.exp) - df.applymap(str) - # na_action parameter was added in 1.2.0 https://pandas.pydata.org/docs/whatsnew/v1.2.0.html - df.applymap(np.exp, na_action="ignore") - df.applymap(str, na_action=None) + with pytest_warns_bounded( + FutureWarning, + match="DataFrame.applymap has been deprecated. Use DataFrame.map instead.", + lower="2.0.99", + ): + df.applymap(lambda x: x**2) + df.applymap(np.exp) + df.applymap(str) + # na_action parameter was added in 1.2.0 https://pandas.pydata.org/docs/whatsnew/v1.2.0.html + df.applymap(np.exp, na_action="ignore") + df.applymap(str, na_action=None) def test_types_element_wise_arithmetic() -> None: @@ -875,7 +881,12 @@ def test_types_groupby() -> None: df4: pd.DataFrame = df.groupby(by=["col1", "col2"]).count() df5: pd.DataFrame = df.groupby(by=["col1", "col2"]).filter(lambda x: x["col1"] > 0) df6: pd.DataFrame = df.groupby(by=["col1", "col2"]).nunique() - df7: pd.DataFrame = df.groupby(by="col1").apply(sum) + with pytest_warns_bounded( + FutureWarning, + match="DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated", + lower="2.0.99", + ): + df7: pd.DataFrame = df.groupby(by="col1").apply(sum) df8: pd.DataFrame = df.groupby("col1").transform("sum") s1: pd.Series = df.set_index("col1")["col2"] s2: pd.Series = s1.groupby("col1").transform("sum") @@ -1454,9 +1465,19 @@ def test_types_regressions() -> None: # https://github.com/microsoft/python-type-stubs/issues/115 df = pd.DataFrame({"A": [1, 2, 3], "B": [5, 6, 7]}) - pd.DatetimeIndex( - data=df["A"], tz=None, normalize=False, closed=None, ambiguous="NaT", copy=True - ) + with pytest_warns_bounded( + FutureWarning, + match="The 'closed' keyword in DatetimeIndex construction is deprecated", + lower="2.0.99", + ): + pd.DatetimeIndex( + data=df["A"], + tz=None, + normalize=False, + closed=None, + ambiguous="NaT", + copy=True, + ) def test_read_csv() -> None: @@ -2022,35 +2043,41 @@ def test_groupby_apply() -> None: def sum_mean(x: pd.DataFrame) -> float: return x.sum().mean() - check(assert_type(df.groupby("col1").apply(sum_mean), pd.Series), pd.Series) + with pytest_warns_bounded( + FutureWarning, + match="DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated", + lower="2.0.99", + ): + check(assert_type(df.groupby("col1").apply(sum_mean), pd.Series), pd.Series) - lfunc: Callable[[pd.DataFrame], float] = lambda x: x.sum().mean() - check( - assert_type(df.groupby("col1").apply(lfunc), pd.Series), - pd.Series, - ) + lfunc: Callable[[pd.DataFrame], float] = lambda x: x.sum().mean() + check( + assert_type(df.groupby("col1").apply(lfunc), pd.Series), + pd.Series, + ) - def sum_to_list(x: pd.DataFrame) -> list: - return x.sum().tolist() + def sum_to_list(x: pd.DataFrame) -> list: + return x.sum().tolist() - check(assert_type(df.groupby("col1").apply(sum_to_list), pd.Series), pd.Series) + check(assert_type(df.groupby("col1").apply(sum_to_list), pd.Series), pd.Series) - def sum_to_series(x: pd.DataFrame) -> pd.Series: - return x.sum() + def sum_to_series(x: pd.DataFrame) -> pd.Series: + return x.sum() - check( - assert_type(df.groupby("col1").apply(sum_to_series), pd.DataFrame), pd.DataFrame - ) + check( + assert_type(df.groupby("col1").apply(sum_to_series), pd.DataFrame), + pd.DataFrame, + ) - def sample_to_df(x: pd.DataFrame) -> pd.DataFrame: - return x.sample() + def sample_to_df(x: pd.DataFrame) -> pd.DataFrame: + return x.sample() - check( - assert_type( - df.groupby("col1", group_keys=False).apply(sample_to_df), pd.DataFrame - ), - pd.DataFrame, - ) + check( + assert_type( + df.groupby("col1", group_keys=False).apply(sample_to_df), pd.DataFrame + ), + pd.DataFrame, + ) def test_resample() -> None: diff --git a/tests/test_series.py b/tests/test_series.py index 8435c134..86340473 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -450,7 +450,12 @@ def square(x: float) -> float: def makeseries(x: float) -> pd.Series: return pd.Series([x, 2 * x]) - check(assert_type(s.apply(makeseries), pd.DataFrame), pd.DataFrame) + with pytest_warns_bounded( + FutureWarning, + match="Returning a DataFrame from Series.apply when the supplied functionreturns a Series is deprecated", + lower="2.0.99", + ): + check(assert_type(s.apply(makeseries), pd.DataFrame), pd.DataFrame) # GH 293 diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index ec5d165e..76162dcc 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -22,6 +22,8 @@ from pandas._libs.tslibs import BaseOffset from pandas._libs.tslibs.offsets import DateOffset +from tests import pytest_warns_bounded + if TYPE_CHECKING: from pandas._typing import FulldatetimeDict else: @@ -361,7 +363,13 @@ def test_series_dt_accessors() -> None: check(assert_type(s0.dt.freq, Optional[str]), str) check(assert_type(s0.dt.isocalendar(), pd.DataFrame), pd.DataFrame) check(assert_type(s0.dt.to_period("D"), "PeriodSeries"), pd.Series, pd.Period) - check(assert_type(s0.dt.to_pydatetime(), np.ndarray), np.ndarray, dt.datetime) + + with pytest_warns_bounded( + FutureWarning, + match="The behavior of DatetimeProperties.to_pydatetime is deprecated", + lower="2.0.99", + ): + check(assert_type(s0.dt.to_pydatetime(), np.ndarray), np.ndarray, dt.datetime) s0_local = s0.dt.tz_localize("UTC") check( assert_type(s0_local, "TimestampSeries"),