Skip to content

Commit 6f5e30f

Browse files
committed
Rewrite checks for plottable data.
Allow for numeric and date-like numpy data types and for datetime objects.
1 parent e31aaab commit 6f5e30f

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

xarray/plot/plot.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,49 @@
1313

1414
import numpy as np
1515
import pandas as pd
16+
from datetime import datetime
1617

1718
from .utils import _determine_cmap_params, _infer_xy_labels, get_axis
1819
from .facetgrid import FacetGrid
1920
from xarray.core.pycompat import basestring
2021

2122

22-
# Maybe more appropriate to keep this in .utils
23-
def _right_dtype(arr, types):
23+
def _valid_numpy_subdtype(x, numpy_types):
2424
"""
25-
Is the numpy array a sub dtype of anything in types?
25+
Is any dtype from numpy_types superior to the dtype of x?
2626
"""
27-
return any(np.issubdtype(arr.dtype, t) for t in types)
27+
# If any of the types given in numpy_types is understood as numpy.generic,
28+
# all possible x will be considered valid. This is probably unwanted.
29+
# Warn then.
30+
for t in numpy_types:
31+
if np.issubdtype(np.generic, t):
32+
warnings.warn('{} is treated as numpy.generic.'.format(t),
33+
RuntimeWarning, stacklevel=3)
2834

35+
return any(np.issubdtype(x.dtype, t) for t in numpy_types)
2936

30-
def _ensure_plottable(*args):
37+
38+
def _valid_other_type(x, types):
3139
"""
32-
Raise exception if there is anything in args that can't be plotted on
33-
an axis.
40+
Do all elements of x have a type from types?
3441
"""
35-
from netCDF4 import datetime as nc4datetime
36-
plottypes = [np.floating, np.integer, np.timedelta64, np.datetime64,
37-
nc4datetime]
42+
return all(any(isinstance(el, t) for t in types)
43+
for el in np.ravel(x))
3844

39-
# Lists need to be converted to np.arrays here.
40-
if not any(_right_dtype(np.array(x), plottypes) for x in args):
41-
raise TypeError('Plotting requires coordinates to be numeric '
42-
'or dates.')
4345

46+
def _ensure_plottable(*args):
47+
"""
48+
Raise exception if there is anything in args that can't be plotted on an
49+
axis.
50+
"""
51+
numpy_types = [np.floating, np.integer, np.timedelta64, np.datetime64]
52+
other_types = [datetime]
53+
54+
for x in args:
55+
if not (_valid_numpy_subdtype(np.array(x), numpy_types)
56+
or _valid_other_type(np.array(x), other_types)):
57+
raise TypeError('Plotting requires coordinates to be numeric '
58+
'or dates.')
4459

4560
def _easy_facetgrid(darray, plotfunc, x, y, row=None, col=None,
4661
col_wrap=None, sharex=True, sharey=True, aspect=None,

0 commit comments

Comments
 (0)