Skip to content

Commit c58d142

Browse files
authored
Merge pull request #1669 from shoyer/pandas-0.21-errors
Fix errors that appear after update to pandas 0.21
2 parents 3a99529 + 4cc8ea2 commit c58d142

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

doc/whats-new.rst

+4
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ Bug fixes
346346
- Fix ``rasterio`` backend for Rasterio versions 1.0alpha10 and newer.
347347
(:issue:`1641`). By `Chris Holden <https://github.com/ceholden>`_.
348348

349+
- Fix plotting with datetime64 axis labels under pandas 0.21 and newer
350+
(:issue:`1661`).
351+
By `Stephan Hoyer <https://github.com/shoyer>`_.
352+
349353
.. _whats-new.0.9.6:
350354

351355
v0.9.6 (8 June 2017)

xarray/plot/facetgrid.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
import numpy as np
1111

1212
from ..core.formatting import format_item
13-
from .utils import _determine_cmap_params, _infer_xy_labels
13+
from .utils import (_determine_cmap_params, _infer_xy_labels,
14+
import_matplotlib_pyplot)
1415

1516

1617
# Overrides axes.labelsize, xtick.major.size, ytick.major.size
@@ -101,7 +102,7 @@ def __init__(self, data, col=None, row=None, col_wrap=None,
101102
102103
"""
103104

104-
import matplotlib.pyplot as plt
105+
plt = import_matplotlib_pyplot()
105106

106107
# Handle corner case of nonunique coordinates
107108
rep_col = col is not None and not data[col].to_index().is_unique
@@ -409,7 +410,7 @@ def map(self, func, *args, **kwargs):
409410
self : FacetGrid object
410411
411412
"""
412-
import matplotlib.pyplot as plt
413+
plt = import_matplotlib_pyplot()
413414

414415
for ax, namedict in zip(self.axes.flat, self.name_dicts.flat):
415416
if namedict is not None:

xarray/plot/plot.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import pandas as pd
1616
from datetime import datetime
1717

18-
from .utils import _determine_cmap_params, _infer_xy_labels, get_axis
18+
from .utils import (_determine_cmap_params, _infer_xy_labels, get_axis,
19+
import_matplotlib_pyplot)
1920
from .facetgrid import FacetGrid
2021
from xarray.core.pycompat import basestring
2122

@@ -179,7 +180,7 @@ def line(darray, *args, **kwargs):
179180
Additional arguments to matplotlib.pyplot.plot
180181
181182
"""
182-
import matplotlib.pyplot as plt
183+
plt = import_matplotlib_pyplot()
183184

184185
ndims = len(darray.dims)
185186
if ndims != 1:
@@ -429,7 +430,7 @@ def newplotfunc(darray, x=None, y=None, figsize=None, size=None,
429430

430431
return _easy_facetgrid(**allargs)
431432

432-
import matplotlib.pyplot as plt
433+
plt = import_matplotlib_pyplot()
433434

434435
# colors is mutually exclusive with cmap
435436
if cmap and colors:

xarray/plot/utils.py

+19
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ def import_seaborn():
4040
return sns
4141

4242

43+
_registered = False
44+
45+
46+
def register_pandas_datetime_converter_if_needed():
47+
# based on https://github.com/pandas-dev/pandas/pull/17710
48+
global _registered
49+
if not _registered:
50+
from pandas.tseries import converter
51+
converter.register()
52+
_registered = True
53+
54+
55+
def import_matplotlib_pyplot():
56+
"""Import pyplot as register appropriate converters."""
57+
register_pandas_datetime_converter_if_needed()
58+
import matplotlib.pyplot as plt
59+
return plt
60+
61+
4362
def _determine_extend(calc_data, vmin, vmax):
4463
extend_min = calc_data.min() < vmin
4564
extend_max = calc_data.max() > vmax

xarray/tests/test_indexing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ def test_convert_label_indexer(self):
6161
indexing.convert_label_indexer(mindex, 0)
6262
with self.assertRaises(ValueError):
6363
indexing.convert_label_indexer(index, {'three': 0})
64-
with self.assertRaisesRegexp(KeyError, 'index to be fully lexsorted'):
65-
indexing.convert_label_indexer(mindex, (slice(None), 1, 'no_level'))
64+
with self.assertRaises((KeyError, IndexError)):
65+
# pandas 0.21 changed this from KeyError to IndexError
66+
indexing.convert_label_indexer(
67+
mindex, (slice(None), 1, 'no_level'))
6668

6769
def test_convert_unsorted_datetime_index_raises(self):
6870
index = pd.to_datetime(['2001', '2000', '2002'])

0 commit comments

Comments
 (0)