From ee9e2fd043d07146013bcf140df0042f1d0f8467 Mon Sep 17 00:00:00 2001 From: Ido Ronen Date: Mon, 8 May 2023 19:10:54 +0300 Subject: [PATCH 1/7] Add test case for applying a function to a groupby object that appends each group to a list without using copy() function. The test checks if the groups are correctly appended to the list. This resolves GH issue 17718. --- pandas/tests/groupby/test_apply.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 5eaa2eb20f6bf..92783d4d87124 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -19,6 +19,26 @@ from pandas.tests.groupby import get_groupby_method_args + +def test_apply_func_that_appends_group_to_list_without_copy(): + + # GH 17718 + + df = pd.DataFrame(1, index=list(range(10))*10, columns=[0]).reset_index() + groups = [] + + def store(group): + groups.append(group) + + df.groupby('index').apply(store) + expected_value = pd.DataFrame(1, index=list(range(0,100,10)), columns=[0]).reset_index() + expected_value = expected_value.set_index('index',drop=False) + expected_value['index'] = 0 + expected_value = expected_value.rename_axis(None) + + tm.assert_frame_equal(groups[0], expected_value) + + def test_apply_issues(): # GH 5788 From 39daefa3466c6d47249bb76c1feb997da579776e Mon Sep 17 00:00:00 2001 From: Ido Ronen Date: Mon, 8 May 2023 22:25:00 +0300 Subject: [PATCH 2/7] installed pre-commit --- pandas/tests/groupby/test_apply.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 92783d4d87124..202fcbfb152a5 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -19,21 +19,21 @@ from pandas.tests.groupby import get_groupby_method_args - def test_apply_func_that_appends_group_to_list_without_copy(): - # GH 17718 - df = pd.DataFrame(1, index=list(range(10))*10, columns=[0]).reset_index() + df = DataFrame(1, index=list(range(10)) * 10, columns=[0]).reset_index() groups = [] def store(group): groups.append(group) - - df.groupby('index').apply(store) - expected_value = pd.DataFrame(1, index=list(range(0,100,10)), columns=[0]).reset_index() - expected_value = expected_value.set_index('index',drop=False) - expected_value['index'] = 0 + + df.groupby("index").apply(store) + expected_value = DataFrame( + 1, index=list(range(0, 100, 10)), columns=[0] + ).reset_index() + expected_value = expected_value.set_index("index", drop=False) + expected_value["index"] = 0 expected_value = expected_value.rename_axis(None) tm.assert_frame_equal(groups[0], expected_value) From b700c839e58f830bc635dfa65f8401182e98769f Mon Sep 17 00:00:00 2001 From: Ido Ronen Date: Tue, 9 May 2023 00:38:00 +0300 Subject: [PATCH 3/7] update expected_value --- pandas/tests/groupby/test_apply.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 202fcbfb152a5..0adc5687ed260 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -29,12 +29,7 @@ def store(group): groups.append(group) df.groupby("index").apply(store) - expected_value = DataFrame( - 1, index=list(range(0, 100, 10)), columns=[0] - ).reset_index() - expected_value = expected_value.set_index("index", drop=False) - expected_value["index"] = 0 - expected_value = expected_value.rename_axis(None) + expected_value = DataFrame({"index": [0] * 10, 0: [1] * 10}, index=np.arange(0, 100, 10)) tm.assert_frame_equal(groups[0], expected_value) From 1a0b18ac27d906e62920848f9c9a59daaa765718 Mon Sep 17 00:00:00 2001 From: Ido Ronen Date: Tue, 9 May 2023 00:52:43 +0300 Subject: [PATCH 4/7] update expected and installed pre-commit --- pandas/tests/groupby/test_apply.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 0adc5687ed260..e0d52764e0a33 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -29,7 +29,9 @@ def store(group): groups.append(group) df.groupby("index").apply(store) - expected_value = DataFrame({"index": [0] * 10, 0: [1] * 10}, index=np.arange(0, 100, 10)) + expected_value = DataFrame( + {"index": [0] * 10, 0: [1] * 10}, index=np.arange(0, 100, 10) + ) tm.assert_frame_equal(groups[0], expected_value) From 06c9aece060f43cab76a06ce65275a18facd50e7 Mon Sep 17 00:00:00 2001 From: Ido Ronen Date: Tue, 9 May 2023 01:52:54 +0300 Subject: [PATCH 5/7] test commit --- pandas/tests/groupby/test_apply.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index e0d52764e0a33..798f95dcd2b27 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -20,7 +20,7 @@ def test_apply_func_that_appends_group_to_list_without_copy(): - # GH 17718 + # GH: 17718 df = DataFrame(1, index=list(range(10)) * 10, columns=[0]).reset_index() groups = [] From 4252074239140597b8e6e9763f4b20bcac4d9157 Mon Sep 17 00:00:00 2001 From: Ido Ronen Date: Tue, 9 May 2023 02:45:39 +0300 Subject: [PATCH 6/7] committing update for expected value from my last verified commit --- pandas/tests/groupby/test_apply.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 202fcbfb152a5..e0d52764e0a33 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -30,11 +30,8 @@ def store(group): df.groupby("index").apply(store) expected_value = DataFrame( - 1, index=list(range(0, 100, 10)), columns=[0] - ).reset_index() - expected_value = expected_value.set_index("index", drop=False) - expected_value["index"] = 0 - expected_value = expected_value.rename_axis(None) + {"index": [0] * 10, 0: [1] * 10}, index=np.arange(0, 100, 10) + ) tm.assert_frame_equal(groups[0], expected_value) From fd2f721d8845a6b952ad8535510f655efd4c9ab0 Mon Sep 17 00:00:00 2001 From: Ido Ronen Date: Tue, 9 May 2023 10:35:56 +0300 Subject: [PATCH 7/7] update expected_value to pd.DataFrame({index: [0] * 10, 0: [1] * 10}, index=pd.RangeIndex(0, 100, step=10)) --- pandas/tests/groupby/test_apply.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 798f95dcd2b27..b52b708de50a6 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -30,7 +30,7 @@ def store(group): df.groupby("index").apply(store) expected_value = DataFrame( - {"index": [0] * 10, 0: [1] * 10}, index=np.arange(0, 100, 10) + {"index": [0] * 10, 0: [1] * 10}, index=pd.RangeIndex(0, 100, 10) ) tm.assert_frame_equal(groups[0], expected_value)