MCPcopy Index your code
hub / github.com/pydata/xarray / test_unstack_sparse

Method test_unstack_sparse

xarray/tests/test_dataset.py:4246–4290  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

4244
4245 @requires_sparse
4246 def test_unstack_sparse(self) -> None:
4247 ds = xr.Dataset(
4248 {"var": (("x",), np.arange(6))},
4249 coords={"x": [0, 1, 2] * 2, "y": (("x",), ["a"] * 3 + ["b"] * 3)},
4250 )
4251 # make ds incomplete
4252 ds = ds.isel(x=[0, 2, 3, 4]).set_index(index=["x", "y"])
4253 # test fill_value
4254 actual1 = ds.unstack("index", sparse=True)
4255 expected1 = ds.unstack("index")
4256 assert isinstance(actual1["var"].data, sparse_array_type)
4257 assert actual1["var"].variable._to_dense().equals(expected1["var"].variable)
4258 assert actual1["var"].data.density < 1.0
4259
4260 actual2 = ds["var"].unstack("index", sparse=True)
4261 expected2 = ds["var"].unstack("index")
4262 assert isinstance(actual2.data, sparse_array_type)
4263 assert actual2.variable._to_dense().equals(expected2.variable)
4264 assert actual2.data.density < 1.0
4265
4266 midx = pd.MultiIndex.from_arrays([np.arange(3), np.arange(3)], names=["a", "b"])
4267 coords = Coordinates.from_pandas_multiindex(midx, "z")
4268 coords["foo"] = np.arange(4)
4269 coords["bar"] = np.arange(5)
4270 ds_eye = Dataset(
4271 {"var": (("z", "foo", "bar"), np.ones((3, 4, 5)))}, coords=coords
4272 )
4273 actual3 = ds_eye.unstack(sparse=True, fill_value=0)
4274 assert isinstance(actual3["var"].data, sparse_array_type)
4275 expected3 = xr.Dataset(
4276 {
4277 "var": (
4278 ("foo", "bar", "a", "b"),
4279 np.broadcast_to(np.eye(3, 3), (4, 5, 3, 3)),
4280 )
4281 },
4282 coords={
4283 "foo": np.arange(4),
4284 "bar": np.arange(5),
4285 "a": np.arange(3),
4286 "b": np.arange(3),
4287 },
4288 )
4289 actual3["var"].data = actual3["var"].data.todense()
4290 assert_equal(expected3, actual3)
4291
4292 def test_stack_unstack_fast(self) -> None:
4293 ds = Dataset(

Callers

nothing calls this directly

Calls 12

iselMethod · 0.95
unstackMethod · 0.95
DatasetClass · 0.90
assert_equalFunction · 0.90
arangeMethod · 0.80
broadcast_toMethod · 0.80
set_indexMethod · 0.45
equalsMethod · 0.45
_to_denseMethod · 0.45
unstackMethod · 0.45
todenseMethod · 0.45

Tested by

no test coverage detected