Skip to content

Commit

Permalink
Merge branch 'master' into fix/1652
Browse files Browse the repository at this point in the history
  • Loading branch information
shoyer authored Oct 29, 2017
2 parents 53b1e4a + c58d142 commit e5999c1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ Bug fixes
- Fix ``rasterio`` backend for Rasterio versions 1.0alpha10 and newer.
(:issue:`1641`). By `Chris Holden <https://github.com/ceholden>`_.

- Fix plotting with datetime64 axis labels under pandas 0.21 and newer
(:issue:`1661`).
By `Stephan Hoyer <https://github.com/shoyer>`_.

.. _whats-new.0.9.6:

v0.9.6 (8 June 2017)
Expand Down
7 changes: 4 additions & 3 deletions xarray/plot/facetgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

from ..core.pycompat import getargspec
from ..core.formatting import format_item
from .utils import _determine_cmap_params, _infer_xy_labels
from .utils import (_determine_cmap_params, _infer_xy_labels,
import_matplotlib_pyplot)


# Overrides axes.labelsize, xtick.major.size, ytick.major.size
Expand Down Expand Up @@ -101,7 +102,7 @@ def __init__(self, data, col=None, row=None, col_wrap=None,
"""

import matplotlib.pyplot as plt
plt = import_matplotlib_pyplot()

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

for ax, namedict in zip(self.axes.flat, self.name_dicts.flat):
if namedict is not None:
Expand Down
7 changes: 4 additions & 3 deletions xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import pandas as pd
from datetime import datetime

from .utils import _determine_cmap_params, _infer_xy_labels, get_axis
from .utils import (_determine_cmap_params, _infer_xy_labels, get_axis,
import_matplotlib_pyplot)
from .facetgrid import FacetGrid
from xarray.core.pycompat import basestring

Expand Down Expand Up @@ -179,7 +180,7 @@ def line(darray, *args, **kwargs):
Additional arguments to matplotlib.pyplot.plot
"""
import matplotlib.pyplot as plt
plt = import_matplotlib_pyplot()

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

return _easy_facetgrid(**allargs)

import matplotlib.pyplot as plt
plt = import_matplotlib_pyplot()

# colors is mutually exclusive with cmap
if cmap and colors:
Expand Down
19 changes: 19 additions & 0 deletions xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ def import_seaborn():
return sns


_registered = False


def register_pandas_datetime_converter_if_needed():
# based on https://github.com/pandas-dev/pandas/pull/17710
global _registered
if not _registered:
from pandas.tseries import converter
converter.register()
_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


def _determine_extend(calc_data, vmin, vmax):
extend_min = calc_data.min() < vmin
extend_max = calc_data.max() > vmax
Expand Down
6 changes: 4 additions & 2 deletions xarray/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ def test_convert_label_indexer(self):
indexing.convert_label_indexer(mindex, 0)
with pytest.raises(ValueError):
indexing.convert_label_indexer(index, {'three': 0})
with raises_regex(KeyError, 'index to be fully lexsorted'):
indexing.convert_label_indexer(mindex, (slice(None), 1, 'no_level'))
with pytest.raises((KeyError, IndexError)):
# pandas 0.21 changed this from KeyError to IndexError
indexing.convert_label_indexer(
mindex, (slice(None), 1, 'no_level'))

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

0 comments on commit e5999c1

Please sign in to comment.