(self)
| 3766 | @requires_dask |
| 3767 | @pytest.mark.xfail(not has_dask_ge_2025_1_0, reason="dask-expr is broken") |
| 3768 | def test_to_dask_dataframe(self) -> None: |
| 3769 | arr_np = np.arange(3 * 4).reshape(3, 4) |
| 3770 | arr = DataArray(arr_np, [("B", [1, 2, 3]), ("A", list("cdef"))], name="foo") |
| 3771 | expected_s = arr.to_series() |
| 3772 | actual = arr.to_dask_dataframe()["foo"] |
| 3773 | |
| 3774 | assert_array_equal(actual.values, np.asarray(expected_s.values)) |
| 3775 | |
| 3776 | actual = arr.to_dask_dataframe(dim_order=["A", "B"])["foo"] |
| 3777 | assert_array_equal(arr_np.transpose().reshape(-1), actual.values) |
| 3778 | |
| 3779 | # regression test for coords with different dimensions |
| 3780 | |
| 3781 | arr.coords["C"] = ("B", [-1, -2, -3]) |
| 3782 | expected_df = arr.to_series().to_frame() |
| 3783 | expected_df["C"] = [-1] * 4 + [-2] * 4 + [-3] * 4 |
| 3784 | expected_df = expected_df[["C", "foo"]] |
| 3785 | actual = arr.to_dask_dataframe()[["C", "foo"]] |
| 3786 | |
| 3787 | assert_array_equal(expected_df.values, np.asarray(actual.values)) |
| 3788 | assert_array_equal( |
| 3789 | expected_df.columns.values, np.asarray(actual.columns.values) |
| 3790 | ) |
| 3791 | |
| 3792 | with pytest.raises(ValueError, match="does not match the set of dimensions"): |
| 3793 | arr.to_dask_dataframe(dim_order=["B", "A", "C"]) |
| 3794 | |
| 3795 | arr.name = None |
| 3796 | with pytest.raises( |
| 3797 | ValueError, |
| 3798 | match="Cannot convert an unnamed DataArray", |
| 3799 | ): |
| 3800 | arr.to_dask_dataframe() |
| 3801 | |
| 3802 | def test_to_pandas_name_matches_coordinate(self) -> None: |
| 3803 | # coordinate with same name as array |
nothing calls this directly
no test coverage detected