|
13 | 13 |
|
14 | 14 | import numpy as np
|
15 | 15 | import pandas as pd
|
| 16 | +from datetime import datetime |
16 | 17 |
|
17 | 18 | from .utils import _determine_cmap_params, _infer_xy_labels, get_axis
|
18 | 19 | from .facetgrid import FacetGrid
|
19 | 20 | from xarray.core.pycompat import basestring
|
20 | 21 |
|
21 | 22 |
|
22 |
| -# Maybe more appropriate to keep this in .utils |
23 |
| -def _right_dtype(arr, types): |
| 23 | +def _valid_numpy_subdtype(x, numpy_types): |
24 | 24 | """
|
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? |
26 | 26 | """
|
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) |
28 | 34 |
|
| 35 | + return any(np.issubdtype(x.dtype, t) for t in numpy_types) |
29 | 36 |
|
30 |
| -def _ensure_plottable(*args): |
| 37 | + |
| 38 | +def _valid_other_type(x, types): |
31 | 39 | """
|
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? |
34 | 41 | """
|
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)) |
38 | 44 |
|
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.') |
43 | 45 |
|
| 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.') |
44 | 59 |
|
45 | 60 | def _easy_facetgrid(darray, plotfunc, x, y, row=None, col=None,
|
46 | 61 | col_wrap=None, sharex=True, sharey=True, aspect=None,
|
|
0 commit comments