(self)
| 5491 | assert expected.equals(actual) |
| 5492 | |
| 5493 | def test_from_dataframe_multiindex(self) -> None: |
| 5494 | index = pd.MultiIndex.from_product([["a", "b"], [1, 2, 3]], names=["x", "y"]) |
| 5495 | df = pd.DataFrame({"z": np.arange(6)}, index=index) |
| 5496 | |
| 5497 | expected = Dataset( |
| 5498 | {"z": (("x", "y"), [[0, 1, 2], [3, 4, 5]])}, |
| 5499 | coords={"x": ["a", "b"], "y": [1, 2, 3]}, |
| 5500 | ) |
| 5501 | actual = Dataset.from_dataframe(df) |
| 5502 | assert_identical(actual, expected) |
| 5503 | |
| 5504 | df2 = df.iloc[[3, 2, 1, 0, 4, 5], :] |
| 5505 | actual = Dataset.from_dataframe(df2) |
| 5506 | assert_identical(actual, expected) |
| 5507 | |
| 5508 | df3 = df.iloc[:4, :] |
| 5509 | expected3 = Dataset( |
| 5510 | {"z": (("x", "y"), [[0, 1, 2], [3, np.nan, np.nan]])}, |
| 5511 | coords={"x": ["a", "b"], "y": [1, 2, 3]}, |
| 5512 | ) |
| 5513 | actual = Dataset.from_dataframe(df3) |
| 5514 | assert_identical(actual, expected3) |
| 5515 | |
| 5516 | df_nonunique = df.iloc[[0, 0], :] |
| 5517 | with pytest.raises(ValueError, match=r"non-unique MultiIndex"): |
| 5518 | Dataset.from_dataframe(df_nonunique) |
| 5519 | |
| 5520 | def test_from_dataframe_unsorted_levels(self) -> None: |
| 5521 | # regression test for GH-4186 |
nothing calls this directly
no test coverage detected