-
Notifications
You must be signed in to change notification settings - Fork 367
Fix the input check in groupby. #1824
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
HyukjinKwon
merged 3 commits into
databricks:master
from
ueshin:check_non-string_names/groupby
Oct 7, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -119,8 +119,8 @@ def test_groupby_simple(self): | |
| self.assertRaises(ValueError, lambda: kdf.groupby("a", as_index=False)["a"]) | ||
| self.assertRaises(ValueError, lambda: kdf.groupby("a", as_index=False)[["a"]]) | ||
| self.assertRaises(ValueError, lambda: kdf.groupby("a", as_index=False)[["a", "c"]]) | ||
| self.assertRaises(ValueError, lambda: kdf.groupby(0, as_index=False)[["a", "c"]]) | ||
| self.assertRaises(KeyError, lambda: kdf.groupby([0], as_index=False)[["a", "c"]]) | ||
| self.assertRaises(KeyError, lambda: kdf.groupby("z", as_index=False)[["a", "c"]]) | ||
| self.assertRaises(KeyError, lambda: kdf.groupby(["z"], as_index=False)[["a", "c"]]) | ||
|
|
||
| self.assertRaises(TypeError, lambda: kdf.a.groupby(kdf.b, as_index=False)) | ||
|
|
||
|
|
@@ -139,53 +139,84 @@ def test_groupby_simple(self): | |
| self.assertRaises(ValueError, lambda: kdf.a.groupby(kdf)) | ||
| self.assertRaises(ValueError, lambda: kdf.a.groupby((kdf,))) | ||
|
|
||
| # non-string names | ||
| pdf = pd.DataFrame( | ||
| { | ||
| 10: [1, 2, 6, 4, 4, 6, 4, 3, 7], | ||
| 20: [4, 2, 7, 3, 3, 1, 1, 1, 2], | ||
| 30: [4, 2, 7, 3, None, 1, 1, 1, 2], | ||
| 40: list("abcdefght"), | ||
| }, | ||
| index=[0, 1, 3, 5, 6, 8, 9, 9, 9], | ||
| ) | ||
| kdf = ks.from_pandas(pdf) | ||
|
|
||
| for as_index in [True, False]: | ||
| if as_index: | ||
| sort = lambda df: df.sort_index() | ||
| else: | ||
| sort = lambda df: df.sort_values(10).reset_index(drop=True) | ||
| self.assert_eq( | ||
| sort(kdf.groupby(10, as_index=as_index).sum()), | ||
| sort(pdf.groupby(10, as_index=as_index).sum()), | ||
| ) | ||
| self.assert_eq( | ||
| sort(kdf.groupby(10, as_index=as_index)[20].sum()), | ||
| sort(pdf.groupby(10, as_index=as_index)[20].sum()), | ||
| ) | ||
| self.assert_eq( | ||
| sort(kdf.groupby(10, as_index=as_index)[[20, 30]].sum()), | ||
| sort(pdf.groupby(10, as_index=as_index)[[20, 30]].sum()), | ||
| ) | ||
|
|
||
| def test_groupby_multiindex_columns(self): | ||
| pdf = pd.DataFrame( | ||
| { | ||
| ("x", "a"): [1, 2, 6, 4, 4, 6, 4, 3, 7], | ||
| ("x", "b"): [4, 2, 7, 3, 3, 1, 1, 1, 2], | ||
| ("y", "c"): [4, 2, 7, 3, None, 1, 1, 1, 2], | ||
| ("z", "d"): list("abcdefght"), | ||
| (10, "a"): [1, 2, 6, 4, 4, 6, 4, 3, 7], | ||
| (10, "b"): [4, 2, 7, 3, 3, 1, 1, 1, 2], | ||
| (20, "c"): [4, 2, 7, 3, None, 1, 1, 1, 2], | ||
| (30, "d"): list("abcdefght"), | ||
| }, | ||
| index=[0, 1, 3, 5, 6, 8, 9, 9, 9], | ||
| ) | ||
| kdf = ks.from_pandas(pdf) | ||
|
|
||
| self.assert_eq( | ||
| kdf.groupby(("x", "a")).sum().sort_index(), pdf.groupby(("x", "a")).sum().sort_index() | ||
| kdf.groupby((10, "a")).sum().sort_index(), pdf.groupby((10, "a")).sum().sort_index() | ||
| ) | ||
| self.assert_eq( | ||
| kdf.groupby(("x", "a"), as_index=False) | ||
| kdf.groupby((10, "a"), as_index=False) | ||
| .sum() | ||
| .sort_values(("x", "a")) | ||
| .sort_values((10, "a")) | ||
| .reset_index(drop=True), | ||
| pdf.groupby(("x", "a"), as_index=False) | ||
| pdf.groupby((10, "a"), as_index=False) | ||
| .sum() | ||
| .sort_values(("x", "a")) | ||
| .sort_values((10, "a")) | ||
| .reset_index(drop=True), | ||
| ) | ||
| self.assert_eq( | ||
| kdf.groupby(("x", "a"))[[("y", "c")]].sum().sort_index(), | ||
| pdf.groupby(("x", "a"))[[("y", "c")]].sum().sort_index(), | ||
| kdf.groupby((10, "a"))[[(20, "c")]].sum().sort_index(), | ||
| pdf.groupby((10, "a"))[[(20, "c")]].sum().sort_index(), | ||
| ) | ||
|
|
||
| # TODO: a pandas bug? | ||
| # expected = pdf.groupby((10, "a"))[(20, "c")].sum().sort_index() | ||
| expected = pd.Series( | ||
| [4.0, 2.0, 1.0, 4.0, 8.0, 2.0], | ||
| name=(20, "c"), | ||
| index=pd.Index([1, 2, 3, 4, 6, 7], name=(10, "a")), | ||
| ) | ||
| # TODO: should work? | ||
| # self.assert_eq( | ||
| # kdf.groupby(("x", "a"))[("y", "c")].sum().sort_index(), | ||
| # pdf.groupby(("x", "a"))[("y", "c")].sum().sort_index(), | ||
| # ) | ||
|
|
||
| self.assert_eq(kdf.groupby((10, "a"))[(20, "c")].sum().sort_index(), expected) | ||
|
|
||
| if LooseVersion(pd.__version__) < LooseVersion("1.1.3"): | ||
| self.assert_eq( | ||
| kdf[("x", "a")].groupby(kdf[("x", "b")]).sum().sort_index(), | ||
| pdf[("x", "a")].groupby(pdf[("x", "b")]).sum().sort_index(), | ||
| kdf[(20, "c")].groupby(kdf[(10, "a")]).sum().sort_index(), | ||
| pdf[(20, "c")].groupby(pdf[(10, "a")]).sum().sort_index(), | ||
| ) | ||
| else: | ||
| # seems like a pandas bug introduced in pandas 1.1.3. | ||
| expected_result = ks.Series( | ||
| [13, 9, 8, 1, 6], name=("x", "a"), index=pd.Index([1, 2, 3, 4, 7], name=("x", "b")) | ||
| ) | ||
| self.assert_eq( | ||
| kdf[("x", "a")].groupby(kdf[("x", "b")]).sum().sort_index(), expected_result | ||
| ) | ||
| self.assert_eq(kdf[(20, "c")].groupby(kdf[(10, "a")]).sum().sort_index(), expected) | ||
|
|
||
| def test_split_apply_combine_on_series(self): | ||
| pdf = pd.DataFrame( | ||
|
|
@@ -382,40 +413,63 @@ def test_aggregate(self): | |
| ) | ||
|
|
||
| expected_error_message = ( | ||
| r"aggs must be a dict mapping from column name \(string or " | ||
| r"tuple\) to aggregate functions \(string or list of strings\)." | ||
| r"aggs must be a dict mapping from column name to aggregate functions " | ||
| r"\(string or list of strings\)." | ||
| ) | ||
| with self.assertRaisesRegex(ValueError, expected_error_message): | ||
| kdf.groupby("A", as_index=as_index).agg(0) | ||
|
|
||
| # multi-index columns | ||
| columns = pd.MultiIndex.from_tuples([("X", "A"), ("X", "B"), ("Y", "C")]) | ||
| columns = pd.MultiIndex.from_tuples([(10, "A"), (10, "B"), (20, "C")]) | ||
| pdf.columns = columns | ||
| kdf.columns = columns | ||
|
|
||
| for as_index in [True, False]: | ||
| stats_kdf = kdf.groupby(("X", "A"), as_index=as_index).agg( | ||
| {("X", "B"): "min", ("Y", "C"): "sum"} | ||
| stats_kdf = kdf.groupby((10, "A"), as_index=as_index).agg( | ||
| {(10, "B"): "min", (20, "C"): "sum"} | ||
| ) | ||
| stats_pdf = pdf.groupby(("X", "A"), as_index=as_index).agg( | ||
| {("X", "B"): "min", ("Y", "C"): "sum"} | ||
| stats_pdf = pdf.groupby((10, "A"), as_index=as_index).agg( | ||
| {(10, "B"): "min", (20, "C"): "sum"} | ||
| ) | ||
| self.assert_eq( | ||
| stats_kdf.sort_values(by=[("X", "B"), ("Y", "C")]).reset_index(drop=True), | ||
| stats_pdf.sort_values(by=[("X", "B"), ("Y", "C")]).reset_index(drop=True), | ||
| stats_kdf.sort_values(by=[(10, "B"), (20, "C")]).reset_index(drop=True), | ||
| stats_pdf.sort_values(by=[(10, "B"), (20, "C")]).reset_index(drop=True), | ||
| ) | ||
|
|
||
| stats_kdf = kdf.groupby(("X", "A")).agg({("X", "B"): ["min", "max"], ("Y", "C"): "sum"}) | ||
| stats_pdf = pdf.groupby(("X", "A")).agg({("X", "B"): ["min", "max"], ("Y", "C"): "sum"}) | ||
| stats_kdf = kdf.groupby((10, "A")).agg({(10, "B"): ["min", "max"], (20, "C"): "sum"}) | ||
| stats_pdf = pdf.groupby((10, "A")).agg({(10, "B"): ["min", "max"], (20, "C"): "sum"}) | ||
| self.assert_eq( | ||
| stats_kdf.sort_values( | ||
| by=[("X", "B", "min"), ("X", "B", "max"), ("Y", "C", "sum")] | ||
| by=[(10, "B", "min"), (10, "B", "max"), (20, "C", "sum")] | ||
| ).reset_index(drop=True), | ||
| stats_pdf.sort_values( | ||
| by=[("X", "B", "min"), ("X", "B", "max"), ("Y", "C", "sum")] | ||
| by=[(10, "B", "min"), (10, "B", "max"), (20, "C", "sum")] | ||
| ).reset_index(drop=True), | ||
| ) | ||
|
|
||
| # non-string names | ||
| pdf.columns = [10, 20, 30] | ||
| kdf.columns = [10, 20, 30] | ||
|
|
||
| for as_index in [True, False]: | ||
| stats_kdf = kdf.groupby(10, as_index=as_index).agg({20: "min", 30: "sum"}) | ||
| stats_pdf = pdf.groupby(10, as_index=as_index).agg({20: "min", 30: "sum"}) | ||
| self.assert_eq( | ||
| stats_kdf.sort_values(by=[20, 30]).reset_index(drop=True), | ||
| stats_pdf.sort_values(by=[20, 30]).reset_index(drop=True), | ||
| ) | ||
|
|
||
| stats_kdf = kdf.groupby(10).agg({20: ["min", "max"], 30: "sum"}) | ||
| stats_pdf = pdf.groupby(10).agg({20: ["min", "max"], 30: "sum"}) | ||
| self.assert_eq( | ||
| stats_kdf.sort_values(by=[(20, "min"), (20, "max"), (30, "sum")]).reset_index( | ||
| drop=True | ||
| ), | ||
| stats_pdf.sort_values(by=[(20, "min"), (20, "max"), (30, "sum")]).reset_index( | ||
| drop=True | ||
| ), | ||
| ) | ||
|
|
||
| def test_aggregate_func_str_list(self): | ||
| # this is test for cases where only string or list is assigned | ||
| pdf = pd.DataFrame( | ||
|
|
@@ -2345,10 +2399,6 @@ def test_get_group(self): | |
| self.assertRaises( | ||
| KeyError, lambda: kdf.groupby(("B", "class"))[("A", "name")].get_group("fish") | ||
| ) | ||
| self.assertRaises( | ||
| KeyError, | ||
| lambda: kdf.groupby(("B", "class"))[("A", "name")].get_group(["bird", "mammal"]), | ||
| ) | ||
|
Comment on lines
-2348
to
-2351
Contributor
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. You removed because this is duplicated test with below ? |
||
| self.assertRaises( | ||
| KeyError, | ||
| lambda: kdf.groupby([("B", "class"), ("A", "name")]).get_group(("lion", "mammal")), | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit of nit: one more space here in the comment 😅
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway,pdf.groupby((10, "a"))[[(20, "c")]].sum()seems working.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, Sorry. nvm above. It's different from
pdf.groupby((10, "a"))[(20, "c")].sum()anyway.