|
28 | 28 | from xarray.core import dtypes, indexing, utils
|
29 | 29 | from xarray.core.common import duck_array_ops, full_like
|
30 | 30 | from xarray.core.indexes import Index
|
31 |
| -from xarray.core.pycompat import integer_types |
| 31 | +from xarray.core.pycompat import integer_types, sparse_array_type |
32 | 32 | from xarray.core.utils import is_scalar
|
33 | 33 |
|
34 | 34 | from . import (
|
@@ -3085,14 +3085,42 @@ def test_unstack_sparse(self):
|
3085 | 3085 | # test fill_value
|
3086 | 3086 | actual = ds.unstack("index", sparse=True)
|
3087 | 3087 | expected = ds.unstack("index")
|
| 3088 | + assert isinstance(actual["var"].data, sparse_array_type) |
3088 | 3089 | assert actual["var"].variable._to_dense().equals(expected["var"].variable)
|
3089 | 3090 | assert actual["var"].data.density < 1.0
|
3090 | 3091 |
|
3091 | 3092 | actual = ds["var"].unstack("index", sparse=True)
|
3092 | 3093 | expected = ds["var"].unstack("index")
|
| 3094 | + assert isinstance(actual.data, sparse_array_type) |
3093 | 3095 | assert actual.variable._to_dense().equals(expected.variable)
|
3094 | 3096 | assert actual.data.density < 1.0
|
3095 | 3097 |
|
| 3098 | + mindex = pd.MultiIndex.from_arrays( |
| 3099 | + [np.arange(3), np.arange(3)], names=["a", "b"] |
| 3100 | + ) |
| 3101 | + ds_eye = Dataset( |
| 3102 | + {"var": (("z", "foo", "bar"), np.ones((3, 4, 5)))}, |
| 3103 | + coords={"z": mindex, "foo": np.arange(4), "bar": np.arange(5)}, |
| 3104 | + ) |
| 3105 | + actual = ds_eye.unstack(sparse=True, fill_value=0) |
| 3106 | + assert isinstance(actual["var"].data, sparse_array_type) |
| 3107 | + expected = xr.Dataset( |
| 3108 | + { |
| 3109 | + "var": ( |
| 3110 | + ("foo", "bar", "a", "b"), |
| 3111 | + np.broadcast_to(np.eye(3, 3), (4, 5, 3, 3)), |
| 3112 | + ) |
| 3113 | + }, |
| 3114 | + coords={ |
| 3115 | + "foo": np.arange(4), |
| 3116 | + "bar": np.arange(5), |
| 3117 | + "a": np.arange(3), |
| 3118 | + "b": np.arange(3), |
| 3119 | + }, |
| 3120 | + ) |
| 3121 | + actual["var"].data = actual["var"].data.todense() |
| 3122 | + assert_equal(expected, actual) |
| 3123 | + |
3096 | 3124 | def test_stack_unstack_fast(self):
|
3097 | 3125 | ds = Dataset(
|
3098 | 3126 | {
|
|
0 commit comments