Skip to content

Commit 9c00578

Browse files
committed
Correctly re-instate Matplotlib converters
1 parent 61819ab commit 9c00578

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

doc/source/whatsnew/v0.25.1.rst

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Plotting
111111
^^^^^^^^
112112

113113
- Added a pandas_plotting_backends entrypoint group for registering plot backends. See :ref:`extending.plotting-backends` for more (:issue:`26747`).
114+
- Fixed the re-instatement of Matplotlib datetime converters after calling
115+
`pandas.plotting.deregister_matplotlib_converters()` (:issue:`27481`).
114116
-
115117
-
116118

pandas/plotting/_matplotlib/converter.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ def register(explicit=True):
6464

6565
pairs = get_pairs()
6666
for type_, cls in pairs:
67-
converter = cls()
68-
if type_ in units.registry:
67+
# Cache previous converter if present
68+
if type_ in units.registry and not isinstance(units.registry[type_], cls):
6969
previous = units.registry[type_]
7070
_mpl_units[type_] = previous
71-
units.registry[type_] = converter
71+
# Replace with pandas converter
72+
units.registry[type_] = cls()
7273

7374

7475
def deregister():

pandas/tests/plotting/test_converter.py

+15
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ def test_initial_warning():
4040
assert "Using an implicitly" in out
4141

4242

43+
def test_registry_mpl_resets():
44+
# Check that Matplotlib converters are properly reset (see issue #27481)
45+
code = (
46+
"import matplotlib.units as units; "
47+
"import matplotlib.dates as mdates; "
48+
"n_conv = len(units.registry); "
49+
"import pandas as pd; "
50+
"pd.plotting.register_matplotlib_converters(); "
51+
"pd.plotting.deregister_matplotlib_converters(); "
52+
"assert len(units.registry) == n_conv"
53+
)
54+
call = [sys.executable, "-c", code]
55+
subprocess.check_output(call)
56+
57+
4358
def test_timtetonum_accepts_unicode():
4459
assert converter.time2num("00:01") == converter.time2num("00:01")
4560

0 commit comments

Comments
 (0)