Skip to content

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

Merged
merged 1 commit into from
Dec 3, 2014

Conversation

mcsalgado
Copy link
Contributor

No description provided.

@@ -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):
Copy link
Contributor

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

Copy link
Contributor

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

@mcsalgado
Copy link
Contributor Author

@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))):
Copy link
Contributor

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?

Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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))

Copy link
Contributor

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)

Copy link
Contributor Author

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.

@jreback jreback added API Design Testing pandas testing functions or related to the test suite Compat pandas objects compatability with Numpy or Python functions labels Nov 27, 2014
@jreback jreback added this to the 0.15.2 milestone Nov 27, 2014
@jreback
Copy link
Contributor

jreback commented Nov 29, 2014

  • pls add a release note in v0.15.2 (enhancements section, use this PR as the refernce issue)
  • squash down

@jreback jreback merged commit 867b288 into pandas-dev:master Dec 3, 2014
@jreback
Copy link
Contributor

jreback commented Dec 3, 2014

@mcsalgado thanks!

@mcsalgado
Copy link
Contributor Author

you're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Compat pandas objects compatability with Numpy or Python functions Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants