-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Ensure valid Block mutation in SeriesBinGrouper. #32561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jreback
merged 9 commits into
pandas-dev:master
from
TomAugspurger:31802-groupby-regression
Mar 11, 2020
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5007426
REGR: Expand ValueError catching in series aggregate
TomAugspurger ad746ba
update mgr_locs
TomAugspurger 922b30d
revert
TomAugspurger f63acd3
TST: separate out pd.crosstab tests from test_pivot (#32536)
jbrockmendel 7e49bd5
CLN: remove Categorical.put (#32554)
jbrockmendel 26ecad8
Merge remote-tracking branch 'upstream/master' into 31802-groupby-reg…
TomAugspurger cb5d20f
Merge remote-tracking branch 'upstream/master' into 31802-groupby-reg…
TomAugspurger 4649033
Merge remote-tracking branch 'upstream/master' into 31802-groupby-reg…
TomAugspurger 7060dd3
Merge remote-tracking branch 'upstream/master' into 31802-groupby-reg…
TomAugspurger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
from pandas.core.dtypes.common import ensure_int64 | ||
|
||
import pandas as pd | ||
from pandas import Index, Series, isna | ||
import pandas._testing as tm | ||
|
||
|
@@ -51,6 +52,30 @@ def test_series_bin_grouper(): | |
tm.assert_almost_equal(counts, exp_counts) | ||
|
||
|
||
def assert_block_lengths(x): | ||
assert len(x) == len(x._data.blocks[0].mgr_locs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this fails, the assertion errors bubbles up? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah diff --git a/pandas/tests/groupby/test_bin_groupby.py b/pandas/tests/groupby/test_bin_groupby.py
index 152086c241..b6518c1962 100644
--- a/pandas/tests/groupby/test_bin_groupby.py
+++ b/pandas/tests/groupby/test_bin_groupby.py
@@ -53,7 +53,7 @@ def test_series_bin_grouper():
def assert_block_lengths(x):
- assert len(x) == len(x._data.blocks[0].mgr_locs)
+ assert len(x) == len(x._data.blocks[0].mgr_locs) + 1
return 0 ___________________________________________________________________ test_mgr_locs_updated[assert_block_lengths] ____________________________________________________________________
func = <function assert_block_lengths at 0x122354b90>
@pytest.mark.parametrize("func", [cumsum_max, assert_block_lengths])
def test_mgr_locs_updated(func):
# https://github.com/pandas-dev/pandas/issues/31802
# Some operations may require creating new blocks, which requires
# valid mgr_locs
df = pd.DataFrame({"A": ["a", "a", "a"], "B": ["a", "b", "b"], "C": [1, 1, 1]})
> result = df.groupby(["A", "B"]).agg(func)
pandas/tests/groupby/test_bin_groupby.py:71:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
pandas/core/groupby/generic.py:939: in aggregate
return self._python_agg_general(func, *args, **kwargs)
pandas/core/groupby/groupby.py:926: in _python_agg_general
result, counts = self.grouper.agg_series(obj, f)
pandas/core/groupby/ops.py:640: in agg_series
return self._aggregate_series_fast(obj, func)
pandas/core/groupby/ops.py:665: in _aggregate_series_fast
result, counts = grouper.get_result()
pandas/_libs/reduction.pyx:377: in pandas._libs.reduction.SeriesGrouper.get_result
res, initialized = self._apply_to_group(cached_typ, cached_ityp,
pandas/_libs/reduction.pyx:195: in pandas._libs.reduction._BaseGrouper._apply_to_group
res = self.f(cached_typ)
pandas/core/groupby/groupby.py:913: in <lambda>
f = lambda x: func(x, *args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
x = Series([], Name: C, dtype: int64)
def assert_block_lengths(x):
> assert len(x) == len(x._data.blocks[0].mgr_locs) + 1
E assert 1 == (1 + 1)
E + where 1 = len(0 1\nName: C, dtype: int64)
E + and 1 = len(BlockPlacement(slice(0, 1, 1)))
E + where BlockPlacement(slice(0, 1, 1)) = IntBlock: 1 dtype: int64.mgr_locs
pandas/tests/groupby/test_bin_groupby.py:56: AssertionError
==================================================================== 1 failed, 1 passed, 9 deselected in 0.24s ===================================================================== |
||
return 0 | ||
|
||
|
||
def cumsum_max(x): | ||
x.cumsum().max() | ||
return 0 | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
@pytest.mark.parametrize("func", [cumsum_max, assert_block_lengths]) | ||
def test_mgr_locs_updated(func): | ||
# https://github.com/pandas-dev/pandas/issues/31802 | ||
# Some operations may require creating new blocks, which requires | ||
# valid mgr_locs | ||
df = pd.DataFrame({"A": ["a", "a", "a"], "B": ["a", "b", "b"], "C": [1, 1, 1]}) | ||
result = df.groupby(["A", "B"]).agg(func) | ||
expected = pd.DataFrame( | ||
{"C": [0, 0]}, | ||
index=pd.MultiIndex.from_product([["a"], ["a", "b"]], names=["A", "B"]), | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"binner,closed,expected", | ||
[ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.