From 911dc2e7dd6e80e393a77236be76822596e62fa8 Mon Sep 17 00:00:00 2001 From: andrewpmk Date: Fri, 20 Oct 2023 20:57:24 +0000 Subject: [PATCH 1/4] Add types for pandas.DataFrame.map --- pandas-stubs/core/frame.pyi | 5 +++++ tests/test_frame.py | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 2d6292faf..2b75fcc18 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -1316,9 +1316,14 @@ class DataFrame(NDFrame, OpsMixin): ) -> DataFrame: ... # Add spacing between apply() overloads and remaining annotations + # Deprecated in pandas 2.1.0 and later def applymap( self, func: Callable, na_action: Literal["ignore"] | None = ..., **kwargs ) -> DataFrame: ... + # In pandas 2.1.0 and later: applymap is renamed map, applymap is deprecated + def map( + self, func: Callable, na_action: Literal["ignore"] | None = ..., **kwargs + ) -> DataFrame: ... def join( self, other: DataFrame | Series | list[DataFrame | Series], diff --git a/tests/test_frame.py b/tests/test_frame.py index c6bb280b9..8880a18e0 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -728,6 +728,7 @@ def gethead(s: pd.Series, y: int) -> pd.Series: ) +# Deprecated in pandas 2.1.0 def test_types_applymap() -> None: with pytest_warns_bounded( FutureWarning, "DataFrame.applymap has been deprecated", lower="2.0.99" @@ -741,6 +742,17 @@ def test_types_applymap() -> None: df.applymap(str, na_action=None) +# Added in pandas 2.1.0 +def test_types_map() -> None: + df = pd.DataFrame(data={"col1": [2, 1], "col2": [3, 4]}) + df.map(lambda x: x**2) + df.map(np.exp) + df.map(str) + # na_action parameter was added in 1.2.0 https://pandas.pydata.org/docs/whatsnew/v1.2.0.html + df.map(np.exp, na_action="ignore") + df.map(str, na_action=None) + + def test_types_element_wise_arithmetic() -> None: df = pd.DataFrame(data={"col1": [2, 1], "col2": [3, 4]}) df2 = pd.DataFrame(data={"col1": [10, 20], "col3": [3, 4]}) From 463ec53a84ac539bd2b86da82b22eecb09f7d5fb Mon Sep 17 00:00:00 2001 From: Andrew MacKinnon Date: Fri, 20 Oct 2023 22:34:17 -0400 Subject: [PATCH 2/4] Made some changes: - Removed stub and test for applymap - Downgraded pyright - Ran the precommit (it just removes some whitespace) --- pandas-stubs/core/frame.pyi | 6 +----- pyproject.toml | 2 +- tests/test_frame.py | 16 +--------------- 3 files changed, 3 insertions(+), 21 deletions(-) diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 2b75fcc18..f357d6729 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -1316,14 +1316,10 @@ class DataFrame(NDFrame, OpsMixin): ) -> DataFrame: ... # Add spacing between apply() overloads and remaining annotations - # Deprecated in pandas 2.1.0 and later - def applymap( - self, func: Callable, na_action: Literal["ignore"] | None = ..., **kwargs - ) -> DataFrame: ... # In pandas 2.1.0 and later: applymap is renamed map, applymap is deprecated def map( self, func: Callable, na_action: Literal["ignore"] | None = ..., **kwargs - ) -> DataFrame: ... + ) -> DataFrame: ... def join( self, other: DataFrame | Series | list[DataFrame | Series], diff --git a/pyproject.toml b/pyproject.toml index 22bd97607..fa9326d53 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,7 +39,7 @@ mypy = "1.6.0" pandas = "2.1.1" pyarrow = ">=10.0.1" pytest = ">=7.1.2" -pyright = ">=1.1.331" +pyright = "==1.1.331" poethepoet = ">=0.16.5" loguru = ">=0.6.0" typing-extensions = ">=4.4.0" diff --git a/tests/test_frame.py b/tests/test_frame.py index 8880a18e0..150d87ae0 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -728,21 +728,7 @@ def gethead(s: pd.Series, y: int) -> pd.Series: ) -# Deprecated in pandas 2.1.0 -def test_types_applymap() -> 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) - - -# Added in pandas 2.1.0 +# In pandas 2.1.0 and later: applymap is renamed map, applymap is deprecated def test_types_map() -> None: df = pd.DataFrame(data={"col1": [2, 1], "col2": [3, 4]}) df.map(lambda x: x**2) From 0460f09ff89cc083dc1a3a16e07baae74bc047a2 Mon Sep 17 00:00:00 2001 From: Andrew MacKinnon Date: Sat, 21 Oct 2023 15:37:32 -0400 Subject: [PATCH 3/4] Remove comment referencing deprecated applymap --- pandas-stubs/core/frame.pyi | 1 - tests/test_frame.py | 1 - 2 files changed, 2 deletions(-) diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index f357d6729..ef52cdaf9 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -1316,7 +1316,6 @@ class DataFrame(NDFrame, OpsMixin): ) -> DataFrame: ... # Add spacing between apply() overloads and remaining annotations - # In pandas 2.1.0 and later: applymap is renamed map, applymap is deprecated def map( self, func: Callable, na_action: Literal["ignore"] | None = ..., **kwargs ) -> DataFrame: ... diff --git a/tests/test_frame.py b/tests/test_frame.py index 150d87ae0..20ada66ef 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -728,7 +728,6 @@ def gethead(s: pd.Series, y: int) -> pd.Series: ) -# In pandas 2.1.0 and later: applymap is renamed map, applymap is deprecated def test_types_map() -> None: df = pd.DataFrame(data={"col1": [2, 1], "col2": [3, 4]}) df.map(lambda x: x**2) From 4e80da252f560f1d6745b398d102aa5fab05942f Mon Sep 17 00:00:00 2001 From: Andrew MacKinnon Date: Sat, 21 Oct 2023 16:47:58 -0400 Subject: [PATCH 4/4] add GH774 comment --- tests/test_frame.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_frame.py b/tests/test_frame.py index 20ada66ef..be7e9fd9f 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -729,6 +729,7 @@ def gethead(s: pd.Series, y: int) -> pd.Series: def test_types_map() -> None: + # GH774 df = pd.DataFrame(data={"col1": [2, 1], "col2": [3, 4]}) df.map(lambda x: x**2) df.map(np.exp)