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

FIX: indexed pandas bar #15166

Merged
merged 2 commits into from
Sep 2, 2019
Merged

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Aug 31, 2019

PR Summary

Closes #15162

basically if you do df = pd.DataFrame({"x":[1,2,3],"width":[.2,.4,.6]},index=[1,2,3]), then df.x[0] errors because that index doesn't exist, which breaks bar.

While I appreciate this is a bit more code than trying to call iat(0), which is what we did before, this solution doesn't presuppose iat exists.

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np

df = pd.DataFrame({"x":[1.,2.,3.],"width":[.2,.4,.6]}, index=[1, 2, 3])
plt.figure()
plt.bar(df.x, 1., width=df.width)
plt.savefig('/Users/jklymak/downloads/Test1.png')

df = pd.DataFrame({"x":[1,3,10]},index=[1000,2000,3000])

plt.figure()
plt.bar(df.x, 1, width=.2)
plt.savefig('/Users/jklymak/downloads/Test2.png')

Now yields the expected (and pre 3.1.0):

Test1
Test2

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

@jklymak jklymak added this to the v3.1.2 milestone Aug 31, 2019
@jklymak jklymak added third-party integration topic: units and array ducktypes Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. labels Aug 31, 2019
@ImportanceOfBeingErnest
Copy link
Member

Is this function used anywhere else then with bars? Should bar also support dataframes, like

df = pd.DataFrame({0:[1,2,3]},index=[1,2,3])

This works with plt.scatter(df, [3,4,5]), but probably not with plt.bar(df, [3,4,5]), right?

@jklymak
Copy link
Member Author

jklymak commented Sep 1, 2019

I don't think

df = pd.DataFrame({'boo':[1,2,3]},index=[1,2,3])
plt.figure()
plt.bar(df, 1, width=.2)

ever worked (TypeError: float() argument must be a string or a number, not 'NoneType' on 3.0.0). The error with this PR if the data name is called 0 is a bit more mysterious, but that's a relatively perverse data frame (I hope).

@ImportanceOfBeingErnest
Copy link
Member

Ok, just take df = pd.DataFrame([1,2,3]) then. I think it would be good to define whether single-column dataframes are supposed to work or not. They do for scatter/plot, so one might expect that, but if they don't, maybe a message that is a bit more helpful than "Your linewidth is wrong" would be useful.

@jklymak
Copy link
Member Author

jklymak commented Sep 1, 2019

This PR is trying to fix something I broke, not expand the duck typing for bar.

Use `safe_first_element` from cbook which does the `next(iter(x))`
dance to get the first element of an object (as defined by what you
get when you iterate over it).
@@ -2138,33 +2138,14 @@ def _convert_dx(dx, x0, xconv, convert):
# removes the units from unit packages like `pint` that
# wrap numpy arrays.
try:
x0 = x0[0]
x0 = cbook.safe_first_element(x0)
Copy link
Member Author

@jklymak jklymak Sep 1, 2019

Choose a reason for hiding this comment

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

This is great! Should have checked cbook....

@dstansby dstansby merged commit e67c2fb into matplotlib:master Sep 2, 2019
@lumberbot-app
Copy link

lumberbot-app bot commented Sep 2, 2019

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
$ git checkout v3.1.x
$ git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
$ git cherry-pick -m1 e67c2fbfc257a1f3414eebfe873b78552ba9a7f3
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
$ git commit -am 'Backport PR #15166: FIX: indexed pandas bar'
  1. Push to a named branch :
git push YOURFORK v3.1.x:auto-backport-of-pr-15166-on-v3.1.x
  1. Create a PR against branch v3.1.x, I would have named this PR:

"Backport PR #15166 on branch v3.1.x"

And apply the correct labels and milestones.

Congratulation you did some good work ! Hopefully your backport PR will be tested by the continuous integration and merged soon!

If these instruction are inaccurate, feel free to suggest an improvement.

dstansby added a commit to dstansby/matplotlib that referenced this pull request Sep 2, 2019
NelleV added a commit that referenced this pull request Sep 2, 2019
@tacaswell
Copy link
Member

tacaswell commented Sep 2, 2019

backported in #15172 / 169a9f7

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 topic: units and array ducktypes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

axes.bar fails when x is int-indexed pandas.Series
4 participants