Skip to content

Remove registration of pandas datetime converter in plotting #6109

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
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ New Features

Breaking changes
~~~~~~~~~~~~~~~~
- Rely on matplotlib's default datetime converters instead of pandas' (:issue:`6102`, :pull:`6109`).
By `Jimmy Westling <https://github.com/illviljan>`_.
- Improve repr readability when there are a large number of dimensions in datasets or dataarrays by
wrapping the text once the maximum display width has been exceeded. (:issue: `5546`, :pull:`5662`)
By `Jimmy Westling <https://github.com/illviljan>`_.
Expand Down
15 changes: 2 additions & 13 deletions xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,9 @@
ROBUST_PERCENTILE = 2.0


_registered = False


def register_pandas_datetime_converter_if_needed():
# based on https://github.com/pandas-dev/pandas/pull/17710
global _registered
if not _registered:
pd.plotting.register_matplotlib_converters()
_registered = True


def import_matplotlib_pyplot():
"""Import pyplot as register appropriate converters."""
register_pandas_datetime_converter_if_needed()
"""import pyplot"""
# TODO: This function doesn't do anything (after #6109), remove it?
import matplotlib.pyplot as plt

return plt
Expand Down
32 changes: 31 additions & 1 deletion xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2674,7 +2674,37 @@ def test_datetime_units(self):
# test that matplotlib-native datetime works:
fig, ax = plt.subplots()
ax.plot(self.darray["time"], self.darray)
assert isinstance(ax.xaxis.get_major_locator(), mpl.dates.AutoDateLocator)

# Make sure only mpl converters are used, use type() so only
# mpl.dates.AutoDateLocator passes and no other subclasses:
assert type(ax.xaxis.get_major_locator()) is mpl.dates.AutoDateLocator

def test_datetime_plot1d(self):
# Test that matplotlib-native datetime works:
p = self.darray.plot.line()
ax = p[0].axes

# Make sure only mpl converters are used, use type() so only
# mpl.dates.AutoDateLocator passes and no other subclasses:
assert type(ax.xaxis.get_major_locator()) is mpl.dates.AutoDateLocator

def test_datetime_plot2d(self):
# Test that matplotlib-native datetime works:
da = DataArray(
np.arange(3 * 4).reshape(3, 4),
dims=("x", "y"),
coords={
"x": [1, 2, 3],
"y": [np.datetime64(f"2000-01-{x:02d}") for x in range(1, 5)],
},
)

p = da.plot.pcolormesh()
ax = p.axes

# Make sure only mpl converters are used, use type() so only
# mpl.dates.AutoDateLocator passes and no other subclasses:
assert type(ax.xaxis.get_major_locator()) is mpl.dates.AutoDateLocator


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