-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: make Series work with map objects the same way as generators #8909
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
Conversation
@@ -183,7 +183,7 @@ def __init__(self, data=None, index=None, dtype=None, name=None, | |||
raise ValueError("cannot specify a dtype with a Categorical") | |||
if name is None: | |||
name = data.name | |||
elif isinstance(data, types.GeneratorType): | |||
elif isinstance(data, types.GeneratorType) or isinstance(data, map): |
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 out the map in the same isinstance (the 2nd arg can be a tuple)
is there a generic types.Map or abc.Map ? (eg a python a tract base class that represent a map expression)
if so maybe use that
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.
There's collections.Iterable
for that purposes.
Whoops, sorry, it's too generic:
In [4]: isinstance({1:2}, collections.Iterable)
Out[4]: True
@jreback I looked around before to see if there was something like types.MapType but I guess there isn't. It's similar with python3's range that actually works fine on Series because you can call len on it, which is not the case with python3's map. |
@@ -183,7 +183,8 @@ def __init__(self, data=None, index=None, dtype=None, name=None, | |||
raise ValueError("cannot specify a dtype with a Categorical") | |||
if name is None: | |||
name = data.name | |||
elif isinstance(data, types.GeneratorType): | |||
elif (isinstance(data, types.GeneratorType) or | |||
(compat.PY3 and isinstance(data, map))): |
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.
so isinstance(data, (types.GeneratorType, map))
doesn't work?
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.
try also with collections.Iterable
here
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.
it doesn't work because python2's map is just a function. python3's map is a class.
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.
ahh ok, what about adding collections.Iterable
alongside GeneratorType as well?
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.
ideally I'd love to make Series just accept any iterable but the way it's working now it raises an exception for sets or frozensets. I didn't want to change that without discussing the reason why it was decided this way.
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 could do isinstance(v, collections.Iterable) and not isinstance(v, (collections.Set, collections.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.
sets and frozensets are iterable but non-orderable, so doesn't make sense for Series to directly accept them (e.g. you need to be a list-list or a dictionary-like which maps keys->values)
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.
this is a reasonable argument, but I don't think this is the way python works in general.
python doesn't throw exceptions for things like these. I mean, you don't need to wrap sets with list(some_set) or iter(some_set) in most cases. python just uses the "order" it gets from iterating the set.
I think zip is a bit similar to Series, and it works fine if you just use sets with it.
and it's not just a way to conform with python idioms, I think a reasonable amount of code in the constructors could be simplified if you just ask these objects to be iterable.
|
@mcsalgado thanks! |
you're welcome! |
No description provided.