Skip to content

Consider accepting a positional dict argument in place of keyword arguments #1231

Closed
@shoyer

Description

@shoyer

Using **kwargs in xarray operations is incredibly convenient, but it means there is no way to use a keyword argument that conflicts with an existing argument name. For example, there is no way to use .sel to select along 'method', 'tolerance' or 'drop' dimensions.

For interactive use, this is fine -- these reserved names are rarely used and it's nice to save the keystrokes it takes to write dict(). But it's a problem for writing reliable complex software, which may be calling xarray methods from somewhere very different. In fact, knowing how this works, it's possible to trigger bugs without even using **kwargs, e.g., by indexing a DataArray:

In [34]: array = xr.DataArray([1, 2, 3], dims='drop')

# result should be a scalar!
In [35]: array[0]
Out[35]:
<xarray.DataArray (drop: 3)>
array([1, 2, 3])
Unindexed dimensions:
    drop

One option to resolve this is to make the first argument to every function like this (including sel, isel, sel_points, isel_points, stack, ...) accept a dictionary, which is interpreted exactly like **kwargs. In fact, we already do this in a highly inconsistent fashion for Dataset.reindex only. For all cases where axis names are set dynamically (and certainly within xarray), we could encourage using the dictionary form, e.g., array.isel(kwargs, drop=True) rather than array.isel(drop=True, **kwargs).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions