(self)
| 3660 | DataArray(np.random.randn(1, 2, 3, 4, 5)).to_pandas() |
| 3661 | |
| 3662 | def test_to_dataframe(self) -> None: |
| 3663 | # regression test for #260 |
| 3664 | arr_np = np.random.randn(3, 4) |
| 3665 | |
| 3666 | arr = DataArray(arr_np, [("B", [1, 2, 3]), ("A", list("cdef"))], name="foo") |
| 3667 | expected_s = arr.to_series() |
| 3668 | actual_s = arr.to_dataframe()["foo"] |
| 3669 | assert_array_equal(np.asarray(expected_s.values), np.asarray(actual_s.values)) |
| 3670 | assert_array_equal(np.asarray(expected_s.name), np.asarray(actual_s.name)) |
| 3671 | assert_array_equal(expected_s.index.values, actual_s.index.values) |
| 3672 | |
| 3673 | actual_s = arr.to_dataframe(dim_order=["A", "B"])["foo"] |
| 3674 | assert_array_equal(arr_np.transpose().reshape(-1), np.asarray(actual_s.values)) |
| 3675 | |
| 3676 | # regression test for coords with different dimensions |
| 3677 | arr.coords["C"] = ("B", [-1, -2, -3]) |
| 3678 | expected_df = arr.to_series().to_frame() |
| 3679 | expected_df["C"] = [-1] * 4 + [-2] * 4 + [-3] * 4 |
| 3680 | expected_df = expected_df[["C", "foo"]] |
| 3681 | actual_df = arr.to_dataframe() |
| 3682 | assert_array_equal(np.asarray(expected_df.values), np.asarray(actual_df.values)) |
| 3683 | assert_array_equal(expected_df.columns.values, actual_df.columns.values) |
| 3684 | assert_array_equal(expected_df.index.values, actual_df.index.values) |
| 3685 | |
| 3686 | with pytest.raises(ValueError, match="does not match the set of dimensions"): |
| 3687 | arr.to_dataframe(dim_order=["B", "A", "C"]) |
| 3688 | |
| 3689 | with pytest.raises(ValueError, match=r"cannot convert a scalar"): |
| 3690 | arr.sel(A="c", B=2).to_dataframe() |
| 3691 | |
| 3692 | arr.name = None # unnamed |
| 3693 | with pytest.raises(ValueError, match=r"unnamed"): |
| 3694 | arr.to_dataframe() |
| 3695 | |
| 3696 | def test_to_dataframe_multiindex(self) -> None: |
| 3697 | # regression test for #3008 |
nothing calls this directly
no test coverage detected