From 05fdeb354a54c7cb3639e04ef6941b2df9a8924f Mon Sep 17 00:00:00 2001 From: Somtochi Umeh <74836691+SomtochiUmeh@users.noreply.github.com> Date: Fri, 22 Jul 2022 10:43:22 -0500 Subject: [PATCH 1/4] Fixed metadata propagation in Dataframe.idxmax and Dataframe.idxmin #28283 --- pandas/core/frame.py | 4 ++-- pandas/tests/generic/test_finalize.py | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index e62f9fa8076d8..d6e7103a768dc 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11010,7 +11010,7 @@ def idxmin( index = data._get_axis(axis) result = [index[i] if i >= 0 else np.nan for i in indices] - return data._constructor_sliced(result, index=data._get_agg_axis(axis)) + return data._constructor_sliced(result, index=data._get_agg_axis(axis)).__finalize__(self, method="idxmin") @doc(_shared_docs["idxmax"], numeric_only_default="False") def idxmax( @@ -11035,7 +11035,7 @@ def idxmax( index = data._get_axis(axis) result = [index[i] if i >= 0 else np.nan for i in indices] - return data._constructor_sliced(result, index=data._get_agg_axis(axis)) + return data._constructor_sliced(result, index=data._get_agg_axis(axis)).__finalize__(self, method="idxmax") def _get_agg_axis(self, axis_num: int) -> Index: """ diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index 6901d415a5ae7..63f202e8e1ebf 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -227,12 +227,10 @@ (pd.DataFrame, frame_data, operator.methodcaller("nunique")), ), pytest.param( - (pd.DataFrame, frame_data, operator.methodcaller("idxmin")), - marks=not_implemented_mark, + (pd.DataFrame, frame_data, operator.methodcaller("idxmin")) ), pytest.param( - (pd.DataFrame, frame_data, operator.methodcaller("idxmax")), - marks=not_implemented_mark, + (pd.DataFrame, frame_data, operator.methodcaller("idxmax")) ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("mode")), From 06afd4fad66024c4a1e61fde414cba76fc681f27 Mon Sep 17 00:00:00 2001 From: Somtochi Umeh <74836691+SomtochiUmeh@users.noreply.github.com> Date: Fri, 22 Jul 2022 11:05:42 -0500 Subject: [PATCH 2/4] fixing PEP 8 issues --- pandas/core/frame.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index d6e7103a768dc..47203fbf315e5 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11010,7 +11010,8 @@ def idxmin( index = data._get_axis(axis) result = [index[i] if i >= 0 else np.nan for i in indices] - return data._constructor_sliced(result, index=data._get_agg_axis(axis)).__finalize__(self, method="idxmin") + final_result = data._constructor_sliced(result, index=data._get_agg_axis(axis)) + return final_result.__finalize__(self, method="idxmin") @doc(_shared_docs["idxmax"], numeric_only_default="False") def idxmax( @@ -11035,7 +11036,8 @@ def idxmax( index = data._get_axis(axis) result = [index[i] if i >= 0 else np.nan for i in indices] - return data._constructor_sliced(result, index=data._get_agg_axis(axis)).__finalize__(self, method="idxmax") + final_result = data._constructor_sliced(result, index=data._get_agg_axis(axis)) + return final_result.__finalize__(self, method="idxmax") def _get_agg_axis(self, axis_num: int) -> Index: """ From 6d7d9a9d4ffe2abe8d21050a31f2509a69defda7 Mon Sep 17 00:00:00 2001 From: Somtochi Umeh <74836691+SomtochiUmeh@users.noreply.github.com> Date: Fri, 22 Jul 2022 13:01:06 -0500 Subject: [PATCH 3/4] removing unnecessary pytest.param() --- pandas/tests/generic/test_finalize.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index 63f202e8e1ebf..3658d1a6d4ce6 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -229,12 +229,8 @@ pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("idxmin")) ), - pytest.param( - (pd.DataFrame, frame_data, operator.methodcaller("idxmax")) - ), - pytest.param( - (pd.DataFrame, frame_data, operator.methodcaller("mode")), - ), + (pd.DataFrame, frame_data, operator.methodcaller("idxmax")), + (pd.DataFrame, frame_data, operator.methodcaller("mode")), pytest.param( (pd.Series, [0], operator.methodcaller("mode")), marks=not_implemented_mark, From cfc78214908404c275d233f5cd4d58b907c71ee3 Mon Sep 17 00:00:00 2001 From: Somtochi Umeh <74836691+SomtochiUmeh@users.noreply.github.com> Date: Fri, 22 Jul 2022 13:11:29 -0500 Subject: [PATCH 4/4] removing unnecessary pytest.param --- pandas/tests/generic/test_finalize.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index 3658d1a6d4ce6..dddab05af7341 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -226,9 +226,7 @@ pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("nunique")), ), - pytest.param( - (pd.DataFrame, frame_data, operator.methodcaller("idxmin")) - ), + (pd.DataFrame, frame_data, operator.methodcaller("idxmin")), (pd.DataFrame, frame_data, operator.methodcaller("idxmax")), (pd.DataFrame, frame_data, operator.methodcaller("mode")), pytest.param(