-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
STYLE: fix pylint unnecessary-comprehension warnings #49674
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
STYLE: fix pylint unnecessary-comprehension warnings #49674
Conversation
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.
lgtm, ping on green
# calling `dict` on a DataFrameGroupBy leads to a TypeError, | ||
# we need to use a dictionary comprehension here | ||
groups = { | ||
key: gp for key, gp in grouped | ||
} # pylint: disable=unnecessary-comprehension |
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.
Related question: is it intentional that dict(df.groupby(["k1", "k2"]))
leads to a TypeError
while it's possible to iterate on it as if it was a dictionary?
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.
Not exactly intentional, but I don't think there is a good way to make it work. The dict constructor either takes a mapping or an iterable. A groupby object has all the data for a mapping, but doesn't support accessing like a map. We can however use the fact that dict takes an iterable - you should be able to use dict(grouped.items())
.
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.
dict(grouped.items())
leads to
AttributeError: 'DataFrameGroupBy' object has no attribute 'items'
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.
Ah, indeed! A couple of issues with my previous comment.
- GroupBy implements
__getitem__
, so as far as Python is concerned it is a mapping; however our implementation of this method is not in the sense of a mapping (it is for column selection) - GroupBy implements
keys
as a property; Python'sdict
construction is expectingkeys
to be a method. So when Python's dict constructor doesgrouped.keys()
, it fails. dict(iter(grouped))
works.
3a91169
to
5831cd5
Compare
* STYLE: fix pylint unnecessary-comprehension warnings * fixup! STYLE: fix pylint unnecessary-comprehension warnings * fixup! fixup! STYLE: fix pylint unnecessary-comprehension warnings
Related to #48855
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.