diff --git a/doc/whats-new.rst b/doc/whats-new.rst index f3ab5d46e1d..ac849c7ec19 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -43,7 +43,8 @@ Bug fixes By `Justus Magin `_. - Promote floating-point numeric datetimes before decoding (:issue:`9179`, :pull:`9182`). By `Justus Magin `_. - +- Fiy static typing of tolerance arguments by allowing `str` type (:issue:`8892`, :pull:`9194`). + By `Michael Niklas `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/core/alignment.py b/xarray/core/alignment.py index 13e3400d170..44fc7319170 100644 --- a/xarray/core/alignment.py +++ b/xarray/core/alignment.py @@ -137,7 +137,7 @@ def __init__( exclude_dims: str | Iterable[Hashable] = frozenset(), exclude_vars: Iterable[Hashable] = frozenset(), method: str | None = None, - tolerance: int | float | Iterable[int | float] | None = None, + tolerance: float | Iterable[float] | str | None = None, copy: bool = True, fill_value: Any = dtypes.NA, sparse: bool = False, @@ -965,7 +965,7 @@ def reindex( obj: T_Alignable, indexers: Mapping[Any, Any], method: str | None = None, - tolerance: int | float | Iterable[int | float] | None = None, + tolerance: float | Iterable[float] | str | None = None, copy: bool = True, fill_value: Any = dtypes.NA, sparse: bool = False, @@ -1004,7 +1004,7 @@ def reindex_like( obj: T_Alignable, other: Dataset | DataArray, method: str | None = None, - tolerance: int | float | Iterable[int | float] | None = None, + tolerance: float | Iterable[float] | str | None = None, copy: bool = True, fill_value: Any = dtypes.NA, ) -> T_Alignable: diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index d3390d26655..b67f8089eb2 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1909,7 +1909,7 @@ def reindex_like( other: T_DataArrayOrSet, *, method: ReindexMethodOptions = None, - tolerance: int | float | Iterable[int | float] | None = None, + tolerance: float | Iterable[float] | str | None = None, copy: bool = True, fill_value=dtypes.NA, ) -> Self: @@ -1936,7 +1936,7 @@ def reindex_like( - backfill / bfill: propagate next valid index value backward - nearest: use nearest valid index value - tolerance : optional + tolerance : float | Iterable[float] | str | None, default: None Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations must satisfy the equation ``abs(index[indexer] - target) <= tolerance``. @@ -2096,7 +2096,7 @@ def reindex( indexers: Mapping[Any, Any] | None = None, *, method: ReindexMethodOptions = None, - tolerance: float | Iterable[float] | None = None, + tolerance: float | Iterable[float] | str | None = None, copy: bool = True, fill_value=dtypes.NA, **indexers_kwargs: Any, @@ -2126,7 +2126,7 @@ def reindex( - backfill / bfill: propagate next valid index value backward - nearest: use nearest valid index value - tolerance : float | Iterable[float] | None, default: None + tolerance : float | Iterable[float] | str | None, default: None Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations must satisfy the equation ``abs(index[indexer] - target) <= tolerance``. diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 0b8be674675..50cfc7b0c29 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -3499,7 +3499,7 @@ def reindex_like( self, other: T_Xarray, method: ReindexMethodOptions = None, - tolerance: int | float | Iterable[int | float] | None = None, + tolerance: float | Iterable[float] | str | None = None, copy: bool = True, fill_value: Any = xrdtypes.NA, ) -> Self: @@ -3526,7 +3526,7 @@ def reindex_like( - "backfill" / "bfill": propagate next valid index value backward - "nearest": use nearest valid index value - tolerance : optional + tolerance : float | Iterable[float] | str | None, default: None Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations must satisfy the equation ``abs(index[indexer] - target) <= tolerance``. @@ -3569,7 +3569,7 @@ def reindex( self, indexers: Mapping[Any, Any] | None = None, method: ReindexMethodOptions = None, - tolerance: int | float | Iterable[int | float] | None = None, + tolerance: float | Iterable[float] | str | None = None, copy: bool = True, fill_value: Any = xrdtypes.NA, **indexers_kwargs: Any, @@ -3594,7 +3594,7 @@ def reindex( - "backfill" / "bfill": propagate next valid index value backward - "nearest": use nearest valid index value - tolerance : optional + tolerance : float | Iterable[float] | str | None, default: None Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations must satisfy the equation ``abs(index[indexer] - target) <= tolerance``. diff --git a/xarray/core/resample.py b/xarray/core/resample.py index ceab0a891c9..ec86f2a283f 100644 --- a/xarray/core/resample.py +++ b/xarray/core/resample.py @@ -66,12 +66,12 @@ def _drop_coords(self) -> T_Xarray: obj = obj.drop_vars([k]) return obj - def pad(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray: + def pad(self, tolerance: float | Iterable[float] | str | None = None) -> T_Xarray: """Forward fill new values at up-sampled frequency. Parameters ---------- - tolerance : float | Iterable[float] | None, default: None + tolerance : float | Iterable[float] | str | None, default: None Maximum distance between original and new labels to limit the up-sampling method. Up-sampled data with indices that satisfy the equation @@ -91,12 +91,14 @@ def pad(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray: ffill = pad - def backfill(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray: + def backfill( + self, tolerance: float | Iterable[float] | str | None = None + ) -> T_Xarray: """Backward fill new values at up-sampled frequency. Parameters ---------- - tolerance : float | Iterable[float] | None, default: None + tolerance : float | Iterable[float] | str | None, default: None Maximum distance between original and new labels to limit the up-sampling method. Up-sampled data with indices that satisfy the equation @@ -116,13 +118,15 @@ def backfill(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray bfill = backfill - def nearest(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray: + def nearest( + self, tolerance: float | Iterable[float] | str | None = None + ) -> T_Xarray: """Take new values from nearest original coordinate to up-sampled frequency coordinates. Parameters ---------- - tolerance : float | Iterable[float] | None, default: None + tolerance : float | Iterable[float] | str | None, default: None Maximum distance between original and new labels to limit the up-sampling method. Up-sampled data with indices that satisfy the equation diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index 47cda064143..f0a0fd14d9d 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -2037,17 +2037,17 @@ def test_upsample_tolerance(self) -> None: array = DataArray(np.arange(2), [("time", times)]) # Forward fill - actual = array.resample(time="6h").ffill(tolerance="12h") # type: ignore[arg-type] # TODO: tolerance also allows strings, same issue in .reindex. + actual = array.resample(time="6h").ffill(tolerance="12h") expected = DataArray([0.0, 0.0, 0.0, np.nan, 1.0], [("time", times_upsampled)]) assert_identical(expected, actual) # Backward fill - actual = array.resample(time="6h").bfill(tolerance="12h") # type: ignore[arg-type] # TODO: tolerance also allows strings, same issue in .reindex. + actual = array.resample(time="6h").bfill(tolerance="12h") expected = DataArray([0.0, np.nan, 1.0, 1.0, 1.0], [("time", times_upsampled)]) assert_identical(expected, actual) # Nearest - actual = array.resample(time="6h").nearest(tolerance="6h") # type: ignore[arg-type] # TODO: tolerance also allows strings, same issue in .reindex. + actual = array.resample(time="6h").nearest(tolerance="6h") expected = DataArray([0, 0, np.nan, 1, 1], [("time", times_upsampled)]) assert_identical(expected, actual)