Skip to content
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: Change implementation of pandas.api.types.is_list_like to check for iterability in more pythonic way #43196

Closed
dcoukos opened this issue Aug 24, 2021 · 4 comments
Labels
API Design Closing Candidate May be closeable, needs more eyeballs Enhancement Needs Discussion Requires discussion from core team before further action

Comments

@dcoukos
Copy link

dcoukos commented Aug 24, 2021

Is your feature request related to a problem?

Yes, this would improve predictability of behavior with pandas extension arrays.

Describe the solution you'd like

pandas.api.types.is_list_like can check for iterability in a more pythonic way according to https://docs.python.org/3/glossary.html#term-iterable. This would result in the appropriate call being iter(obj) rather than checking for __iter__ attribute.

API breaking implications

This will not affect the API.

Describe alternatives you've considered

Alternative solutions include communicating to third parties that they must change their extension arrays to include hasattr(q, "__iter__") == False to make sure that classes with ambiguous data iterability (may contain either scalar or array-like data) are perceived as utterable when they contain scalar data.

Additional context

Opened a bug report for this issue. I think an enhancement request is more appropriate, but I am linking the previous (closed) bug report as it includes useful information.
#43195

@dcoukos dcoukos added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 24, 2021
@dcoukos
Copy link
Author

dcoukos commented Aug 24, 2021

Looks like this has actually been discussed earlier, even though I had not found them when searching.

Not sure if development of a solution for this problem has stalled.
For reference:

#35127
#35131

hgrecco/pint#1128

@mroeschke mroeschke added API Design Needs Discussion Requires discussion from core team before further action and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 30, 2021
@jbrockmendel
Copy link
Member

cc @burnpanck closed by #43196?

@jbrockmendel jbrockmendel added the Closing Candidate May be closeable, needs more eyeballs label Dec 21, 2021
@burnpanck
Copy link
Contributor

While not implementing the proposed enhancement exactly, it does enable the feature motivating this request to be implemented. Therefore, it's fine with me if this is closed.

The enhancement remains unimplemented, as is_list_like still only does an approximate model of an iterability check. It only checks if the type supports iterability in general, but not if the specific value does. Before #43196, it special-cased numpy arrays by taking the dimensionality of the value into account. Now, this special-case is extended to some duck-arrays (specifically, any value having a .ndim attribute).

However, is_list_like never intended to actually model iterability exactly - it explicitly special-cases str and bytes as not list-like despite being iterable. Therefore that would require a fundamental API discussion - what should Pandas even consider as list-like? Is it really iterability with the exception of str and bytes? Then, implementing this would cause further performance concerns.

@jbrockmendel
Copy link
Member

@dcoukos closing this as solved, pls reopen if im wrong on that point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Closing Candidate May be closeable, needs more eyeballs Enhancement Needs Discussion Requires discussion from core team before further action
Projects
None yet
Development

No branches or pull requests

4 participants