8000 Fix pandas DataFrame align center by ryanlaclair · Pull Request #8785 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fix pandas DataFrame align center #8785

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 4 commits into from
Jul 1, 2017

Conversation

ryanlaclair
Copy link
Contributor

Fixes pandas KeyError when align=center is specified for bar plot by replacing index access with a list comprehension using zip.

PR Summary

Added test highlighting the bug to lib/matplotlib/tests/test_axes.py.
Fixed bug in lib/matplotlib/axes/_axes.py by replacing the index access with a list comprehension using zip.

Bugfix for issue #8767 .

PR Checklist

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

fig, axArr = plt.subplots(2)

curr = 1
for ax in axArr:
Copy link
Member

Choose a reason for hiding this comment

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

is there a reason you need to test this two ways? (and if so, why aren't they explicitly separate parameterized tests?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could not recreate the error found inn 8767 without the 2 subplots.

Copy link
Member

Choose a reason for hiding this comment

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

it is probably worth doing both bar and barh (as you changed the code path for both).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will add the test for barh to this pr tonight, and will add a fig.canvas.draw to make sure the rendering finishes all the way.

Copy link
Member

Choose a reason for hiding this comment

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

The trick is probably to have an index without 0 in it, I think df = pd.DataFrame({'a': range(5), 'b': range(5)}, index=list('abcde')) would produce the exception.


curr = 1
for ax in axArr:
rects = ax.bar(df.loc[df['col1'] == curr, 'col2'],
Copy link
Member

Choose a reason for hiding this comment

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

also, what's considered a passing test here? Just no error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct

Copy link
Member

Choose a reason for hiding this comment

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

I think it'd be worthwhile to do an affirmative test to verify it works as expected.

Copy link
Member

Choose a reason for hiding this comment

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

We have a lot of other tests were not raising is sufficient. We do not really need an image test for this. Might be worth putting in a fig.canvas.draw to make sure it renders all the way through though.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think it needs an image test either...but I do think just a not raises isn't great when testing a kwarg - like it could very well be doing something unexpected/not parsing the data correctly.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Jun 22, 2017
@tacaswell
Copy link
Member

@ryanlaclair Thanks !

👍 on the fix, would like to see a test for barh as well.

@tacaswell
Copy link
Member

You can also pass index=['foo', 'bar'] to the data frame init

Copy link
Member
@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

The test could be tweaked to pass an explicit index karg to the DataFrame init, but this is ok as it is

This is a candidate for squash merge.

8000
@anntzer anntzer merged commit a19ed84 into matplotlib:master Jul 1, 2017
@anntzer
Copy link
Contributor
anntzer commented Jul 1, 2017

Squashed.
Thanks!

@tacaswell
Copy link
Member

@ryanlaclair I think this was your first Matplotlib contribution. Congratulations 🎉 ! Hopefully we will hear from you again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0