From 282e28de5484063269bb4a7a8126142f8646759d Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 26 Dec 2021 11:12:06 +0100 Subject: [PATCH 01/10] remove datetime converters --- xarray/plot/utils.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index a49302f7f87..0602a3ed04a 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -28,20 +28,20 @@ ROBUST_PERCENTILE = 2.0 -_registered = False +# _registered = False -def register_pandas_datetime_converter_if_needed(): +# 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 + # 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() + # register_pandas_datetime_converter_if_needed() import matplotlib.pyplot as plt return plt From 207998154803d26b7ffbf74b82959b137563eac8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 26 Dec 2021 10:16:37 +0000 Subject: [PATCH 02/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/plot/utils.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index 0602a3ed04a..1c510697a23 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -32,11 +32,11 @@ # 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 +# 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(): From 7ba730733de9621afa315d628fedfdd44118876b Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Mon, 27 Dec 2021 08:00:20 +0100 Subject: [PATCH 03/10] Update utils.py --- xarray/plot/utils.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index 1c510697a23..cb032ed274d 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -28,20 +28,8 @@ 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 matplotlib.pyplot as plt return plt From add7086f60515ae6271806a5a0a6b4f745fa2fba Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 30 Dec 2021 23:40:43 +0100 Subject: [PATCH 04/10] Update whats-new.rst --- doc/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index bd6097d61fe..cdc9121b3bb 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -27,6 +27,8 @@ New Features Breaking changes ~~~~~~~~~~~~~~~~ +- Rely on matplotlibs default converters instead of pandas (:issue:`6102`, :pull:`6109`). + By `Jimmy Westling `_. Deprecations From 3890d0984f8a131290bf4a75621789e4c597cfdf Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 30 Dec 2021 23:49:11 +0100 Subject: [PATCH 05/10] add a todo comment --- xarray/plot/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index cc268ec9bf3..01b4cfeb8e9 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -30,6 +30,7 @@ def import_matplotlib_pyplot(): """Import pyplot as register appropriate converters.""" + # TODO: This function doesn't do anything, remove it? import matplotlib.pyplot as plt return plt From a00d506afed021903a04520aa515340db280f387 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Mon, 3 Jan 2022 18:54:00 +0100 Subject: [PATCH 06/10] Update whats-new.rst --- doc/whats-new.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index ee9fb77d090..7a10e8376d5 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -28,6 +28,7 @@ New Features Breaking changes ~~~~~~~~~~~~~~~~ - Rely on matplotlibs default converters instead of pandas (:issue:`6102`, :pull:`6109`). + By `Jimmy Westling `_. - 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 `_. From 9da14c943d506c3ecce0305d22a18ed82514a64d Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Mon, 3 Jan 2022 19:53:05 +0100 Subject: [PATCH 07/10] Add tests --- xarray/tests/test_plot.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index b5cc334bcd0..c405826b8c4 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -2674,7 +2674,26 @@ 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) + 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 + 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 + assert type(ax.xaxis.get_major_locator()) is mpl.dates.AutoDateLocator @pytest.mark.filterwarnings("ignore:setting an array element with a sequence") From 5b772fb00df37f1e4cb6548966daaabacde8ec04 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 3 Jan 2022 18:55:14 +0000 Subject: [PATCH 08/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/tests/test_plot.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index c405826b8c4..3b3fa639235 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -2685,11 +2685,13 @@ def test_datetime_plot1d(self): def test_datetime_plot2d(self): # Test that matplotlib-native datetime works: da = DataArray( - np.arange(3*4).reshape(3,4), + 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)]}) + 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 From 5842964d87ac7f5d352e04397c1374cc541da44e Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 5 Jan 2022 18:24:35 +0100 Subject: [PATCH 09/10] Apply suggestions from code review Co-authored-by: Mathias Hauser --- doc/whats-new.rst | 2 +- xarray/plot/utils.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index fb0f49a8822..8638d1eb8dc 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -29,7 +29,7 @@ New Features Breaking changes ~~~~~~~~~~~~~~~~ -- Rely on matplotlibs default converters instead of pandas (:issue:`6102`, :pull:`6109`). +- Rely on matplotlib's default datetime converters instead of pandas' (:issue:`6102`, :pull:`6109`). By `Jimmy Westling `_. - 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`) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index 01b4cfeb8e9..3b2a133b3e5 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -29,8 +29,8 @@ def import_matplotlib_pyplot(): - """Import pyplot as register appropriate converters.""" - # TODO: This function doesn't do anything, remove it? + """import pyplot""" + # TODO: This function doesn't do anything (after #6109), remove it? import matplotlib.pyplot as plt return plt From 228e0be009ddfb383495b0c9cfa5dcd7ae1b2793 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 5 Jan 2022 18:38:25 +0100 Subject: [PATCH 10/10] Update test_plot.py --- xarray/tests/test_plot.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 3b3fa639235..3088b7e109c 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -2674,12 +2674,18 @@ def test_datetime_units(self): # test that matplotlib-native datetime works: fig, ax = plt.subplots() ax.plot(self.darray["time"], self.darray) + + # 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): @@ -2695,6 +2701,9 @@ def test_datetime_plot2d(self): 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