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

Method test_to_pandas

xarray/tests/test_dataarray.py:3627–3660  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

3625 assert_identical(exp_y, y_bc)
3626
3627 def test_to_pandas(self) -> None:
3628 # 0d
3629 actual_xr = DataArray(42).to_pandas()
3630 expected = np.array(42)
3631 assert_array_equal(actual_xr, expected)
3632
3633 # 1d
3634 values = np.random.randn(3)
3635 index = pd.Index(["a", "b", "c"], name="x")
3636 da = DataArray(values, coords=[index])
3637 actual_s = da.to_pandas()
3638 assert_array_equal(np.asarray(actual_s.values), values)
3639 assert_array_equal(actual_s.index, index)
3640 assert_array_equal(actual_s.index.name, "x")
3641
3642 # 2d
3643 values = np.random.randn(3, 2)
3644 da = DataArray(
3645 values, coords=[("x", ["a", "b", "c"]), ("y", [0, 1])], name="foo"
3646 )
3647 actual_df = da.to_pandas()
3648 assert_array_equal(np.asarray(actual_df.values), values)
3649 assert_array_equal(actual_df.index, ["a", "b", "c"])
3650 assert_array_equal(actual_df.columns, [0, 1])
3651
3652 # roundtrips
3653 for shape in [(3,), (3, 4)]:
3654 dims = list("abc")[: len(shape)]
3655 da = DataArray(np.random.randn(*shape), dims=dims)
3656 roundtripped = DataArray(da.to_pandas()).drop_vars(dims)
3657 assert_identical(da, roundtripped)
3658
3659 with pytest.raises(ValueError, match=r"Cannot convert"):
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

Callers

nothing calls this directly

Calls 5

to_pandasMethod · 0.95
DataArrayClass · 0.90
assert_identicalFunction · 0.90
to_pandasMethod · 0.45
drop_varsMethod · 0.45

Tested by

no test coverage detected