|
11 | 11 | import pandas as pd
|
12 | 12 |
|
13 | 13 | from xarray.core import dtypes, duck_array_ops, formatting, formatting_html, ops
|
| 14 | +from xarray.core.indexing import BasicIndexer, ExplicitlyIndexed |
14 | 15 | from xarray.core.options import OPTIONS, _get_keep_attrs
|
15 | 16 | from xarray.core.pycompat import is_duck_dask_array
|
16 | 17 | from xarray.core.utils import Frozen, either_dict_or_kwargs, is_scalar
|
|
40 | 41 | ScalarOrArray,
|
41 | 42 | SideOptions,
|
42 | 43 | T_DataWithCoords,
|
| 44 | + T_Variable, |
43 | 45 | )
|
44 | 46 | from xarray.core.variable import Variable
|
45 | 47 |
|
@@ -1770,31 +1772,27 @@ def is_np_timedelta_like(dtype: DTypeLike) -> bool:
|
1770 | 1772 | return np.issubdtype(dtype, np.timedelta64)
|
1771 | 1773 |
|
1772 | 1774 |
|
1773 |
| -def _contains_cftime_datetimes(array) -> bool: |
1774 |
| - """Check if an array contains cftime.datetime objects""" |
| 1775 | +def _contains_cftime_datetimes(array: Any) -> bool: |
| 1776 | + """Check if a array inside a Variable contains cftime.datetime objects""" |
1775 | 1777 | if cftime is None:
|
1776 | 1778 | return False
|
1777 |
| - else: |
1778 |
| - if array.dtype == np.dtype("O") and array.size > 0: |
1779 |
| - sample = np.asarray(array).flat[0] |
1780 |
| - if is_duck_dask_array(sample): |
1781 |
| - sample = sample.compute() |
1782 |
| - if isinstance(sample, np.ndarray): |
1783 |
| - sample = sample.item() |
1784 |
| - return isinstance(sample, cftime.datetime) |
1785 |
| - else: |
1786 |
| - return False |
1787 | 1779 |
|
| 1780 | + if array.dtype == np.dtype("O") and array.size > 0: |
| 1781 | + first_idx = (0,) * array.ndim |
| 1782 | + if isinstance(array, ExplicitlyIndexed): |
| 1783 | + first_idx = BasicIndexer(first_idx) |
| 1784 | + sample = array[first_idx] |
| 1785 | + return isinstance(np.asarray(sample).item(), cftime.datetime) |
| 1786 | + |
| 1787 | + return False |
1788 | 1788 |
|
1789 |
| -def contains_cftime_datetimes(var) -> bool: |
| 1789 | + |
| 1790 | +def contains_cftime_datetimes(var: T_Variable) -> bool: |
1790 | 1791 | """Check if an xarray.Variable contains cftime.datetime objects"""
|
1791 |
| - if var.dtype == np.dtype("O") and var.size > 0: |
1792 |
| - return _contains_cftime_datetimes(var.data) |
1793 |
| - else: |
1794 |
| - return False |
| 1792 | + return _contains_cftime_datetimes(var._data) |
1795 | 1793 |
|
1796 | 1794 |
|
1797 |
| -def _contains_datetime_like_objects(var) -> bool: |
| 1795 | +def _contains_datetime_like_objects(var: T_Variable) -> bool: |
1798 | 1796 | """Check if a variable contains datetime like objects (either
|
1799 | 1797 | np.datetime64, np.timedelta64, or cftime.datetime)
|
1800 | 1798 | """
|
|
0 commit comments