xarray icon indicating copy to clipboard operation
xarray copied to clipboard

Add new kwarg `drop_existing` to `set_xindex`

Open ianhi opened this issue 2 months ago • 1 comments

Is your feature request related to a problem?

Currently if you want to set an xindex on a coord that already has an index you have to remember the slightly differently named drop_indexes and to write out all the coord names twice.

da = xr.DataArray([0,1,2,3], dims=('x'), coords={'x': np.linspace(0,1,4)})

# need to repeat all the variables
da.drop_indexes('x').set_xindex('x', CustomIndex)

# ValueError: those coordinates already have an index: {'x'}
da.set_xindex('x', CustomIndex)

Describe the solution you'd like

Give set_xindex a drop_existing argument (default to False) that automatically drops whatever is necessary to complete the index setting the user requested.

Describe alternatives you've considered

No response

Additional context

No response

ianhi avatar Dec 12 '25 16:12 ianhi

should set_index be an upsert?

max-sixty avatar Dec 12 '25 20:12 max-sixty