Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make more args kw only (except 'dim') #6403

Merged
merged 16 commits into from
Oct 5, 2023
Merged
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ New Features
Breaking changes
~~~~~~~~~~~~~~~~

- Made more arguments keyword-only (e.g. ``keep_attrs``, ``skipna``) for many :py:class:`xarray.DataArray` and
:py:class:`xarray.Dataset` methods (:pull:`6403`). By `Mathias Hauser <https://github.com/mathause>`_.
- :py:meth:`Dataset.to_zarr` & :py:meth:`DataArray.to_zarr` require keyword
arguments after the initial 7 positional arguments.
By `Maximilian Roos <https://github.com/max-sixty>`_.
Expand Down
34 changes: 33 additions & 1 deletion xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
)
from xarray.plot.accessor import DataArrayPlotAccessor
from xarray.plot.utils import _get_units_from_attrs
from xarray.util.deprecation_helpers import _deprecate_positional_args

if TYPE_CHECKING:
from typing import TypeVar, Union
Expand Down Expand Up @@ -954,6 +955,7 @@ def coords(self) -> DataArrayCoordinates:
def reset_coords(
self,
names: Dims = None,
*,
drop: Literal[False] = False,
) -> Dataset:
...
Expand All @@ -967,9 +969,11 @@ def reset_coords(
) -> Self:
...

@_deprecate_positional_args("v2023.10.0")
def reset_coords(
self,
names: Dims = None,
*,
drop: bool = False,
) -> Self | Dataset:
"""Given names of coordinates, reset them to become variables.
Expand Down Expand Up @@ -1287,9 +1291,11 @@ def chunksizes(self) -> Mapping[Any, tuple[int, ...]]:
all_variables = [self.variable] + [c.variable for c in self.coords.values()]
return get_chunksizes(all_variables)

@_deprecate_positional_args("v2023.10.0")
def chunk(
self,
chunks: T_Chunks = {}, # {} even though it's technically unsafe, is being used intentionally here (#4667)
*,
name_prefix: str = "xarray-",
token: str | None = None,
lock: bool = False,
Expand Down Expand Up @@ -1724,9 +1730,11 @@ def thin(
ds = self._to_temp_dataset().thin(indexers, **indexers_kwargs)
return self._from_temp_dataset(ds)

@_deprecate_positional_args("v2023.10.0")
def broadcast_like(
self,
other: T_DataArrayOrSet,
*,
exclude: Iterable[Hashable] | None = None,
) -> Self:
"""Broadcast this DataArray against another Dataset or DataArray.
Expand Down Expand Up @@ -1835,9 +1843,11 @@ def _reindex_callback(

return da

@_deprecate_positional_args("v2023.10.0")
def reindex_like(
self,
other: T_DataArrayOrSet,
*,
method: ReindexMethodOptions = None,
tolerance: int | float | Iterable[int | float] | None = None,
copy: bool = True,
Expand Down Expand Up @@ -2005,9 +2015,11 @@ def reindex_like(
fill_value=fill_value,
)

@_deprecate_positional_args("v2023.10.0")
def reindex(
self,
indexers: Mapping[Any, Any] | None = None,
*,
method: ReindexMethodOptions = None,
tolerance: float | Iterable[float] | None = None,
copy: bool = True,
Expand Down Expand Up @@ -2787,9 +2799,11 @@ def stack(
)
return self._from_temp_dataset(ds)

@_deprecate_positional_args("v2023.10.0")
def unstack(
self,
dim: Dims = None,
*,
fill_value: Any = dtypes.NA,
sparse: bool = False,
) -> Self:
Expand Down Expand Up @@ -2847,7 +2861,7 @@ def unstack(
--------
DataArray.stack
"""
ds = self._to_temp_dataset().unstack(dim, fill_value, sparse)
ds = self._to_temp_dataset().unstack(dim, fill_value=fill_value, sparse=sparse)
return self._from_temp_dataset(ds)

def to_unstacked_dataset(self, dim: Hashable, level: int | Hashable = 0) -> Dataset:
Expand Down Expand Up @@ -3198,9 +3212,11 @@ def drop_isel(
dataset = dataset.drop_isel(indexers=indexers, **indexers_kwargs)
return self._from_temp_dataset(dataset)

@_deprecate_positional_args("v2023.10.0")
def dropna(
self,
dim: Hashable,
*,
how: Literal["any", "all"] = "any",
thresh: int | None = None,
) -> Self:
Expand Down Expand Up @@ -4696,10 +4712,12 @@ def _title_for_slice(self, truncate: int = 50) -> str:

return title

@_deprecate_positional_args("v2023.10.0")
def diff(
self,
dim: Hashable,
n: int = 1,
*,
label: Literal["upper", "lower"] = "upper",
) -> Self:
"""Calculate the n-th order discrete difference along given axis.
Expand Down Expand Up @@ -4985,10 +5003,12 @@ def sortby(
ds = self._to_temp_dataset().sortby(variables, ascending=ascending)
return self._from_temp_dataset(ds)

@_deprecate_positional_args("v2023.10.0")
def quantile(
self,
q: ArrayLike,
dim: Dims = None,
*,
method: QuantileMethods = "linear",
keep_attrs: bool | None = None,
skipna: bool | None = None,
Expand Down Expand Up @@ -5103,9 +5123,11 @@ def quantile(
)
return self._from_temp_dataset(ds)

@_deprecate_positional_args("v2023.10.0")
def rank(
self,
dim: Hashable,
*,
pct: bool = False,
keep_attrs: bool | None = None,
) -> Self:
Expand Down Expand Up @@ -5678,9 +5700,11 @@ def pad(
)
return self._from_temp_dataset(ds)

@_deprecate_positional_args("v2023.10.0")
def idxmin(
self,
dim: Hashable | None = None,
*,
skipna: bool | None = None,
fill_value: Any = dtypes.NA,
keep_attrs: bool | None = None,
Expand Down Expand Up @@ -5774,9 +5798,11 @@ def idxmin(
keep_attrs=keep_attrs,
)

@_deprecate_positional_args("v2023.10.0")
def idxmax(
self,
dim: Hashable = None,
*,
skipna: bool | None = None,
fill_value: Any = dtypes.NA,
keep_attrs: bool | None = None,
Expand Down Expand Up @@ -5870,9 +5896,11 @@ def idxmax(
keep_attrs=keep_attrs,
)

@_deprecate_positional_args("v2023.10.0")
def argmin(
self,
dim: Dims = None,
*,
axis: int | None = None,
keep_attrs: bool | None = None,
skipna: bool | None = None,
Expand Down Expand Up @@ -5970,9 +5998,11 @@ def argmin(
else:
return self._replace_maybe_drop_dims(result)

@_deprecate_positional_args("v2023.10.0")
def argmax(
self,
dim: Dims = None,
*,
axis: int | None = None,
keep_attrs: bool | None = None,
skipna: bool | None = None,
Expand Down Expand Up @@ -6317,9 +6347,11 @@ def curvefit(
kwargs=kwargs,
)

@_deprecate_positional_args("v2023.10.0")
def drop_duplicates(
self,
dim: Hashable | Iterable[Hashable],
*,
keep: Literal["first", "last", False] = "first",
) -> Self:
"""Returns a new DataArray with duplicate dimension values removed.
Expand Down
19 changes: 19 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
calculate_dimensions,
)
from xarray.plot.accessor import DatasetPlotAccessor
from xarray.util.deprecation_helpers import _deprecate_positional_args

if TYPE_CHECKING:
from numpy.typing import ArrayLike
Expand Down Expand Up @@ -4775,9 +4776,11 @@ def set_index(
variables, coord_names=coord_names, indexes=indexes_
)

@_deprecate_positional_args("v2023.10.0")
def reset_index(
self,
dims_or_levels: Hashable | Sequence[Hashable],
*,
drop: bool = False,
) -> Self:
"""Reset the specified index(es) or multi-index level(s).
Expand Down Expand Up @@ -5412,9 +5415,11 @@ def _unstack_full_reindex(
variables, coord_names=coord_names, indexes=indexes
)

@_deprecate_positional_args("v2023.10.0")
def unstack(
self,
dim: Dims = None,
*,
fill_value: Any = xrdtypes.NA,
sparse: bool = False,
) -> Self:
Expand Down Expand Up @@ -6155,9 +6160,11 @@ def transpose(
ds._variables[name] = var.transpose(*var_dims)
return ds

@_deprecate_positional_args("v2023.10.0")
def dropna(
self,
dim: Hashable,
*,
how: Literal["any", "all"] = "any",
thresh: int | None = None,
subset: Iterable[Hashable] | None = None,
Expand Down Expand Up @@ -7583,10 +7590,12 @@ def _copy_attrs_from(self, other):
if v in self.variables:
self.variables[v].attrs = other.variables[v].attrs

@_deprecate_positional_args("v2023.10.0")
def diff(
self,
dim: Hashable,
n: int = 1,
*,
label: Literal["upper", "lower"] = "upper",
) -> Self:
"""Calculate the n-th order discrete difference along given axis.
Expand Down Expand Up @@ -7913,10 +7922,12 @@ def sortby(
indices[key] = order if ascending else order[::-1]
return aligned_self.isel(indices)

@_deprecate_positional_args("v2023.10.0")
def quantile(
self,
q: ArrayLike,
dim: Dims = None,
*,
method: QuantileMethods = "linear",
numeric_only: bool = False,
keep_attrs: bool | None = None,
Expand Down Expand Up @@ -8091,9 +8102,11 @@ def quantile(
)
return new.assign_coords(quantile=q)

@_deprecate_positional_args("v2023.10.0")
def rank(
self,
dim: Hashable,
*,
pct: bool = False,
keep_attrs: bool | None = None,
) -> Self:
Expand Down Expand Up @@ -9037,9 +9050,11 @@ def pad(
attrs = self._attrs if keep_attrs else None
return self._replace_with_new_dims(variables, indexes=indexes, attrs=attrs)

@_deprecate_positional_args("v2023.10.0")
def idxmin(
self,
dim: Hashable | None = None,
*,
skipna: bool | None = None,
fill_value: Any = xrdtypes.NA,
keep_attrs: bool | None = None,
Expand Down Expand Up @@ -9134,9 +9149,11 @@ def idxmin(
)
)

@_deprecate_positional_args("v2023.10.0")
def idxmax(
self,
dim: Hashable | None = None,
*,
skipna: bool | None = None,
fill_value: Any = xrdtypes.NA,
keep_attrs: bool | None = None,
Expand Down Expand Up @@ -9757,9 +9774,11 @@ def _wrapper(Y, *args, **kwargs):

return result

@_deprecate_positional_args("v2023.10.0")
def drop_duplicates(
self,
dim: Hashable | Iterable[Hashable],
*,
keep: Literal["first", "last", False] = "first",
) -> Self:
"""Returns a new Dataset with duplicate dimension values removed.
Expand Down
3 changes: 3 additions & 0 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
peek_at,
)
from xarray.core.variable import IndexVariable, Variable
from xarray.util.deprecation_helpers import _deprecate_positional_args

if TYPE_CHECKING:
from numpy.typing import ArrayLike
Expand Down Expand Up @@ -1092,10 +1093,12 @@ def fillna(self, value: Any) -> T_Xarray:
"""
return ops.fillna(self, value)

@_deprecate_positional_args("v2023.10.0")
def quantile(
self,
q: ArrayLike,
dim: Dims = None,
*,
method: QuantileMethods = "linear",
keep_attrs: bool | None = None,
skipna: bool | None = None,
Expand Down
Loading
Loading