Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions xarray/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,13 @@ def to_index(self, ordered_dims: Sequence[Hashable] | None = None) -> pd.Index:
np.tile(np.repeat(code, repeat_counts[i]), tile_counts[i])
for code in codes
]
level_list += [list(level) for level in levels]
level_list += levels
names += index.names

return pd.MultiIndex(
levels=level_list, codes=[list(c) for c in code_list], names=names
levels=level_list, # type: ignore[arg-type,unused-ignore]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the pandas stubs might be wrong here. They want a list of hashables, but a list of dtypes does work fine

codes=[list(c) for c in code_list],
names=names,
)


Expand Down
28 changes: 28 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -3528,6 +3528,34 @@ def test_to_dataframe_0length(self) -> None:
assert len(actual) == 0
assert_array_equal(actual.index.names, list("ABC"))

@pytest.mark.parametrize(
"x_dtype,y_dtype,v_dtype",
[
(np.uint32, np.float32, np.uint32),
(np.int16, np.float64, np.int64),
(np.uint8, np.float32, np.uint16),
(np.int32, np.float32, np.int8),
],
)
def test_to_dataframe_coord_dtypes_2d(self, x_dtype, y_dtype, v_dtype) -> None:
x = np.array([1], dtype=x_dtype)
y = np.array([1.0], dtype=y_dtype)
v = np.array([[42]], dtype=v_dtype)

da = DataArray(v, dims=["x", "y"], coords={"x": x, "y": y})
df = da.to_dataframe(name="v").reset_index()

# Check that coordinate dtypes are preserved
assert df["x"].dtype == np.dtype(x_dtype), (
f"x coord: expected {x_dtype}, got {df['x'].dtype}"
)
assert df["y"].dtype == np.dtype(y_dtype), (
f"y coord: expected {y_dtype}, got {df['y'].dtype}"
)
assert df["v"].dtype == np.dtype(v_dtype), (
f"v data: expected {v_dtype}, got {df['v'].dtype}"
)

@requires_dask_expr
@requires_dask
@pytest.mark.xfail(not has_dask_ge_2025_1_0, reason="dask-expr is broken")
Expand Down
Loading