Skip to content

Commit

Permalink
Allow str in static typing of reindex, ffill etc. (#9194)
Browse files Browse the repository at this point in the history
* allow str in reindex

* add whats-new
  • Loading branch information
headtr1ck authored Jun 30, 2024
1 parent 3deee7b commit fff8253
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 21 deletions.
3 changes: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ Bug fixes
By `Justus Magin <https://github.com/keewis>`_.
- Promote floating-point numeric datetimes before decoding (:issue:`9179`, :pull:`9182`).
By `Justus Magin <https://github.com/keewis>`_.

- Fiy static typing of tolerance arguments by allowing `str` type (:issue:`8892`, :pull:`9194`).
By `Michael Niklas <https://github.com/headtr1ck>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
6 changes: 3 additions & 3 deletions xarray/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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``.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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``.
Expand Down
8 changes: 4 additions & 4 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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``.
Expand Down Expand Up @@ -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,
Expand All @@ -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``.
Expand Down
16 changes: 10 additions & 6 deletions xarray/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit fff8253

Please sign in to comment.