-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed
Labels
Description
What is your issue?
Take this example:
#!/usr/bin/env python
import xarray as xr
import numpy as np
# Create a simple DataArray with 3 dimensions
da = xr.DataArray(
np.random.rand(2, 3, 4),
dims=['x', 'y', 'z'],
coords={'x': [1, 2], 'y': [10, 20, 30], 'z': [100, 200, 300, 400]}
)
print("Original DataArray:")
print(da)
# Stack dimensions to create a MultiIndex
stacked = da.stack(combined=['x', 'y'])
print("\nStacked DataArray:")
print(stacked)
# Try to replace the MultiIndex coordinate with string values
# This should trigger the FutureWarning
string_coords = [f"({x},{y})" for x, y in stacked.combined.to_index()]
print("\nAttempting to assign string coordinates...")
# This raises a FutureWarning..
result = stacked.assign_coords({'combined': string_coords})
print("\nResult:")
print(result)It raises the following warning:
FutureWarning: updating coordinate 'combined' with a PandasMultiIndex would leave the multi-index level coordinates ['x', 'y'] in an inconsistent state. This will raise an error in the future. Use `.drop_vars(['combined', 'x', 'y'])` before assigning new coordinate values.
result = stacked.assign_coords({'combined': string_coords})Whereas the following doesn't raise a FutureWarning:
result = stacked.assign_coords({
'tmp_dim': ('combined', string_coords)
}).swap_dims({
'combined': 'tmp_dim'
}).drop_vars(
['combined'] + ['x', 'y', 'z']
).rename({
'tmp_dim': 'combined'
})I was wondering: Why is that? Could we somehow make the first option not raise that warning? It seems to work as expected anyway.