diff --git a/tests/test_api_types.py b/tests/test_api_types.py index a6fd41b74..e06fe69d9 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,12 +55,15 @@ 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, "is_categorical_dtype is deprecated", 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: @@ -121,12 +127,15 @@ 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, "is_datetime64tz_dtype is deprecated", 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: @@ -203,12 +212,15 @@ 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, "is_int64_dtype is deprecated", 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: @@ -248,13 +260,16 @@ 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, "is_interval_dtype is deprecated", 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: @@ -327,12 +342,15 @@ def test_is_object_dtype() -> None: def test_is_period_dtype() -> None: - check(assert_type(api.is_period_dtype(arr), bool), bool) - check(assert_type(api.is_period_dtype(nparr), bool), bool) - check(assert_type(api.is_period_dtype(dtylike), bool), bool) - check(assert_type(api.is_period_dtype(dframe), bool), bool) - check(assert_type(api.is_period_dtype(ind), bool), bool) - check(assert_type(api.is_period_dtype(ExtensionDtype), bool), bool) + with pytest_warns_bounded( + FutureWarning, "is_period_dtype is deprecated", lower="2.0.99" + ): + check(assert_type(api.is_period_dtype(arr), bool), bool) + check(assert_type(api.is_period_dtype(nparr), bool), bool) + check(assert_type(api.is_period_dtype(dtylike), bool), bool) + check(assert_type(api.is_period_dtype(dframe), bool), bool) + check(assert_type(api.is_period_dtype(ind), bool), bool) + check(assert_type(api.is_period_dtype(ExtensionDtype), bool), bool) def test_is_re() -> None: @@ -378,9 +396,10 @@ def test_is_signed_integer_dtype() -> None: def test_is_sparse() -> None: - check(assert_type(api.is_sparse(arr), bool), bool) - check(assert_type(api.is_sparse(nparr), bool), bool) - check(assert_type(api.is_sparse(dframe), bool), bool) + with pytest_warns_bounded(FutureWarning, "is_sparse is deprecated", lower="2.0.99"): + check(assert_type(api.is_sparse(arr), bool), bool) + check(assert_type(api.is_sparse(nparr), bool), bool) + check(assert_type(api.is_sparse(dframe), bool), bool) def test_is_string_dtype() -> None: diff --git a/tests/test_frame.py b/tests/test_frame.py index d8e45161e..51d901fdd 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 @@ -304,7 +305,10 @@ def test_types_dropna() -> None: def test_types_fillna() -> None: df = pd.DataFrame(data={"col1": [np.nan, np.nan], "col2": [3, np.nan]}) res: pd.DataFrame = df.fillna(0) - res2: None = df.fillna(method="pad", axis=1, inplace=True) + with pytest_warns_bounded( + FutureWarning, "DataFrame.fillna with 'method' is deprecated", lower="2.0.99" + ): + res2: None = df.fillna(method="pad", axis=1, inplace=True) def test_types_sort_index() -> None: @@ -725,13 +729,16 @@ 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, "DataFrame.applymap has been deprecated", lower="2.0.99" + ): + 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) def test_types_element_wise_arithmetic() -> None: @@ -875,7 +882,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, + "The provided callable is currently using", + 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") @@ -907,32 +919,44 @@ def test_types_groupby_agg() -> None: df = pd.DataFrame( data={"col1": [1, 1, 2], "col2": [3, 4, 5], "col3": [0, 1, 0], 0: [-1, -1, -1]} ) - check(assert_type(df.groupby("col1")["col3"].agg(min), pd.Series), pd.Series) - check( - assert_type(df.groupby("col1")["col3"].agg([min, max]), pd.DataFrame), - pd.DataFrame, - ) check(assert_type(df.groupby("col1").agg("min"), pd.DataFrame), pd.DataFrame) - check(assert_type(df.groupby("col1").agg(min), pd.DataFrame), pd.DataFrame) check( assert_type(df.groupby("col1").agg(["min", "max"]), pd.DataFrame), pd.DataFrame ) - check(assert_type(df.groupby("col1").agg([min, max]), pd.DataFrame), pd.DataFrame) agg_dict1 = {"col2": "min", "col3": "max", 0: "sum"} check(assert_type(df.groupby("col1").agg(agg_dict1), pd.DataFrame), pd.DataFrame) - agg_dict2 = {"col2": min, "col3": max, 0: min} - check(assert_type(df.groupby("col1").agg(agg_dict2), pd.DataFrame), pd.DataFrame) def wrapped_min(x: Any) -> Any: return x.min() - # Here, MyPy infers dict[object, object], so it must be explicitly annotated - agg_dict3: dict[str | int, str | Callable] = { - "col2": min, - "col3": "max", - 0: wrapped_min, - } - check(assert_type(df.groupby("col1").agg(agg_dict3), pd.DataFrame), pd.DataFrame) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check(assert_type(df.groupby("col1")["col3"].agg(min), pd.Series), pd.Series) + check( + assert_type(df.groupby("col1")["col3"].agg([min, max]), pd.DataFrame), + pd.DataFrame, + ) + check(assert_type(df.groupby("col1").agg(min), pd.DataFrame), pd.DataFrame) + check( + assert_type(df.groupby("col1").agg([min, max]), pd.DataFrame), pd.DataFrame + ) + agg_dict2 = {"col2": min, "col3": max, 0: min} + check( + assert_type(df.groupby("col1").agg(agg_dict2), pd.DataFrame), pd.DataFrame + ) + + # Here, MyPy infers dict[object, object], so it must be explicitly annotated + agg_dict3: dict[str | int, str | Callable] = { + "col2": min, + "col3": "max", + 0: wrapped_min, + } + check( + assert_type(df.groupby("col1").agg(agg_dict3), pd.DataFrame), pd.DataFrame + ) agg_dict4 = {"col2": "sum"} check(assert_type(df.groupby("col1").agg(agg_dict4), pd.DataFrame), pd.DataFrame) agg_dict5 = {0: "sum"} @@ -1040,34 +1064,40 @@ def test_types_window() -> None: assert_type(df.rolling(2).agg("max"), pd.DataFrame), pd.DataFrame, ) - check( - assert_type(df.rolling(2).agg(max), pd.DataFrame), - pd.DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check( + assert_type(df.rolling(2).agg(max), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type(df.rolling(2).agg([max, min]), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type(df.rolling(2).agg({"col2": max}), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type(df.rolling(2).agg({"col2": [max, min]}), pd.DataFrame), + pd.DataFrame, + ) + check( assert_type(df.rolling(2).agg(["max", "min"]), pd.DataFrame), pd.DataFrame, ) - check( - assert_type(df.rolling(2).agg([max, min]), pd.DataFrame), - pd.DataFrame, - ) check( assert_type(df.rolling(2).agg({"col2": "max"}), pd.DataFrame), pd.DataFrame, ) - check( - assert_type(df.rolling(2).agg({"col2": max}), pd.DataFrame), - pd.DataFrame, - ) check( assert_type(df.rolling(2).agg({"col2": ["max", "min"]}), pd.DataFrame), pd.DataFrame, ) - check( - assert_type(df.rolling(2).agg({"col2": [max, min]}), pd.DataFrame), - pd.DataFrame, - ) def test_types_cov() -> None: @@ -1132,37 +1162,48 @@ def test_types_compare() -> None: def test_types_agg() -> None: df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=["A", "B", "C"]) check(assert_type(df.agg("min"), pd.Series), pd.Series) - check(assert_type(df.agg(min), pd.Series), pd.Series) check(assert_type(df.agg(["min", "max"]), pd.DataFrame), pd.DataFrame) - check(assert_type(df.agg([min, max]), pd.DataFrame), pd.DataFrame) + + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check(assert_type(df.agg(min), pd.Series), pd.Series) + check(assert_type(df.agg([min, max]), pd.DataFrame), pd.DataFrame) + check( + assert_type( + df.agg(x=("A", max), y=("B", "min"), z=("C", np.mean)), pd.DataFrame + ), + pd.DataFrame, + ) check( assert_type(df.agg({"A": ["min", "max"], "B": "min"}), pd.DataFrame), pd.DataFrame, ) - check(assert_type(df.agg({"A": [min, max], "B": min}), pd.DataFrame), pd.DataFrame) - check( - assert_type( - df.agg(x=("A", max), y=("B", "min"), z=("C", np.mean)), pd.DataFrame - ), - pd.DataFrame, - ) check(assert_type(df.agg("mean", axis=1), pd.Series), pd.Series) def test_types_aggregate() -> None: df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=["A", "B", "C"]) check(assert_type(df.aggregate("min"), pd.Series), pd.Series) - check(assert_type(df.aggregate(min), pd.Series), pd.Series) check(assert_type(df.aggregate(["min", "max"]), pd.DataFrame), pd.DataFrame) - check(assert_type(df.aggregate([min, max]), pd.DataFrame), pd.DataFrame) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check(assert_type(df.aggregate(min), pd.Series), pd.Series) + check(assert_type(df.aggregate([min, max]), pd.DataFrame), pd.DataFrame) + check( + assert_type(df.aggregate({"A": [min, max], "B": min}), pd.DataFrame), + pd.DataFrame, + ) + check( assert_type(df.aggregate({"A": ["min", "max"], "B": "min"}), pd.DataFrame), pd.DataFrame, ) - check( - assert_type(df.aggregate({"A": [min, max], "B": min}), pd.DataFrame), - pd.DataFrame, - ) def test_types_transform() -> None: @@ -1471,14 +1512,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, + "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: @@ -2081,14 +2127,21 @@ def test_groupby_result_for_ambiguous_indexes() -> None: check(assert_type(value, pd.DataFrame), pd.DataFrame) # categorical indexes are also ambiguous - categorical_index = pd.CategoricalIndex(df.a) - iterator2 = df.groupby(categorical_index).__iter__() - assert_type(iterator2, Iterator[Tuple[Any, pd.DataFrame]]) - index2, value2 = next(iterator2) - assert_type((index2, value2), Tuple[Any, pd.DataFrame]) - check(assert_type(index2, Any), int) - check(assert_type(value2, pd.DataFrame), pd.DataFrame) + # https://github.com/pandas-dev/pandas/issues/54054 needs to be fixed + with pytest_warns_bounded( + FutureWarning, + "The default of observed=False is deprecated", + lower="2.0.99", + ): + categorical_index = pd.CategoricalIndex(df.a) + iterator2 = df.groupby(categorical_index).__iter__() + assert_type(iterator2, Iterator[Tuple[Any, pd.DataFrame]]) + index2, value2 = next(iterator2) + assert_type((index2, value2), Tuple[Any, pd.DataFrame]) + + check(assert_type(index2, Any), int) + check(assert_type(value2, pd.DataFrame), pd.DataFrame) def test_setitem_list(): @@ -2465,11 +2518,16 @@ def test_getattr_and_dataframe_groupby() -> None: df = pd.DataFrame( data={"col1": [1, 1, 2], "col2": [3, 4, 5], "col3": [0, 1, 0], 0: [-1, -1, -1]} ) - check(assert_type(df.groupby("col1").col3.agg(min), pd.Series), pd.Series) - check( - assert_type(df.groupby("col1").col3.agg([min, max]), pd.DataFrame), - pd.DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check(assert_type(df.groupby("col1").col3.agg(min), pd.Series), pd.Series) + check( + assert_type(df.groupby("col1").col3.agg([min, max]), pd.DataFrame), + pd.DataFrame, + ) def test_getsetitem_multiindex() -> None: diff --git a/tests/test_io.py b/tests/test_io.py index 7de860f6f..3832f705e 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -474,37 +474,45 @@ def test_json_series(): check(assert_type(s.to_json(path), None), type(None)) check(assert_type(read_json(path, typ="series"), Series), Series) check(assert_type(DF.to_json(), str), str) - check( - assert_type( - read_json(s.to_json(orient=None), typ="series", orient=None), Series - ), - Series, - ) - check( - assert_type( - read_json(s.to_json(orient="split"), typ="series", orient="split"), Series - ), - Series, - ) - check( - assert_type( - read_json(s.to_json(orient="records"), typ="series", orient="records"), + with pytest_warns_bounded( + FutureWarning, + "Passing literal json to 'read_json' is deprecated ", + lower="2.0.99", + ): + check( + assert_type( + read_json(s.to_json(orient=None), typ="series", orient=None), Series + ), Series, - ), - Series, - ) - check( - assert_type( - read_json(s.to_json(orient="index"), typ="series", orient="index"), Series - ), - Series, - ) - check( - assert_type( - read_json(s.to_json(orient="table"), typ="series", orient="table"), Series - ), - Series, - ) + ) + check( + assert_type( + read_json(s.to_json(orient="split"), typ="series", orient="split"), + Series, + ), + Series, + ) + check( + assert_type( + read_json(s.to_json(orient="records"), typ="series", orient="records"), + Series, + ), + Series, + ) + check( + assert_type( + read_json(s.to_json(orient="index"), typ="series", orient="index"), + Series, + ), + Series, + ) + check( + assert_type( + read_json(s.to_json(orient="table"), typ="series", orient="table"), + Series, + ), + Series, + ) def test_json_chunk(): @@ -981,7 +989,12 @@ def test_read_excel_io_types() -> None: check(assert_type(pd.read_excel(as_file), pd.DataFrame), pd.DataFrame) as_bytes = as_path.read_bytes() - check(assert_type(pd.read_excel(as_bytes), pd.DataFrame), pd.DataFrame) + with pytest_warns_bounded( + FutureWarning, + "Passing bytes to 'read_excel' is deprecated", + lower="2.0.99", + ): + check(assert_type(pd.read_excel(as_bytes), pd.DataFrame), pd.DataFrame) def test_read_excel_basic(): diff --git a/tests/test_pandas.py b/tests/test_pandas.py index 948b84731..3d1426223 100644 --- a/tests/test_pandas.py +++ b/tests/test_pandas.py @@ -21,8 +21,10 @@ from pandas._typing import Scalar from tests import ( + PD_LTE_20, TYPE_CHECKING_INVALID_USAGE, check, + pytest_warns_bounded, ) @@ -412,7 +414,13 @@ def test_unique() -> None: pd.DatetimeIndex, ) - check(assert_type(pd.unique(list("baabc")), np.ndarray), np.ndarray) + with pytest_warns_bounded( + FutureWarning, + "unique with argument that is not not a Series, Index, ExtensionArray, " + "or np.ndarray is deprecated", + lower="2.0.99", + ): + check(assert_type(pd.unique(list("baabc")), np.ndarray), np.ndarray) check( assert_type( @@ -439,12 +447,18 @@ def test_unique() -> None: ), pd.Categorical, ) - check( - assert_type( - pd.unique([("a", "b"), ("b", "a"), ("a", "c"), ("b", "a")]), np.ndarray - ), - np.ndarray, - ) + with pytest_warns_bounded( + FutureWarning, + "unique with argument that is not not a Series, Index, ExtensionArray, " + "or np.ndarray is deprecated", + lower="2.0.99", + ): + check( + assert_type( + pd.unique([("a", "b"), ("b", "a"), ("a", "c"), ("b", "a")]), np.ndarray + ), + np.ndarray, + ) check( assert_type(pd.unique(pd.Index(["a", "b", "c", "a"])), np.ndarray), np.ndarray, @@ -752,9 +766,15 @@ def test_lreshape() -> None: def test_factorize() -> None: - codes, uniques = pd.factorize(["b", "b", "a", "c", "b"]) - check(assert_type(codes, np.ndarray), np.ndarray) - check(assert_type(uniques, np.ndarray), np.ndarray) + with pytest_warns_bounded( + FutureWarning, + "factorize with argument that is not not a Series, Index, ExtensionArray, " + "or np.ndarray is deprecated", + lower="2.0.99", + ): + codes, uniques = pd.factorize(["b", "b", "a", "c", "b"]) + check(assert_type(codes, np.ndarray), np.ndarray) + check(assert_type(uniques, np.ndarray), np.ndarray) codes, uniques = pd.factorize(np.recarray((1,), dtype=[("x", int)])) check(assert_type(codes, np.ndarray), np.ndarray) @@ -772,15 +792,21 @@ def test_factorize() -> None: check(assert_type(codes, np.ndarray), np.ndarray) check(assert_type(idx_uniques, pd.Index), pd.Index) - codes, uniques = pd.factorize("bbacb") - check(assert_type(codes, np.ndarray), np.ndarray) - check(assert_type(uniques, np.ndarray), np.ndarray) - - codes, uniques = pd.factorize( - ["b", "b", "a", "c", "b"], use_na_sentinel=True, size_hint=10 - ) - check(assert_type(codes, np.ndarray), np.ndarray) - check(assert_type(uniques, np.ndarray), np.ndarray) + with pytest_warns_bounded( + FutureWarning, + "factorize with argument that is not not a Series, Index, ExtensionArray, " + "or np.ndarray is deprecated", + lower="2.0.99", + ): + codes, uniques = pd.factorize("bbacb") + check(assert_type(codes, np.ndarray), np.ndarray) + check(assert_type(uniques, np.ndarray), np.ndarray) + + codes, uniques = pd.factorize( + ["b", "b", "a", "c", "b"], use_na_sentinel=True, size_hint=10 + ) + check(assert_type(codes, np.ndarray), np.ndarray) + check(assert_type(uniques, np.ndarray), np.ndarray) def test_index_unqiue() -> None: @@ -1004,8 +1030,10 @@ def test_merge() -> None: ), pd.DataFrame, ) - # TODO: When cross don't need on?? - check(assert_type(pd.merge(ls, rs, how="cross"), pd.DataFrame), pd.DataFrame) + if PD_LTE_20: + # https://github.com/pandas-dev/pandas/issues/54055 needs to be fixed + # TODO: When cross don't need on?? + check(assert_type(pd.merge(ls, rs, how="cross"), pd.DataFrame), pd.DataFrame) check( assert_type( pd.merge(ls, rs, how="inner", left_index=True, right_index=True), @@ -1462,38 +1490,45 @@ def test_crosstab_args() -> None: pd.DataFrame, ) values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] - check( - assert_type(pd.crosstab(a, b, values=values, aggfunc=np.sum), pd.DataFrame), - pd.DataFrame, - ) check( assert_type(pd.crosstab(a, b, values=values, aggfunc="sum"), pd.DataFrame), pd.DataFrame, ) - check( - assert_type( - pd.crosstab(a, b, values=pd.Index(values), aggfunc=np.sum), pd.DataFrame - ), - pd.DataFrame, - ) - check( assert_type( pd.crosstab(a, b, values=np.array(values), aggfunc="var"), pd.DataFrame ), pd.DataFrame, ) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check( + assert_type(pd.crosstab(a, b, values=values, aggfunc=np.sum), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type( + pd.crosstab(a, b, values=pd.Index(values), aggfunc=np.sum), pd.DataFrame + ), + pd.DataFrame, + ) - check( - assert_type( - pd.crosstab(a, b, values=pd.Series(values), aggfunc=np.sum), pd.DataFrame - ), - pd.DataFrame, - ) - check( - assert_type(pd.crosstab(a, b, values=values, aggfunc=np.mean), pd.DataFrame), - pd.DataFrame, - ) + check( + assert_type( + pd.crosstab(a, b, values=pd.Series(values), aggfunc=np.sum), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.crosstab(a, b, values=values, aggfunc=np.mean), pd.DataFrame + ), + pd.DataFrame, + ) check( assert_type(pd.crosstab(a, b, values=values, aggfunc="mean"), pd.DataFrame), pd.DataFrame, @@ -1693,15 +1728,20 @@ def test_pivot_table() -> None: ], } ) - check( - assert_type( - pd.pivot_table( - df, values="D", index=["A", "B"], columns=["C"], aggfunc=np.sum + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check( + assert_type( + pd.pivot_table( + df, values="D", index=["A", "B"], columns=["C"], aggfunc=np.sum + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) + ) check( assert_type( pd.pivot_table( @@ -1724,32 +1764,37 @@ def test_pivot_table() -> None: ), pd.DataFrame, ) - check( - assert_type( - pd.pivot_table( - df, - values="D", - index=["A", "B"], - columns=[(7, "seven")], - aggfunc=np.sum, + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check( + assert_type( + pd.pivot_table( + df, + values="D", + index=["A", "B"], + columns=[(7, "seven")], + aggfunc=np.sum, + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.pivot_table( - df, - values="D", - index=[("col5",), ("col6", 6)], - columns=[(7, "seven")], - aggfunc=np.sum, + ) + check( + assert_type( + pd.pivot_table( + df, + values="D", + index=[("col5",), ("col6", 6)], + columns=[(7, "seven")], + aggfunc=np.sum, + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) + ) check( assert_type( pd.pivot_table( @@ -1787,15 +1832,24 @@ def g(x: pd.Series) -> int: ), pd.DataFrame, ) - check( - assert_type( - pd.pivot_table( - df, values="D", index=["A", "B"], columns=["C"], aggfunc={"D": np.sum} + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check( + assert_type( + pd.pivot_table( + df, + values="D", + index=["A", "B"], + columns=["C"], + aggfunc={"D": np.sum}, + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) + ) check( assert_type( pd.pivot_table( @@ -1805,90 +1859,95 @@ def g(x: pd.Series) -> int: ), pd.DataFrame, ) - check( - assert_type( - pd.pivot_table( - df, - values="D", - index=["A", "B"], - columns=["C"], - aggfunc=[f, np.sum, "sum"], + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check( + assert_type( + pd.pivot_table( + df, + values="D", + index=["A", "B"], + columns=["C"], + aggfunc=[f, np.sum, "sum"], + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.pivot_table( - df, - values="D", - index=["A", "B"], - columns=["C"], - aggfunc=np.sum, - margins=True, - margins_name="Total", + ) + check( + assert_type( + pd.pivot_table( + df, + values="D", + index=["A", "B"], + columns=["C"], + aggfunc=np.sum, + margins=True, + margins_name="Total", + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.pivot_table( - df, - values="D", - index=["A", "B"], - columns=["C"], - aggfunc=np.sum, - dropna=True, + ) + check( + assert_type( + pd.pivot_table( + df, + values="D", + index=["A", "B"], + columns=["C"], + aggfunc=np.sum, + dropna=True, + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.pivot_table( - df, - values="D", - index=["A", "B"], - columns=["C"], - aggfunc=np.sum, - dropna=True, + ) + check( + assert_type( + pd.pivot_table( + df, + values="D", + index=["A", "B"], + columns=["C"], + aggfunc=np.sum, + dropna=True, + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.pivot_table( - df, - values="D", - index=["A", "B"], - columns=["C"], - aggfunc=np.sum, - observed=True, + ) + check( + assert_type( + pd.pivot_table( + df, + values="D", + index=["A", "B"], + columns=["C"], + aggfunc=np.sum, + observed=True, + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.pivot_table( - df, - values="D", - index=["A", "B"], - columns=["C"], - aggfunc=np.sum, - sort=False, + ) + check( + assert_type( + pd.pivot_table( + df, + values="D", + index=["A", "B"], + columns=["C"], + aggfunc=np.sum, + sort=False, + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) + ) idx = pd.DatetimeIndex( ["2011-01-01", "2011-02-01", "2011-01-02", "2011-01-01", "2011-01-02"] diff --git a/tests/test_resampler.py b/tests/test_resampler.py index 313599a01..6b192b60a 100644 --- a/tests/test_resampler.py +++ b/tests/test_resampler.py @@ -20,7 +20,10 @@ from pandas._typing import Scalar -from tests import check +from tests import ( + check, + pytest_warns_bounded, +) DR = date_range("1999-1-1", periods=365, freq="D") DF_ = DataFrame(np.random.standard_normal((365, 1)), index=DR) @@ -83,41 +86,52 @@ def test_filling() -> None: def test_fillna() -> None: - check(assert_type(DF.resample("m").fillna("pad"), DataFrame), DataFrame) - check(assert_type(DF.resample("m").fillna("backfill"), DataFrame), DataFrame) - check(assert_type(DF.resample("m").fillna("ffill"), DataFrame), DataFrame) - check(assert_type(DF.resample("m").fillna("bfill"), DataFrame), DataFrame) - check( - assert_type(DF.resample("m").fillna("nearest", limit=2), DataFrame), DataFrame - ) + with pytest_warns_bounded( + FutureWarning, + "DatetimeIndexResampler.fillna is deprecated ", + lower="2.0.99", + ): + check(assert_type(DF.resample("m").fillna("pad"), DataFrame), DataFrame) + check(assert_type(DF.resample("m").fillna("backfill"), DataFrame), DataFrame) + check(assert_type(DF.resample("m").fillna("ffill"), DataFrame), DataFrame) + check(assert_type(DF.resample("m").fillna("bfill"), DataFrame), DataFrame) + check( + assert_type(DF.resample("m").fillna("nearest", limit=2), DataFrame), + DataFrame, + ) def test_aggregate() -> None: - check(assert_type(DF.resample("m").aggregate(np.sum), _AggRetType), DataFrame) - check(assert_type(DF.resample("m").agg(np.sum), _AggRetType), DataFrame) - check(assert_type(DF.resample("m").apply(np.sum), _AggRetType), DataFrame) - check( - assert_type(DF.resample("m").aggregate([np.sum, np.mean]), _AggRetType), - DataFrame, - ) - check( - assert_type(DF.resample("m").aggregate(["sum", np.mean]), _AggRetType), - DataFrame, - ) - check( - assert_type( - DF.resample("m").aggregate({"col1": "sum", "col2": np.mean}), - _AggRetType, - ), - DataFrame, - ) - check( - assert_type( - DF.resample("m").aggregate({"col1": ["sum", np.mean], "col2": np.mean}), - _AggRetType, - ), - DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using ", + lower="2.0.99", + ): + check(assert_type(DF.resample("m").aggregate(np.sum), _AggRetType), DataFrame) + check(assert_type(DF.resample("m").agg(np.sum), _AggRetType), DataFrame) + check(assert_type(DF.resample("m").apply(np.sum), _AggRetType), DataFrame) + check( + assert_type(DF.resample("m").aggregate([np.sum, np.mean]), _AggRetType), + DataFrame, + ) + check( + assert_type(DF.resample("m").aggregate(["sum", np.mean]), _AggRetType), + DataFrame, + ) + check( + assert_type( + DF.resample("m").aggregate({"col1": "sum", "col2": np.mean}), + _AggRetType, + ), + DataFrame, + ) + check( + assert_type( + DF.resample("m").aggregate({"col1": ["sum", np.mean], "col2": np.mean}), + _AggRetType, + ), + DataFrame, + ) def f(val: DataFrame) -> Series: return val.mean() @@ -218,32 +232,42 @@ def test_filling_series() -> None: def test_fillna_series() -> None: - check(assert_type(S.resample("m").fillna("pad"), Series), Series) - check(assert_type(S.resample("m").fillna("backfill"), Series), Series) - check(assert_type(S.resample("m").fillna("ffill"), Series), Series) - check(assert_type(S.resample("m").fillna("bfill"), Series), Series) - check(assert_type(S.resample("m").fillna("nearest", limit=2), Series), Series) + with pytest_warns_bounded( + FutureWarning, + "DatetimeIndexResampler.fillna is deprecated ", + lower="2.0.99", + ): + check(assert_type(S.resample("m").fillna("pad"), Series), Series) + check(assert_type(S.resample("m").fillna("backfill"), Series), Series) + check(assert_type(S.resample("m").fillna("ffill"), Series), Series) + check(assert_type(S.resample("m").fillna("bfill"), Series), Series) + check(assert_type(S.resample("m").fillna("nearest", limit=2), Series), Series) def test_aggregate_series() -> None: - check(assert_type(S.resample("m").aggregate(np.sum), _AggRetType), Series) - check(assert_type(S.resample("m").agg(np.sum), _AggRetType), Series) - check(assert_type(S.resample("m").apply(np.sum), _AggRetType), Series) - check( - assert_type(S.resample("m").aggregate([np.sum, np.mean]), _AggRetType), - DataFrame, - ) - check( - assert_type(S.resample("m").aggregate(["sum", np.mean]), _AggRetType), - DataFrame, - ) - check( - assert_type( - S.resample("m").aggregate({"col1": "sum", "col2": np.mean}), - _AggRetType, - ), - DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using ", + lower="2.0.99", + ): + check(assert_type(S.resample("m").aggregate(np.sum), _AggRetType), Series) + check(assert_type(S.resample("m").agg(np.sum), _AggRetType), Series) + check(assert_type(S.resample("m").apply(np.sum), _AggRetType), Series) + check( + assert_type(S.resample("m").aggregate([np.sum, np.mean]), _AggRetType), + DataFrame, + ) + check( + assert_type(S.resample("m").aggregate(["sum", np.mean]), _AggRetType), + DataFrame, + ) + check( + assert_type( + S.resample("m").aggregate({"col1": "sum", "col2": np.mean}), + _AggRetType, + ), + DataFrame, + ) def f(val: Series) -> float: return val.mean() @@ -295,14 +319,19 @@ def s2series(val: Series) -> Series: def s2scalar(val: Series) -> float: return float(val.mean()) - check(S.resample("m").aggregate(np.sum), Series) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using ", + lower="2.0.99", + ): + check(S.resample("m").aggregate(np.sum), Series) + check(S.resample("m").aggregate([np.mean]), DataFrame) + check(S.resample("m").aggregate(["sum", np.mean]), DataFrame) + check(S.resample("m").aggregate({"sum": np.sum}), DataFrame) + check(S.resample("m").aggregate({"sum": np.sum, "mean": np.mean}), DataFrame) check(S.resample("m").aggregate("sum"), Series) check(S.resample("m").aggregate(s2series), Series) check(S.resample("m").aggregate(s2scalar), Series) - check(S.resample("m").aggregate([np.mean]), DataFrame) - check(S.resample("m").aggregate(["sum", np.mean]), DataFrame) - check(S.resample("m").aggregate({"sum": np.sum}), DataFrame) - check(S.resample("m").aggregate({"sum": np.sum, "mean": np.mean}), DataFrame) def test_aggregate_frame_combinations() -> None: @@ -315,21 +344,27 @@ def df2series(val: DataFrame) -> Series: def df2scalar(val: DataFrame) -> float: return float(val.mean().mean()) - check(DF.resample("m").aggregate(np.sum), DataFrame) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using ", + lower="2.0.99", + ): + check(DF.resample("m").aggregate(np.sum), DataFrame) + check(DF.resample("m").aggregate([np.mean]), DataFrame) + check(DF.resample("m").aggregate(["sum", np.mean]), DataFrame) + check(DF.resample("m").aggregate({"col1": np.sum}), DataFrame) + check(DF.resample("m").aggregate({"col1": np.sum, "col2": np.mean}), DataFrame) + check( + DF.resample("m").aggregate({"col1": [np.sum], "col2": ["sum", np.mean]}), + DataFrame, + ) + check( + DF.resample("m").aggregate({"col1": np.sum, "col2": ["sum", np.mean]}), + DataFrame, + ) + check(DF.resample("m").aggregate({"col1": "sum", "col2": [np.mean]}), DataFrame) + check(DF.resample("m").aggregate("sum"), DataFrame) check(DF.resample("m").aggregate(df2frame), DataFrame) check(DF.resample("m").aggregate(df2series), DataFrame) check(DF.resample("m").aggregate(df2scalar), DataFrame) - check(DF.resample("m").aggregate([np.mean]), DataFrame) - check(DF.resample("m").aggregate(["sum", np.mean]), DataFrame) - check(DF.resample("m").aggregate({"col1": np.sum}), DataFrame) - check(DF.resample("m").aggregate({"col1": np.sum, "col2": np.mean}), DataFrame) - check( - DF.resample("m").aggregate({"col1": [np.sum], "col2": ["sum", np.mean]}), - DataFrame, - ) - check( - DF.resample("m").aggregate({"col1": np.sum, "col2": ["sum", np.mean]}), - DataFrame, - ) - check(DF.resample("m").aggregate({"col1": "sum", "col2": [np.mean]}), DataFrame) diff --git a/tests/test_series.py b/tests/test_series.py index 3291b2930..d1eb5c7cb 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -47,6 +47,7 @@ PD_LTE_20, TYPE_CHECKING_INVALID_USAGE, check, + pytest_warns_bounded, ) from tests.extension.decimal.array import DecimalDtype @@ -119,8 +120,13 @@ def test_types_copy() -> None: def test_types_select() -> None: s = pd.Series(data={"row1": 1, "row2": 2}) - s[0] - s[1:] + with pytest_warns_bounded( + FutureWarning, + "Series.__getitem__ treating keys as positions is deprecated", + lower="2.0.99", + ): + s[0] + s[1:] def test_types_iloc_iat() -> None: @@ -247,10 +253,15 @@ def test_types_fillna() -> None: s = pd.Series([1, np.nan, np.nan, 3]) check(assert_type(s.fillna(0), pd.Series), pd.Series) check(assert_type(s.fillna(0, axis="index"), pd.Series), pd.Series) - check(assert_type(s.fillna(method="backfill", axis=0), pd.Series), pd.Series) - assert assert_type(s.fillna(method="bfill", inplace=True), None) is None - check(assert_type(s.fillna(method="pad"), pd.Series), pd.Series) - check(assert_type(s.fillna(method="ffill", limit=1), pd.Series), pd.Series) + with pytest_warns_bounded( + FutureWarning, + "Series.fillna with 'method' is deprecated", + lower="2.0.99", + ): + check(assert_type(s.fillna(method="backfill", axis=0), pd.Series), pd.Series) + assert assert_type(s.fillna(method="bfill", inplace=True), None) is None + check(assert_type(s.fillna(method="pad"), pd.Series), pd.Series) + check(assert_type(s.fillna(method="ffill", limit=1), pd.Series), pd.Series) # GH 263 check(assert_type(s.fillna(pd.NA), pd.Series), pd.Series) @@ -451,7 +462,13 @@ 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, + "Returning a DataFrame from Series.apply when the supplied function" + "returns a Series is deprecated", + lower="2.0.99", + ): + check(assert_type(s.apply(makeseries), pd.DataFrame), pd.DataFrame) # GH 293 @@ -693,38 +710,56 @@ def test_groupby_result_for_ambiguous_indexes() -> None: check(assert_type(value, "pd.Series[int]"), pd.Series, np.integer) # categorical indexes are also ambiguous - categorical_index = pd.CategoricalIndex(s.index) - iterator2 = s.groupby(categorical_index).__iter__() - assert_type(iterator2, Iterator[Tuple[Any, "pd.Series[int]"]]) - index2, value2 = next(iterator2) - assert_type((index2, value2), Tuple[Any, "pd.Series[int]"]) - - check(assert_type(index2, Any), str) - check(assert_type(value2, "pd.Series[int]"), pd.Series, np.integer) + # https://github.com/pandas-dev/pandas/issues/54054 needs to be fixed + with pytest_warns_bounded( + FutureWarning, + "The default of observed=False is deprecated", + lower="2.0.99", + ): + categorical_index = pd.CategoricalIndex(s.index) + iterator2 = s.groupby(categorical_index).__iter__() + assert_type(iterator2, Iterator[Tuple[Any, "pd.Series[int]"]]) + index2, value2 = next(iterator2) + assert_type((index2, value2), Tuple[Any, "pd.Series[int]"]) + + check(assert_type(index2, Any), str) + check(assert_type(value2, "pd.Series[int]"), pd.Series, np.integer) def test_types_groupby_agg() -> None: s = pd.Series([4, 2, 1, 8], index=["a", "b", "a", "b"]) check(assert_type(s.groupby(level=0).agg("sum"), pd.Series), pd.Series) - check(assert_type(s.groupby(level=0).agg(sum), pd.Series), pd.Series) check( assert_type(s.groupby(level=0).agg(["min", "sum"]), pd.DataFrame), pd.DataFrame ) - check(assert_type(s.groupby(level=0).agg([min, sum]), pd.DataFrame), pd.DataFrame) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check(assert_type(s.groupby(level=0).agg(sum), pd.Series), pd.Series) + check( + assert_type(s.groupby(level=0).agg([min, sum]), pd.DataFrame), pd.DataFrame + ) def test_types_groupby_aggregate() -> None: s = pd.Series([4, 2, 1, 8], index=["a", "b", "a", "b"]) check(assert_type(s.groupby(level=0).aggregate("sum"), pd.Series), pd.Series) - check(assert_type(s.groupby(level=0).aggregate(sum), pd.Series), pd.Series) check( assert_type(s.groupby(level=0).aggregate(["min", "sum"]), pd.DataFrame), pd.DataFrame, ) - check( - assert_type(s.groupby(level=0).aggregate([min, sum]), pd.DataFrame), - pd.DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check(assert_type(s.groupby(level=0).aggregate(sum), pd.Series), pd.Series) + check( + assert_type(s.groupby(level=0).aggregate([min, sum]), pd.DataFrame), + pd.DataFrame, + ) # This added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html @@ -767,18 +802,23 @@ def test_types_window() -> None: assert_type(s.rolling(2).agg("sum"), pd.Series), pd.Series, ) - check( - assert_type(s.rolling(2).agg(sum), pd.Series), - pd.Series, - ) check( assert_type(s.rolling(2).agg(["max", "min"]), pd.DataFrame), pd.DataFrame, ) - check( - assert_type(s.rolling(2).agg([max, min]), pd.DataFrame), - pd.DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check( + assert_type(s.rolling(2).agg(sum), pd.Series), + pd.Series, + ) + check( + assert_type(s.rolling(2).agg([max, min]), pd.DataFrame), + pd.DataFrame, + ) def test_types_cov() -> None: @@ -832,23 +872,33 @@ def test_types_between() -> None: def test_types_agg() -> None: s = pd.Series([1, 2, 3], index=["col1", "col2", "col3"]) check(assert_type(s.agg("min"), Any), np.int64) - check(assert_type(s.agg(min), Any), np.int64) check(assert_type(s.agg(["min", "max"]), pd.Series), pd.Series) - check(assert_type(s.agg([min, max]), pd.Series), pd.Series) check(assert_type(s.agg({"a": "min"}), pd.Series), pd.Series) - check(assert_type(s.agg({0: min}), pd.Series), pd.Series) - check(assert_type(s.agg(x=max, y="min", z=np.mean), pd.Series), pd.Series) check(assert_type(s.agg("mean", axis=0), Any), np.float64) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check(assert_type(s.agg(min), Any), np.int64) + check(assert_type(s.agg([min, max]), pd.Series), pd.Series) + check(assert_type(s.agg({0: min}), pd.Series), pd.Series) + check(assert_type(s.agg(x=max, y="min", z=np.mean), pd.Series), pd.Series) def test_types_aggregate() -> None: s = pd.Series([1, 2, 3], index=["col1", "col2", "col3"]) check(assert_type(s.aggregate("min"), Any), np.int64) - check(assert_type(s.aggregate(min), Any), np.int64) check(assert_type(s.aggregate(["min", "max"]), pd.Series), pd.Series) - check(assert_type(s.aggregate([min, max]), pd.Series), pd.Series) check(assert_type(s.aggregate({"a": "min"}), pd.Series), pd.Series) - check(assert_type(s.aggregate({0: min}), pd.Series), pd.Series) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using", + lower="2.0.99", + ): + check(assert_type(s.aggregate(min), Any), np.int64) + check(assert_type(s.aggregate([min, max]), pd.Series), pd.Series) + check(assert_type(s.aggregate({0: min}), pd.Series), pd.Series) def test_types_transform() -> None: @@ -1533,14 +1583,20 @@ def test_bitwise_operators() -> None: check(assert_type(s ^ s2, "pd.Series[int]"), pd.Series, np.integer) check(assert_type(s2 ^ s, "pd.Series[int]"), pd.Series, np.integer) - check(assert_type(s & [1, 2, 3, 4], "pd.Series[bool]"), pd.Series, np.bool_) - check(assert_type([1, 2, 3, 4] & s, "pd.Series[bool]"), pd.Series, np.bool_) + with pytest_warns_bounded( + FutureWarning, + r"Logical ops \(and, or, xor\) between Pandas objects and dtype-less sequences " + r"\(e.g. list, tuple\) are deprecated", + lower="2.0.99", + ): + check(assert_type(s & [1, 2, 3, 4], "pd.Series[bool]"), pd.Series, np.bool_) + check(assert_type([1, 2, 3, 4] & s, "pd.Series[bool]"), pd.Series, np.bool_) - check(assert_type(s | [1, 2, 3, 4], "pd.Series[bool]"), pd.Series, np.bool_) - check(assert_type([1, 2, 3, 4] | s, "pd.Series[bool]"), pd.Series, np.bool_) + check(assert_type(s | [1, 2, 3, 4], "pd.Series[bool]"), pd.Series, np.bool_) + check(assert_type([1, 2, 3, 4] | s, "pd.Series[bool]"), pd.Series, np.bool_) - check(assert_type(s ^ [1, 2, 3, 4], "pd.Series[bool]"), pd.Series, np.bool_) - check(assert_type([1, 2, 3, 4] ^ s, "pd.Series[bool]"), pd.Series, np.bool_) + check(assert_type(s ^ [1, 2, 3, 4], "pd.Series[bool]"), pd.Series, np.bool_) + check(assert_type([1, 2, 3, 4] ^ s, "pd.Series[bool]"), pd.Series, np.bool_) def test_logical_operators() -> None: @@ -1574,36 +1630,42 @@ def test_logical_operators() -> None: check(assert_type(True ^ (df["a"] >= 2), "pd.Series[bool]"), pd.Series, np.bool_) - check( - assert_type((df["a"] >= 2) ^ [True, False, True], "pd.Series[bool]"), - pd.Series, - np.bool_, - ) - check( - assert_type((df["a"] >= 2) & [True, False, True], "pd.Series[bool]"), - pd.Series, - np.bool_, - ) - check( - assert_type((df["a"] >= 2) | [True, False, True], "pd.Series[bool]"), - pd.Series, - np.bool_, - ) - check( - assert_type([True, False, True] & (df["a"] >= 2), "pd.Series[bool]"), - pd.Series, - np.bool_, - ) - check( - assert_type([True, False, True] | (df["a"] >= 2), "pd.Series[bool]"), - pd.Series, - np.bool_, - ) - check( - assert_type([True, False, True] ^ (df["a"] >= 2), "pd.Series[bool]"), - pd.Series, - np.bool_, - ) + with pytest_warns_bounded( + FutureWarning, + r"Logical ops \(and, or, xor\) between Pandas objects and dtype-less sequences " + r"\(e.g. list, tuple\) are deprecated", + lower="2.0.99", + ): + check( + assert_type((df["a"] >= 2) ^ [True, False, True], "pd.Series[bool]"), + pd.Series, + np.bool_, + ) + check( + assert_type((df["a"] >= 2) & [True, False, True], "pd.Series[bool]"), + pd.Series, + np.bool_, + ) + check( + assert_type((df["a"] >= 2) | [True, False, True], "pd.Series[bool]"), + pd.Series, + np.bool_, + ) + check( + assert_type([True, False, True] & (df["a"] >= 2), "pd.Series[bool]"), + pd.Series, + np.bool_, + ) + check( + assert_type([True, False, True] | (df["a"] >= 2), "pd.Series[bool]"), + pd.Series, + np.bool_, + ) + check( + assert_type([True, False, True] ^ (df["a"] >= 2), "pd.Series[bool]"), + pd.Series, + np.bool_, + ) def test_AnyArrayLike_and_clip() -> None: diff --git a/tests/test_styler.py b/tests/test_styler.py index 8e2ba8453..79a92cb54 100644 --- a/tests/test_styler.py +++ b/tests/test_styler.py @@ -22,7 +22,10 @@ import pytest from typing_extensions import assert_type -from tests import check +from tests import ( + check, + pytest_warns_bounded, +) from pandas.io.formats.style import Styler @@ -70,14 +73,24 @@ def test_applymap() -> None: def g(o: object) -> str: return str(o) - check(assert_type(DF.style.applymap(g), Styler), Styler) + with pytest_warns_bounded( + FutureWarning, + "Styler.applymap has been deprecated. Use Styler.map instead", + lower="2.0.99", + ): + check(assert_type(DF.style.applymap(g), Styler), Styler) def test_applymap_index() -> None: def g(o: object) -> str: return str(o) - check(assert_type(DF.style.applymap_index(g), Styler), Styler) + with pytest_warns_bounded( + FutureWarning, + "Styler.applymap_index has been deprecated. Use Styler.map_index instead", + lower="2.0.99", + ): + check(assert_type(DF.style.applymap_index(g), Styler), Styler) def test_background_gradient() -> None: diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index 34d37ac9f..e5f2a283c 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -27,8 +27,10 @@ else: FulldatetimeDict = Any from tests import ( + PD_LTE_20, TYPE_CHECKING_INVALID_USAGE, check, + pytest_warns_bounded, ) from pandas.tseries.holiday import USFederalHolidayCalendar @@ -362,7 +364,12 @@ def test_series_dt_accessors() -> None: 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, + "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"), @@ -702,7 +709,14 @@ def test_to_timdelta_units() -> None: check(assert_type(pd.to_timedelta(1, "minute"), pd.Timedelta), pd.Timedelta) check(assert_type(pd.to_timedelta(1, "min"), pd.Timedelta), pd.Timedelta) check(assert_type(pd.to_timedelta(1, "minutes"), pd.Timedelta), pd.Timedelta) - check(assert_type(pd.to_timedelta(1, "t"), pd.Timedelta), pd.Timedelta) + with pytest_warns_bounded( + FutureWarning, + r"Unit '[tl]' is deprecated", + lower="2.0.99", + ): + check(assert_type(pd.to_timedelta(1, "t"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "l"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "s"), pd.Timedelta), pd.Timedelta) check(assert_type(pd.to_timedelta(1, "seconds"), pd.Timedelta), pd.Timedelta) check(assert_type(pd.to_timedelta(1, "sec"), pd.Timedelta), pd.Timedelta) @@ -712,7 +726,6 @@ def test_to_timdelta_units() -> None: check(assert_type(pd.to_timedelta(1, "millisecond"), pd.Timedelta), pd.Timedelta) check(assert_type(pd.to_timedelta(1, "milli"), pd.Timedelta), pd.Timedelta) check(assert_type(pd.to_timedelta(1, "millis"), pd.Timedelta), pd.Timedelta) - check(assert_type(pd.to_timedelta(1, "l"), pd.Timedelta), pd.Timedelta) check(assert_type(pd.to_timedelta(1, "us"), pd.Timedelta), pd.Timedelta) check(assert_type(pd.to_timedelta(1, "microseconds"), pd.Timedelta), pd.Timedelta) check(assert_type(pd.to_timedelta(1, "microsecond"), pd.Timedelta), pd.Timedelta) @@ -1121,16 +1134,18 @@ def test_timedelta64_and_arithmatic_operator() -> None: s1 = pd.Series(data=pd.date_range("1/1/2020", "2/1/2020")) s2 = pd.Series(data=pd.date_range("1/1/2021", "2/1/2021")) s3 = s2 - s1 - td = np.timedelta64(1, "M") - check(assert_type((s1 - td), "TimestampSeries"), pd.Series, pd.Timestamp) - check(assert_type((s1 + td), "TimestampSeries"), pd.Series, pd.Timestamp) - check(assert_type((s3 - td), "TimedeltaSeries"), pd.Series, pd.Timedelta) - check(assert_type((s3 + td), "TimedeltaSeries"), pd.Series, pd.Timedelta) - check(assert_type((s3 / td), "pd.Series[float]"), pd.Series, float) - if TYPE_CHECKING_INVALID_USAGE: - r1 = s1 * td # type: ignore[operator] # pyright: ignore[reportGeneralTypeIssues] - r2 = s1 / td # type: ignore[operator] # pyright: ignore[reportGeneralTypeIssues] - r3 = s3 * td # type: ignore[operator] # pyright: ignore[reportGeneralTypeIssues] + # https://github.com/pandas-dev/pandas/issues/54059 needs to be fixed + if PD_LTE_20: + td = np.timedelta64(1, "M") + check(assert_type((s1 - td), "TimestampSeries"), pd.Series, pd.Timestamp) + check(assert_type((s1 + td), "TimestampSeries"), pd.Series, pd.Timestamp) + check(assert_type((s3 - td), "TimedeltaSeries"), pd.Series, pd.Timedelta) + check(assert_type((s3 + td), "TimedeltaSeries"), pd.Series, pd.Timedelta) + check(assert_type((s3 / td), "pd.Series[float]"), pd.Series, float) + if TYPE_CHECKING_INVALID_USAGE: + r1 = s1 * td # type: ignore[operator] # pyright: ignore[reportGeneralTypeIssues] + r2 = s1 / td # type: ignore[operator] # pyright: ignore[reportGeneralTypeIssues] + r3 = s3 * td # type: ignore[operator] # pyright: ignore[reportGeneralTypeIssues] def test_timedeltaseries_add_timestampseries() -> None: diff --git a/tests/test_windowing.py b/tests/test_windowing.py index b973ace97..2fe3ba582 100644 --- a/tests/test_windowing.py +++ b/tests/test_windowing.py @@ -14,7 +14,10 @@ ) from typing_extensions import assert_type -from tests import check +from tests import ( + check, + pytest_warns_bounded, +) from pandas.tseries.frequencies import to_offset @@ -94,19 +97,24 @@ def _mean4(df: DataFrame) -> float: def test_rolling_aggregate() -> None: - check(assert_type(DF.rolling(10).aggregate(np.mean), DataFrame), DataFrame) - check( - assert_type(DF.rolling(10).aggregate(["mean", np.mean]), DataFrame), DataFrame - ) - check( - assert_type( - DF.rolling(10).aggregate({"col1": "mean", "col2": np.mean}), DataFrame - ), - DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using ", + lower="2.0.99", + ): + check(assert_type(DF.rolling(10).aggregate(np.mean), DataFrame), DataFrame) + check( + assert_type(DF.rolling(10).aggregate(["mean", np.mean]), DataFrame), + DataFrame, + ) + check( + assert_type( + DF.rolling(10).aggregate({"col1": "mean", "col2": np.mean}), DataFrame + ), + DataFrame, + ) check(assert_type(DF.rolling(10).agg("sum"), DataFrame), DataFrame) - check(assert_type(DF.rolling(10).aggregate(np.mean), DataFrame), DataFrame) check(assert_type(DF.rolling(10).aggregate("mean"), DataFrame), DataFrame) def _mean(df: DataFrame) -> Series: @@ -114,23 +122,29 @@ def _mean(df: DataFrame) -> Series: check(assert_type(DF.rolling(10).aggregate(_mean), DataFrame), DataFrame) - check(assert_type(DF.rolling(10).aggregate([np.mean]), DataFrame), DataFrame) - check( - assert_type(DF.rolling(10).aggregate([np.mean, "mean"]), DataFrame), DataFrame - ) - check( - assert_type( - DF.rolling(10).aggregate({"col1": np.mean, "col2": "mean"}), DataFrame - ), - DataFrame, - ) - check( - assert_type( - DF.rolling(10).aggregate({"col1": [np.mean, "mean"], "col2": "mean"}), + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using ", + lower="2.0.99", + ): + check(assert_type(DF.rolling(10).aggregate([np.mean]), DataFrame), DataFrame) + check( + assert_type(DF.rolling(10).aggregate([np.mean, "mean"]), DataFrame), DataFrame, - ), - DataFrame, - ) + ) + check( + assert_type( + DF.rolling(10).aggregate({"col1": np.mean, "col2": "mean"}), DataFrame + ), + DataFrame, + ) + check( + assert_type( + DF.rolling(10).aggregate({"col1": [np.mean, "mean"], "col2": "mean"}), + DataFrame, + ), + DataFrame, + ) # func: np.ufunc | Callable | str | list[Callable | str, np.ufunc] | dict[Hashable, Callable | str | np.ufunc| list[Callable | str]] check(assert_type(DF.rolling(10).agg("sum"), DataFrame), DataFrame) @@ -171,7 +185,6 @@ def _mean2(df: Series) -> np.ndarray: def test_rolling_aggregate_series() -> None: - check(assert_type(S.rolling(10).aggregate(np.mean), Series), Series) check(assert_type(S.rolling(10).aggregate("mean"), Series), Series) def _mean(s: Series) -> float: @@ -179,15 +192,27 @@ def _mean(s: Series) -> float: check(assert_type(S.rolling(10).aggregate(_mean), Series), Series) - check(assert_type(S.rolling(10).aggregate([np.mean]), DataFrame), DataFrame) - check(assert_type(S.rolling(10).aggregate([np.mean, "mean"]), DataFrame), DataFrame) - check( - assert_type( - S.rolling(10).aggregate({"col1": np.mean, "col2": "mean", "col3": _mean}), + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using ", + lower="2.0.99", + ): + check(assert_type(S.rolling(10).aggregate(np.mean), Series), Series) + + check(assert_type(S.rolling(10).aggregate([np.mean]), DataFrame), DataFrame) + check( + assert_type(S.rolling(10).aggregate([np.mean, "mean"]), DataFrame), DataFrame, - ), - DataFrame, - ) + ) + check( + assert_type( + S.rolling(10).aggregate( + {"col1": np.mean, "col2": "mean", "col3": _mean} + ), + DataFrame, + ), + DataFrame, + ) check(assert_type(S.rolling(10).agg("sum"), Series), Series) @@ -231,16 +256,22 @@ def _mean4(df: DataFrame) -> float: def test_expanding_aggregate() -> None: - check(assert_type(DF.expanding(10).aggregate(np.mean), DataFrame), DataFrame) - check( - assert_type(DF.expanding(10).aggregate(["mean", np.mean]), DataFrame), DataFrame - ) - check( - assert_type( - DF.expanding(10).aggregate({"col1": "mean", "col2": np.mean}), DataFrame - ), - DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using ", + lower="2.0.99", + ): + check(assert_type(DF.expanding(10).aggregate(np.mean), DataFrame), DataFrame) + check( + assert_type(DF.expanding(10).aggregate(["mean", np.mean]), DataFrame), + DataFrame, + ) + check( + assert_type( + DF.expanding(10).aggregate({"col1": "mean", "col2": np.mean}), DataFrame + ), + DataFrame, + ) check(assert_type(DF.expanding(10).agg("sum"), DataFrame), DataFrame) @@ -279,16 +310,22 @@ def _mean2(df: Series) -> np.ndarray: def test_expanding_aggregate_series() -> None: - check(assert_type(S.expanding(10).aggregate(np.mean), Series), Series) - check( - assert_type(S.expanding(10).aggregate(["mean", np.mean]), DataFrame), DataFrame - ) - check( - assert_type( - S.expanding(10).aggregate({"col1": "mean", "col2": np.mean}), DataFrame - ), - DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using ", + lower="2.0.99", + ): + check(assert_type(S.expanding(10).aggregate(np.mean), Series), Series) + check( + assert_type(S.expanding(10).aggregate(["mean", np.mean]), DataFrame), + DataFrame, + ) + check( + assert_type( + S.expanding(10).aggregate({"col1": "mean", "col2": np.mean}), DataFrame + ), + DataFrame, + ) check(assert_type(S.expanding(10).agg("sum"), Series), Series) @@ -302,16 +339,22 @@ def test_ewm_basic_math() -> None: def test_ewm_aggregate() -> None: - check(assert_type(DF.ewm(span=10).aggregate(np.mean), DataFrame), DataFrame) - check( - assert_type(DF.ewm(span=10).aggregate(["mean", np.mean]), DataFrame), DataFrame - ) - check( - assert_type( - DF.ewm(span=10).aggregate({"col1": "mean", "col2": np.mean}), DataFrame - ), - DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using ", + lower="2.0.99", + ): + check(assert_type(DF.ewm(span=10).aggregate(np.mean), DataFrame), DataFrame) + check( + assert_type(DF.ewm(span=10).aggregate(["mean", np.mean]), DataFrame), + DataFrame, + ) + check( + assert_type( + DF.ewm(span=10).aggregate({"col1": "mean", "col2": np.mean}), DataFrame + ), + DataFrame, + ) check(assert_type(DF.ewm(span=10).agg("sum"), DataFrame), DataFrame) @@ -325,16 +368,22 @@ def test_ewm_basic_math_series() -> None: def test_ewm_aggregate_series() -> None: - check(assert_type(S.ewm(span=10).aggregate(np.mean), Series), Series) - check( - assert_type(S.ewm(span=10).aggregate(["mean", np.mean]), DataFrame), DataFrame - ) - check( - assert_type( - S.ewm(span=10).aggregate({"col1": "mean", "col2": np.mean}), DataFrame - ), - DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, + r"The provided callable is currently using ", + lower="2.0.99", + ): + check(assert_type(S.ewm(span=10).aggregate(np.mean), Series), Series) + check( + assert_type(S.ewm(span=10).aggregate(["mean", np.mean]), DataFrame), + DataFrame, + ) + check( + assert_type( + S.ewm(span=10).aggregate({"col1": "mean", "col2": np.mean}), DataFrame + ), + DataFrame, + ) check(assert_type(S.ewm(span=10).agg("sum"), Series), Series)