-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Remove Ambiguous Behavior of Tuple as Grouping #29755
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
Changes from 6 commits
ef922f2
d078ad5
8ed6ff8
347fb47
1337aef
d937ff8
ea9011e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,10 @@ class providing the base-class of operations. | |
import re | ||
import types | ||
from typing import ( | ||
Callable, | ||
Dict, | ||
FrozenSet, | ||
Hashable, | ||
Iterable, | ||
List, | ||
Mapping, | ||
|
@@ -343,14 +345,25 @@ def _group_selection_context(groupby): | |
groupby._reset_group_selection() | ||
|
||
|
||
KeysArgType = Optional[ | ||
simonjayhawkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Union[ | ||
Hashable, | ||
List[Hashable], | ||
Callable[[Hashable], Hashable], | ||
List[Callable[[Hashable], Hashable]], | ||
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. Pretty large signature and maybe worth trimming down / cleaning up (see #22278) but documenting as-is state 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. yikes. maybe a comment pointing to the relevant issue? |
||
Mapping[Hashable, Hashable], | ||
] | ||
] | ||
|
||
|
||
class _GroupBy(PandasObject, SelectionMixin): | ||
_group_selection = None | ||
_apply_whitelist = frozenset() # type: FrozenSet[str] | ||
|
||
def __init__( | ||
self, | ||
obj: NDFrame, | ||
keys=None, | ||
keys: KeysArgType = None, | ||
axis: int = 0, | ||
level=None, | ||
grouper: "Optional[ops.BaseGrouper]" = None, | ||
|
@@ -2504,7 +2517,7 @@ def _reindex_output( | |
@Appender(GroupBy.__doc__) | ||
def get_groupby( | ||
obj: NDFrame, | ||
by=None, | ||
by: KeysArgType = None, | ||
axis: int = 0, | ||
level=None, | ||
grouper: "Optional[ops.BaseGrouper]" = None, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1734,34 +1734,23 @@ def test_empty_dataframe_groupby(): | |
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_tuple_warns(): | ||
def test_tuple_as_grouping(): | ||
# https://github.com/pandas-dev/pandas/issues/18314 | ||
df = pd.DataFrame( | ||
{ | ||
("a", "b"): [1, 1, 2, 2], | ||
"a": [1, 1, 1, 2], | ||
"b": [1, 2, 2, 2], | ||
("a", "b"): [1, 1, 1, 1], | ||
"a": [2, 2, 2, 2], | ||
"b": [2, 2, 2, 2], | ||
"c": [1, 1, 1, 1], | ||
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. why does this dataframe need to change? the rest of the test change makes sense 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. Didn't need to just figured easier to disambiguate between this and the respective a / b columns if they have entirely different values |
||
} | ||
) | ||
with tm.assert_produces_warning(FutureWarning) as w: | ||
df[["a", "b", "c"]].groupby(("a", "b")).c.mean() | ||
|
||
assert "Interpreting tuple 'by' as a list" in str(w[0].message) | ||
with pytest.raises(KeyError): | ||
df[["a", "b", "c"]].groupby(("a", "b")) | ||
|
||
with tm.assert_produces_warning(None): | ||
df.groupby(("a", "b")).c.mean() | ||
|
||
|
||
def test_tuple_warns_unhashable(): | ||
# https://github.com/pandas-dev/pandas/issues/18314 | ||
business_dates = date_range(start="4/1/2014", end="6/30/2014", freq="B") | ||
df = DataFrame(1, index=business_dates, columns=["a", "b"]) | ||
|
||
with tm.assert_produces_warning(FutureWarning) as w: | ||
df.groupby((df.index.year, df.index.month)).nth([0, 3, -1]) | ||
|
||
assert "Interpreting tuple 'by' as a list" in str(w[0].message) | ||
result = df.groupby(("a", "b"))["c"].sum() | ||
expected = pd.Series([4], name="c", index=pd.Index([1], name=("a", "b"))) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_tuple_correct_keyerror(): | ||
|
Uh oh!
There was an error while loading. Please reload this page.