MCPcopy
hub / github.com/pydata/xarray / test_groupby_bins

Method test_groupby_bins

xarray/tests/test_groupby.py:1711–1748  ·  view source on GitHub ↗
(
        self,
        coords: np.typing.ArrayLike,
        use_flox: bool,
        cut_kwargs: dict,
    )

Source from the content-addressed store, hash-verified

1709 ),
1710 )
1711 def test_groupby_bins(
1712 self,
1713 coords: np.typing.ArrayLike,
1714 use_flox: bool,
1715 cut_kwargs: dict,
1716 ) -> None:
1717 array = DataArray(
1718 np.arange(4), dims="dim_0", coords={"dim_0": coords}, name="a"
1719 )
1720 # the first value should not be part of any group ("right" binning)
1721 array[0] = 99
1722 # bins follow conventions for pandas.cut
1723 # https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.cut.html
1724 bins = [0, 1.5, 5]
1725
1726 df = array.to_dataframe()
1727 df["dim_0_bins"] = pd.cut(array["dim_0"], bins, **cut_kwargs) # type: ignore[call-overload]
1728
1729 expected_df = df.groupby("dim_0_bins", observed=True).sum()
1730 expected = expected_df.to_xarray().assign_coords(
1731 dim_0_bins=cast(pd.CategoricalIndex, expected_df.index).categories
1732 )["a"]
1733
1734 with xr.set_options(use_flox=use_flox):
1735 gb = array.groupby_bins("dim_0", bins=bins, **cut_kwargs)
1736 shuffled = gb.shuffle_to_chunks().groupby_bins(
1737 "dim_0", bins=bins, **cut_kwargs
1738 )
1739 actual = gb.sum()
1740 assert_identical(expected, actual)
1741 assert_identical(expected, shuffled.sum())
1742
1743 actual = gb.map(lambda x: x.sum())
1744 assert_identical(expected, actual)
1745 assert_identical(expected, shuffled.map(lambda x: x.sum()))
1746
1747 # make sure original array dims are unchanged
1748 assert len(array.dim_0) == 4
1749
1750 def test_groupby_bins_ellipsis(self) -> None:
1751 da = xr.DataArray(np.ones((2, 3, 4)))

Callers

nothing calls this directly

Calls 11

to_dataframeMethod · 0.95
groupby_binsMethod · 0.95
DataArrayClass · 0.90
assert_identicalFunction · 0.90
arangeMethod · 0.80
sumMethod · 0.45
groupbyMethod · 0.45
assign_coordsMethod · 0.45
groupby_binsMethod · 0.45
shuffle_to_chunksMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected