From 8846135190d9a86d8fba7123fb31fe547a8bd56e Mon Sep 17 00:00:00 2001 From: Tobias Pitters Date: Mon, 20 Apr 2020 21:44:38 +0200 Subject: [PATCH 1/3] added test for bug which appeared in v0.24.2 but is fixed in master --- pandas/tests/groupby/test_apply.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 9fcbabb07857e..ba0ce0a192fc8 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -901,3 +901,17 @@ def fn(x): name="col2", ) tm.assert_series_equal(result, expected) + + +def test_apply_reindex_values(): + # GH: 26209 + values = [1, 2, 3, 4] + indices = [1, 1, 2, 2] + df = pd.DataFrame( + {"group": ["Group1", "Group2"] * 2, "value": values}, index=indices + ) + srs_exp = pd.Series(values, index=indices, name="value") + srs_grouped = df.groupby("group").value.apply( + lambda x: x.reindex(np.arange(x.index.min(), x.index.max() + 1)) + ) + tm.assert_series_equal(srs_exp, srs_grouped) From 24f16cbe3661d637b6be21a13ab9ab54bf19c679 Mon Sep 17 00:00:00 2001 From: Tobias Pitters Date: Thu, 23 Apr 2020 23:01:42 +0200 Subject: [PATCH 2/3] moved to location of tests dealing with the same issue improved commentary --- pandas/tests/groupby/test_apply.py | 33 +++++++++++++++++------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index ba0ce0a192fc8..18bf0635e5de5 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -491,6 +491,25 @@ def test_apply_with_duplicated_non_sorted_axis(test_series): tm.assert_frame_equal(result, expected) +def test_apply_reindex_values(): + # GH: 26209 + # reindexing from a single column of a groupby object with duplicate indices caused a ValueError + # (cannot reindex from duplicate axis) in 0.24.2, the problem was solved in #30679 + values = [1, 2, 3, 4] + indices = [1, 1, 2, 2] + df = pd.DataFrame( + {"group": ["Group1", "Group2"] * 2, "value": values}, index=indices + ) + expected = pd.Series(values, index=indices, name="value") + + def reindex_helper(x): + return x.reindex(np.arange(x.index.min(), x.index.max() + 1)) + + # the following group by raised a ValueError + result = df.groupby("group").value.apply(reindex_helper) + tm.assert_series_equal(expected, result) + + def test_apply_corner_cases(): # #535, can't use sliding iterator @@ -901,17 +920,3 @@ def fn(x): name="col2", ) tm.assert_series_equal(result, expected) - - -def test_apply_reindex_values(): - # GH: 26209 - values = [1, 2, 3, 4] - indices = [1, 1, 2, 2] - df = pd.DataFrame( - {"group": ["Group1", "Group2"] * 2, "value": values}, index=indices - ) - srs_exp = pd.Series(values, index=indices, name="value") - srs_grouped = df.groupby("group").value.apply( - lambda x: x.reindex(np.arange(x.index.min(), x.index.max() + 1)) - ) - tm.assert_series_equal(srs_exp, srs_grouped) From 35502a4b06b91f31cdeea50f46f9110c744f2779 Mon Sep 17 00:00:00 2001 From: Tobias Pitters Date: Fri, 24 Apr 2020 00:17:25 +0200 Subject: [PATCH 3/3] modified comment for test_apply_reindex_values --- pandas/tests/groupby/test_apply.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 18bf0635e5de5..e2b5118922a5a 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -493,8 +493,9 @@ def test_apply_with_duplicated_non_sorted_axis(test_series): def test_apply_reindex_values(): # GH: 26209 - # reindexing from a single column of a groupby object with duplicate indices caused a ValueError - # (cannot reindex from duplicate axis) in 0.24.2, the problem was solved in #30679 + # reindexing from a single column of a groupby object with duplicate indices caused + # a ValueError (cannot reindex from duplicate axis) in 0.24.2, the problem was + # solved in #30679 values = [1, 2, 3, 4] indices = [1, 1, 2, 2] df = pd.DataFrame(