|
7 | 7 | from html import escape
|
8 | 8 | from numbers import Number
|
9 | 9 | from operator import methodcaller
|
10 |
| -from pathlib import Path |
| 10 | +from os import PathLike |
11 | 11 | from typing import (
|
12 | 12 | TYPE_CHECKING,
|
13 | 13 | Any,
|
@@ -1832,7 +1832,7 @@ def to_netcdf(
|
1832 | 1832 |
|
1833 | 1833 | Parameters
|
1834 | 1834 | ----------
|
1835 |
| - path : str, Path or file-like, optional |
| 1835 | + path : str, path-like or file-like, optional |
1836 | 1836 | Path to which to save this dataset. File-like objects are only
|
1837 | 1837 | supported by the scipy engine. If no path is provided, this
|
1838 | 1838 | function returns the resulting netCDF file as bytes; in this case,
|
@@ -1914,8 +1914,8 @@ def to_netcdf(
|
1914 | 1914 |
|
1915 | 1915 | def to_zarr(
|
1916 | 1916 | self,
|
1917 |
| - store: Union[MutableMapping, str, Path] = None, |
1918 |
| - chunk_store: Union[MutableMapping, str, Path] = None, |
| 1917 | + store: Union[MutableMapping, str, PathLike] = None, |
| 1918 | + chunk_store: Union[MutableMapping, str, PathLike] = None, |
1919 | 1919 | mode: str = None,
|
1920 | 1920 | synchronizer=None,
|
1921 | 1921 | group: str = None,
|
@@ -1944,9 +1944,9 @@ def to_zarr(
|
1944 | 1944 |
|
1945 | 1945 | Parameters
|
1946 | 1946 | ----------
|
1947 |
| - store : MutableMapping, str or Path, optional |
| 1947 | + store : MutableMapping, str or path-like, optional |
1948 | 1948 | Store or path to directory in local or remote file system.
|
1949 |
| - chunk_store : MutableMapping, str or Path, optional |
| 1949 | + chunk_store : MutableMapping, str or path-like, optional |
1950 | 1950 | Store or path to directory in local or remote file system only for Zarr
|
1951 | 1951 | array chunks. Requires zarr-python v2.4.0 or later.
|
1952 | 1952 | mode : {"w", "w-", "a", "r+", None}, optional
|
@@ -4153,34 +4153,34 @@ def unstack(
|
4153 | 4153 | )
|
4154 | 4154 |
|
4155 | 4155 | result = self.copy(deep=False)
|
4156 |
| - for dim in dims: |
4157 | 4156 |
|
4158 |
| - if ( |
4159 |
| - # Dask arrays don't support assignment by index, which the fast unstack |
4160 |
| - # function requires. |
4161 |
| - # https://github.com/pydata/xarray/pull/4746#issuecomment-753282125 |
4162 |
| - any(is_duck_dask_array(v.data) for v in self.variables.values()) |
4163 |
| - # Sparse doesn't currently support (though we could special-case |
4164 |
| - # it) |
4165 |
| - # https://github.com/pydata/sparse/issues/422 |
4166 |
| - or any( |
4167 |
| - isinstance(v.data, sparse_array_type) |
4168 |
| - for v in self.variables.values() |
4169 |
| - ) |
4170 |
| - or sparse |
4171 |
| - # Until https://github.com/pydata/xarray/pull/4751 is resolved, |
4172 |
| - # we check explicitly whether it's a numpy array. Once that is |
4173 |
| - # resolved, explicitly exclude pint arrays. |
4174 |
| - # # pint doesn't implement `np.full_like` in a way that's |
4175 |
| - # # currently compatible. |
4176 |
| - # # https://github.com/pydata/xarray/pull/4746#issuecomment-753425173 |
4177 |
| - # # or any( |
4178 |
| - # # isinstance(v.data, pint_array_type) for v in self.variables.values() |
4179 |
| - # # ) |
4180 |
| - or any( |
4181 |
| - not isinstance(v.data, np.ndarray) for v in self.variables.values() |
4182 |
| - ) |
4183 |
| - ): |
| 4157 | + # we want to avoid allocating an object-dtype ndarray for a MultiIndex, |
| 4158 | + # so we can't just access self.variables[v].data for every variable. |
| 4159 | + # We only check the non-index variables. |
| 4160 | + # https://github.com/pydata/xarray/issues/5902 |
| 4161 | + nonindexes = [ |
| 4162 | + self.variables[k] for k in set(self.variables) - set(self.xindexes) |
| 4163 | + ] |
| 4164 | + # Notes for each of these cases: |
| 4165 | + # 1. Dask arrays don't support assignment by index, which the fast unstack |
| 4166 | + # function requires. |
| 4167 | + # https://github.com/pydata/xarray/pull/4746#issuecomment-753282125 |
| 4168 | + # 2. Sparse doesn't currently support (though we could special-case it) |
| 4169 | + # https://github.com/pydata/sparse/issues/422 |
| 4170 | + # 3. pint requires checking if it's a NumPy array until |
| 4171 | + # https://github.com/pydata/xarray/pull/4751 is resolved, |
| 4172 | + # Once that is resolved, explicitly exclude pint arrays. |
| 4173 | + # pint doesn't implement `np.full_like` in a way that's |
| 4174 | + # currently compatible. |
| 4175 | + needs_full_reindex = sparse or any( |
| 4176 | + is_duck_dask_array(v.data) |
| 4177 | + or isinstance(v.data, sparse_array_type) |
| 4178 | + or not isinstance(v.data, np.ndarray) |
| 4179 | + for v in nonindexes |
| 4180 | + ) |
| 4181 | + |
| 4182 | + for dim in dims: |
| 4183 | + if needs_full_reindex: |
4184 | 4184 | result = result._unstack_full_reindex(dim, fill_value, sparse)
|
4185 | 4185 | else:
|
4186 | 4186 | result = result._unstack_once(dim, fill_value)
|
|
0 commit comments