Skip to content

FIX: support pandas 0.25 #15007

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 3 commits into from
Aug 9, 2019
Merged

FIX: support pandas 0.25 #15007

merged 3 commits into from
Aug 9, 2019

Conversation

tacaswell
Copy link
Member

closes #14992

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@tacaswell tacaswell added this to the v2.2.5 milestone Aug 8, 2019
@jklymak jklymak added Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. third-party integration labels Aug 8, 2019
shape = x[:, None].shape
# work around https://github.com/pandas-dev/pandas/issues/27775
# which mean the shape is not as expected
if len(shape) != 2:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's x[:, None].ndim

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather check the shape as that is what needs to be correct and do not want to risk ndim also being out of sync.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think than .ndim may be returning something wrong then .shape could also be wrong, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the pandas issue they pointed out that this changes was due to an optimization (see pandas-dev/pandas#27775 (comment)) on Index and the current implementation of ndim is:

@property
def ndim(self):
    """
    Number of dimensions of the underlying data, by definition 1.
    """
    return 1

https://github.com/pandas-dev/pandas/blob/8b6942f2876264b745529a3c3a7cf410ee096c34/pandas/core/base.py#L694-L699 which confirmed my suspicioun that shape and ndim were out of sync.

However, digging through this convinced me that we I should check that x[:, None].ndim >= 2 as this will fail on older version of pandas and stop relying on an unintentional quirk of Index classes almost ducktyping as arrays.

This will fail on older version of pandas as well.
@@ -1326,7 +1326,13 @@ def _check_1d(x):
return np.atleast_1d(x)
else:
try:
x[:, None]
ndim = x[:, None].ndim

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't want to do a np.asarray here first? That should make it work with older pandas versions, but also robust to potential future changes (eg if pandas starts raising for that)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do not want to asarray things we don't have to (in particular because in some cases that can strip units off of things). So long as future versions raises one of the two exceptions caught below it will "just work" and as soon as you decide what exception you are going to raise we can add it.

The point of this is to check if we need to asarray (which in done implicitly in atleast_1d).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see. I suppose if we raise an error it will be an IndexError, so that would already be covered then

@anntzer anntzer merged commit e8fa673 into matplotlib:master Aug 9, 2019
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Aug 9, 2019
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Aug 9, 2019
jklymak added a commit that referenced this pull request Aug 9, 2019
…007-on-v3.1.x

Backport PR #15007 on branch v3.1.x (FIX: support pandas 0.25)
jklymak added a commit that referenced this pull request Aug 9, 2019
…007-on-v2.2.x

Backport PR #15007 on branch v2.2.x (FIX: support pandas 0.25)
@tacaswell tacaswell deleted the fix_pd_compat branch August 9, 2019 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. third-party integration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IndexError: tuple index out of range with pandas 0.25.
5 participants