Skip to content

API: Change matplotlib formatter registration #28722

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 15 commits into from
Oct 25, 2019

Conversation

TomAugspurger
Copy link
Contributor

@TomAugspurger TomAugspurger commented Oct 1, 2019

This PR does two things

  1. Enforces the deprecation of pandas auto registering our formatters.
  2. Provides a new option, "auto", which will only apply our formatters to our plots. By default, the value will be "auto".

Background: pandas used to register formatters for Period, datetime, date, and time. This formatters affected plots created using matplotlib only, not pandas. For various reasons (including import time) we don't want to do this on an import pandas.

A minimal change would be to just not do the registration on import pandas. Users would either manually do it, or pandas does it automatically when we do DataFrame.plot. The downside of pandas doing it automatically is then in the following

fig1 = plt.plot(x, y)
DataFrame.plot(...)  # registers formatters
fig2 = plt.plot(x, y)

fig1 and fig2 will be different because pandas has implicilty registered formatters.

The auto option will automatically register the formatters, make the plot, then deregister the formatters.

Closes #18720

@TomAugspurger TomAugspurger added this to the 1.0 milestone Oct 1, 2019
@jreback jreback added the Visualization plotting label Oct 1, 2019
@@ -71,27 +59,12 @@ def test_register_by_default(self):
call = [sys.executable, "-c", code]
assert subprocess.check_call(call) == 0

def test_warns(self):
plt = pytest.importorskip("matplotlib.pyplot")
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a way to check that we have not registered things?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, we do that indirectly with the code check... If we haven't imported matplotlib, then we know we haven't registered anything.

def register(explicit=True):
# Renamed in pandas.plotting.__init__
global _WARN
def pandas_converters_deco(func):
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 why this needs to be a decorator? Perhaps a style preference but I would find it easier to grok if the context manager was used where required in the actual plotting methods

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need. I mainly did it for a smaller diff / less indentation :)

Happy to go either way here.

Tom Augspurger added 2 commits October 10, 2019 10:23
@TomAugspurger
Copy link
Contributor Author

All green here now.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Looks good to me. The auto behaviour seems to work nicely based on a quick test

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Small doc comment

Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

Great PR, nice work. Lgtm, just an idea on implementation and naming, but happy to see this merged with or without thar.

@TomAugspurger
Copy link
Contributor Author

Should be good to go.

@jorisvandenbossche jorisvandenbossche merged commit 4046336 into pandas-dev:master Oct 25, 2019
@jorisvandenbossche
Copy link
Member

Thanks!

HawkinsBA pushed a commit to HawkinsBA/pandas that referenced this pull request Oct 29, 2019
Reksbril pushed a commit to Reksbril/pandas that referenced this pull request Nov 18, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
bongolegend pushed a commit to bongolegend/pandas that referenced this pull request Jan 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VIS: don't modify matplotlib units registry globally
5 participants