Wrapper for accommodating a pandas.Index in an xarray.Variable. IndexVariable preserve loaded values in the form of a pandas.Index instead of a NumPy array. Hence, their values are immutable and must always be one- dimensional. They also have a name property, which is the name of t
| 2746 | |
| 2747 | |
| 2748 | class IndexVariable(Variable): |
| 2749 | """Wrapper for accommodating a pandas.Index in an xarray.Variable. |
| 2750 | |
| 2751 | IndexVariable preserve loaded values in the form of a pandas.Index instead |
| 2752 | of a NumPy array. Hence, their values are immutable and must always be one- |
| 2753 | dimensional. |
| 2754 | |
| 2755 | They also have a name property, which is the name of their sole dimension |
| 2756 | unless another name is given. |
| 2757 | """ |
| 2758 | |
| 2759 | __slots__ = () |
| 2760 | |
| 2761 | # TODO: PandasIndexingAdapter doesn't match the array api: |
| 2762 | _data: PandasIndexingAdapter # type: ignore[assignment] |
| 2763 | |
| 2764 | def __init__(self, dims, data, attrs=None, encoding=None, fastpath=False): |
| 2765 | super().__init__(dims, data, attrs, encoding, fastpath) |
| 2766 | if self.ndim != 1: |
| 2767 | raise ValueError(f"{type(self).__name__} objects must be 1-dimensional") |
| 2768 | |
| 2769 | # Unlike in Variable, always eagerly load values into memory |
| 2770 | if not isinstance(self._data, PandasIndexingAdapter): |
| 2771 | self._data = PandasIndexingAdapter(self._data) |
| 2772 | |
| 2773 | def __dask_tokenize__(self) -> object: |
| 2774 | from dask.base import normalize_token |
| 2775 | |
| 2776 | # Don't waste time converting pd.Index to np.ndarray |
| 2777 | return normalize_token( |
| 2778 | (type(self), self._dims, self._data.array, self._attrs or None) |
| 2779 | ) |
| 2780 | |
| 2781 | def load(self): |
| 2782 | # data is already loaded into memory for IndexVariable |
| 2783 | return self |
| 2784 | |
| 2785 | async def load_async(self): |
| 2786 | # data is already loaded into memory for IndexVariable |
| 2787 | return self |
| 2788 | |
| 2789 | # https://github.com/python/mypy/issues/1465 |
| 2790 | @Variable.data.setter # type: ignore[attr-defined] |
| 2791 | def data(self, data): |
| 2792 | raise ValueError( |
| 2793 | f"Cannot assign to the .data attribute of dimension coordinate a.k.a IndexVariable {self.name!r}. " |
| 2794 | f"Please use DataArray.assign_coords, Dataset.assign_coords or Dataset.assign as appropriate." |
| 2795 | ) |
| 2796 | |
| 2797 | @Variable.values.setter # type: ignore[attr-defined] |
| 2798 | def values(self, values): |
| 2799 | raise ValueError( |
| 2800 | f"Cannot assign to the .values attribute of dimension coordinate a.k.a IndexVariable {self.name!r}. " |
| 2801 | f"Please use DataArray.assign_coords, Dataset.assign_coords or Dataset.assign as appropriate." |
| 2802 | ) |
| 2803 | |
| 2804 | def chunk( |
| 2805 | self, |
no outgoing calls
searching dependent graphs…