Skip to content

Commit e7285eb

Browse files
Illviljanmathausepre-commit-ci[bot]
authored
Remove registration of pandas datetime converter in plotting (pydata#6109)
Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a98309c commit e7285eb

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ New Features
2929

3030
Breaking changes
3131
~~~~~~~~~~~~~~~~
32+
- Rely on matplotlib's default datetime converters instead of pandas' (:issue:`6102`, :pull:`6109`).
33+
By `Jimmy Westling <https://github.com/illviljan>`_.
3234
- Improve repr readability when there are a large number of dimensions in datasets or dataarrays by
3335
wrapping the text once the maximum display width has been exceeded. (:issue: `5546`, :pull:`5662`)
3436
By `Jimmy Westling <https://github.com/illviljan>`_.

xarray/plot/utils.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,9 @@
2828
ROBUST_PERCENTILE = 2.0
2929

3030

31-
_registered = False
32-
33-
34-
def register_pandas_datetime_converter_if_needed():
35-
# based on https://github.com/pandas-dev/pandas/pull/17710
36-
global _registered
37-
if not _registered:
38-
pd.plotting.register_matplotlib_converters()
39-
_registered = True
40-
41-
4231
def import_matplotlib_pyplot():
43-
"""Import pyplot as register appropriate converters."""
44-
register_pandas_datetime_converter_if_needed()
32+
"""import pyplot"""
33+
# TODO: This function doesn't do anything (after #6109), remove it?
4534
import matplotlib.pyplot as plt
4635

4736
return plt

xarray/tests/test_plot.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2674,7 +2674,37 @@ def test_datetime_units(self):
26742674
# test that matplotlib-native datetime works:
26752675
fig, ax = plt.subplots()
26762676
ax.plot(self.darray["time"], self.darray)
2677-
assert isinstance(ax.xaxis.get_major_locator(), mpl.dates.AutoDateLocator)
2677+
2678+
# Make sure only mpl converters are used, use type() so only
2679+
# mpl.dates.AutoDateLocator passes and no other subclasses:
2680+
assert type(ax.xaxis.get_major_locator()) is mpl.dates.AutoDateLocator
2681+
2682+
def test_datetime_plot1d(self):
2683+
# Test that matplotlib-native datetime works:
2684+
p = self.darray.plot.line()
2685+
ax = p[0].axes
2686+
2687+
# Make sure only mpl converters are used, use type() so only
2688+
# mpl.dates.AutoDateLocator passes and no other subclasses:
2689+
assert type(ax.xaxis.get_major_locator()) is mpl.dates.AutoDateLocator
2690+
2691+
def test_datetime_plot2d(self):
2692+
# Test that matplotlib-native datetime works:
2693+
da = DataArray(
2694+
np.arange(3 * 4).reshape(3, 4),
2695+
dims=("x", "y"),
2696+
coords={
2697+
"x": [1, 2, 3],
2698+
"y": [np.datetime64(f"2000-01-{x:02d}") for x in range(1, 5)],
2699+
},
2700+
)
2701+
2702+
p = da.plot.pcolormesh()
2703+
ax = p.axes
2704+
2705+
# Make sure only mpl converters are used, use type() so only
2706+
# mpl.dates.AutoDateLocator passes and no other subclasses:
2707+
assert type(ax.xaxis.get_major_locator()) is mpl.dates.AutoDateLocator
26782708

26792709

26802710
@pytest.mark.filterwarnings("ignore:setting an array element with a sequence")

0 commit comments

Comments
 (0)