Skip to content

Fix errors that appear after update to pandas 0.21 #1669

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 1 commit into from
Oct 29, 2017
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
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,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 @@
import numpy as np

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 @@ -61,8 +61,10 @@ def test_convert_label_indexer(self):
indexing.convert_label_indexer(mindex, 0)
with self.assertRaises(ValueError):
indexing.convert_label_indexer(index, {'three': 0})
with self.assertRaisesRegexp(KeyError, 'index to be fully lexsorted'):
indexing.convert_label_indexer(mindex, (slice(None), 1, 'no_level'))
with self.assertRaises((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