-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Allow map with abc mapping #29788
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
ENH: Allow map with abc mapping #29788
Conversation
7df89d3
to
dadb76a
Compare
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.
Looks good generally. cc @TomAugspurger and @simonjayhawkins for related discussions that we've had on types - not sure if this is something we would need to review holistically
Changes here look sensible.
It's not immediately clear to my why `Series.__init__` had to change. Can
we add a test in pandas/tests/series/test_constructors.py that shows the
change?
We may want to move these Mapping subclass test helpers to a central place
and reuse them.
…On Fri, Nov 22, 2019 at 12:41 PM William Ayd ***@***.***> wrote:
***@***.**** requested changes on this pull request.
Looks good generally. cc @TomAugspurger <https://github.com/TomAugspurger>
and @simonjayhawkins <https://github.com/simonjayhawkins> for related
discussions that we've had on types - not sure if this is something we
would need to review holistically
------------------------------
In pandas/core/base.py
<#29788 (comment)>:
> @@ -1215,8 +1215,8 @@ def _map_values(self, mapper, na_action=None):
# we can fastpath dict/Series to an efficient map
# as we know that we are not going to have to yield
# python types
- if isinstance(mapper, dict):
- if hasattr(mapper, "__missing__"):
+ if isinstance(mapper, abc.Mapping):
+ if isinstance(mapper, dict) and hasattr(mapper, "__missing__"):
I think can just simplify to isinstance(mapper, dict) then no? Check for
*missing* after that is duplicative?
------------------------------
In pandas/core/series.py
<#29788 (comment)>:
> @@ -253,7 +254,7 @@ def __init__(
else:
data = data.reindex(index, copy=copy)
data = data._data
- elif isinstance(data, dict):
+ elif isinstance(data, abc.Mapping):
Is this hit in a test case?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#29788?email_source=notifications&email_token=AAKAOIQADNZIIDNSMG5VKADQVARXPA5CNFSM4JQNDNTKYY3PNVWWK3TUL52HS4DFWFIHK3DMKJSXC5LFON2FEZLWNFSXPKTDN5WW2ZLOORPWSZGOCMWUNVY#pullrequestreview-321734359>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAKAOIWCCMV5AXPYT46DJITQVARXPANCNFSM4JQNDNTA>
.
|
Added a test for constructing a |
pandas/core/series.py
Outdated
@@ -253,7 +254,7 @@ def __init__( | |||
else: | |||
data = data.reindex(index, copy=copy) | |||
data = data._data | |||
elif isinstance(data, dict): | |||
elif isinstance(data, abc.Mapping): |
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.
you can use is_dict_like here instead
pandas/core/base.py
Outdated
@@ -1215,8 +1215,8 @@ def _map_values(self, mapper, na_action=None): | |||
# we can fastpath dict/Series to an efficient map | |||
# as we know that we are not going to have to yield | |||
# python types | |||
if isinstance(mapper, dict): | |||
if hasattr(mapper, "__missing__"): | |||
if isinstance(mapper, abc.Mapping): |
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.
use is_dict_like
lgtm. ping on green. |
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. @ohad83 can you merge master one more time? I think should get CI green
@WillAyd Merged but still a test seems to fail, I am not really sure what's happening and how, if at all, it's related to this PR.. Any ideas? |
55a1dfa
to
5c6bb3c
Compare
pandas/util/testing.py
Outdated
@@ -2121,6 +2121,20 @@ def __init__(self, *args, **kwargs): | |||
dict.__init__(self, *args, **kwargs) | |||
|
|||
|
|||
class TestNonDictMapping(abc.Mapping): |
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.
Can you make this a fixture in pandas/tests/series/conftest.py
instead of putting in pandas.util.testing
? With the latter sometimes things can leak into our API and definitely wouldn't want that here
@ohad83 can you rebase and address Will's comment if still relevant |
can you merge master and will look again |
@ohad83 going to have this close the original issue. if the remaining case is worthile, then pls open a new issue. |
subclass. TST - Add a test for the aforementioned bug
DOC - Moved whatsnew of the aforementioned change from BUG to ENH
DOC - Added issue number for tests of Series.map with abc.Mapping argument
TST - Added a test of constructing a series from NonDictMapping
DOC - Typo fix
This reverts commit 5fe4140. Apparently it's an unwanted pattern checked for by code_checks.sh (from collections.abc import)
1f7d650
to
97d245d
Compare
@WillAyd if any comments |
Thanks @ohad83 great PR |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff