Skip to content

Commit fff8253

Browse files
authored
Allow str in static typing of reindex, ffill etc. (#9194)
* allow str in reindex * add whats-new
1 parent 3deee7b commit fff8253

File tree

6 files changed

+26
-21
lines changed

6 files changed

+26
-21
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ Bug fixes
4343
By `Justus Magin <https://github.com/keewis>`_.
4444
- Promote floating-point numeric datetimes before decoding (:issue:`9179`, :pull:`9182`).
4545
By `Justus Magin <https://github.com/keewis>`_.
46-
46+
- Fiy static typing of tolerance arguments by allowing `str` type (:issue:`8892`, :pull:`9194`).
47+
By `Michael Niklas <https://github.com/headtr1ck>`_.
4748

4849
Documentation
4950
~~~~~~~~~~~~~

xarray/core/alignment.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def __init__(
137137
exclude_dims: str | Iterable[Hashable] = frozenset(),
138138
exclude_vars: Iterable[Hashable] = frozenset(),
139139
method: str | None = None,
140-
tolerance: int | float | Iterable[int | float] | None = None,
140+
tolerance: float | Iterable[float] | str | None = None,
141141
copy: bool = True,
142142
fill_value: Any = dtypes.NA,
143143
sparse: bool = False,
@@ -965,7 +965,7 @@ def reindex(
965965
obj: T_Alignable,
966966
indexers: Mapping[Any, Any],
967967
method: str | None = None,
968-
tolerance: int | float | Iterable[int | float] | None = None,
968+
tolerance: float | Iterable[float] | str | None = None,
969969
copy: bool = True,
970970
fill_value: Any = dtypes.NA,
971971
sparse: bool = False,
@@ -1004,7 +1004,7 @@ def reindex_like(
10041004
obj: T_Alignable,
10051005
other: Dataset | DataArray,
10061006
method: str | None = None,
1007-
tolerance: int | float | Iterable[int | float] | None = None,
1007+
tolerance: float | Iterable[float] | str | None = None,
10081008
copy: bool = True,
10091009
fill_value: Any = dtypes.NA,
10101010
) -> T_Alignable:

xarray/core/dataarray.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,7 +1909,7 @@ def reindex_like(
19091909
other: T_DataArrayOrSet,
19101910
*,
19111911
method: ReindexMethodOptions = None,
1912-
tolerance: int | float | Iterable[int | float] | None = None,
1912+
tolerance: float | Iterable[float] | str | None = None,
19131913
copy: bool = True,
19141914
fill_value=dtypes.NA,
19151915
) -> Self:
@@ -1936,7 +1936,7 @@ def reindex_like(
19361936
- backfill / bfill: propagate next valid index value backward
19371937
- nearest: use nearest valid index value
19381938
1939-
tolerance : optional
1939+
tolerance : float | Iterable[float] | str | None, default: None
19401940
Maximum distance between original and new labels for inexact
19411941
matches. The values of the index at the matching locations must
19421942
satisfy the equation ``abs(index[indexer] - target) <= tolerance``.
@@ -2096,7 +2096,7 @@ def reindex(
20962096
indexers: Mapping[Any, Any] | None = None,
20972097
*,
20982098
method: ReindexMethodOptions = None,
2099-
tolerance: float | Iterable[float] | None = None,
2099+
tolerance: float | Iterable[float] | str | None = None,
21002100
copy: bool = True,
21012101
fill_value=dtypes.NA,
21022102
**indexers_kwargs: Any,
@@ -2126,7 +2126,7 @@ def reindex(
21262126
- backfill / bfill: propagate next valid index value backward
21272127
- nearest: use nearest valid index value
21282128
2129-
tolerance : float | Iterable[float] | None, default: None
2129+
tolerance : float | Iterable[float] | str | None, default: None
21302130
Maximum distance between original and new labels for inexact
21312131
matches. The values of the index at the matching locations must
21322132
satisfy the equation ``abs(index[indexer] - target) <= tolerance``.

xarray/core/dataset.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3499,7 +3499,7 @@ def reindex_like(
34993499
self,
35003500
other: T_Xarray,
35013501
method: ReindexMethodOptions = None,
3502-
tolerance: int | float | Iterable[int | float] | None = None,
3502+
tolerance: float | Iterable[float] | str | None = None,
35033503
copy: bool = True,
35043504
fill_value: Any = xrdtypes.NA,
35053505
) -> Self:
@@ -3526,7 +3526,7 @@ def reindex_like(
35263526
- "backfill" / "bfill": propagate next valid index value backward
35273527
- "nearest": use nearest valid index value
35283528
3529-
tolerance : optional
3529+
tolerance : float | Iterable[float] | str | None, default: None
35303530
Maximum distance between original and new labels for inexact
35313531
matches. The values of the index at the matching locations must
35323532
satisfy the equation ``abs(index[indexer] - target) <= tolerance``.
@@ -3569,7 +3569,7 @@ def reindex(
35693569
self,
35703570
indexers: Mapping[Any, Any] | None = None,
35713571
method: ReindexMethodOptions = None,
3572-
tolerance: int | float | Iterable[int | float] | None = None,
3572+
tolerance: float | Iterable[float] | str | None = None,
35733573
copy: bool = True,
35743574
fill_value: Any = xrdtypes.NA,
35753575
**indexers_kwargs: Any,
@@ -3594,7 +3594,7 @@ def reindex(
35943594
- "backfill" / "bfill": propagate next valid index value backward
35953595
- "nearest": use nearest valid index value
35963596
3597-
tolerance : optional
3597+
tolerance : float | Iterable[float] | str | None, default: None
35983598
Maximum distance between original and new labels for inexact
35993599
matches. The values of the index at the matching locations must
36003600
satisfy the equation ``abs(index[indexer] - target) <= tolerance``.

xarray/core/resample.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ def _drop_coords(self) -> T_Xarray:
6666
obj = obj.drop_vars([k])
6767
return obj
6868

69-
def pad(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray:
69+
def pad(self, tolerance: float | Iterable[float] | str | None = None) -> T_Xarray:
7070
"""Forward fill new values at up-sampled frequency.
7171
7272
Parameters
7373
----------
74-
tolerance : float | Iterable[float] | None, default: None
74+
tolerance : float | Iterable[float] | str | None, default: None
7575
Maximum distance between original and new labels to limit
7676
the up-sampling method.
7777
Up-sampled data with indices that satisfy the equation
@@ -91,12 +91,14 @@ def pad(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray:
9191

9292
ffill = pad
9393

94-
def backfill(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray:
94+
def backfill(
95+
self, tolerance: float | Iterable[float] | str | None = None
96+
) -> T_Xarray:
9597
"""Backward fill new values at up-sampled frequency.
9698
9799
Parameters
98100
----------
99-
tolerance : float | Iterable[float] | None, default: None
101+
tolerance : float | Iterable[float] | str | None, default: None
100102
Maximum distance between original and new labels to limit
101103
the up-sampling method.
102104
Up-sampled data with indices that satisfy the equation
@@ -116,13 +118,15 @@ def backfill(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray
116118

117119
bfill = backfill
118120

119-
def nearest(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray:
121+
def nearest(
122+
self, tolerance: float | Iterable[float] | str | None = None
123+
) -> T_Xarray:
120124
"""Take new values from nearest original coordinate to up-sampled
121125
frequency coordinates.
122126
123127
Parameters
124128
----------
125-
tolerance : float | Iterable[float] | None, default: None
129+
tolerance : float | Iterable[float] | str | None, default: None
126130
Maximum distance between original and new labels to limit
127131
the up-sampling method.
128132
Up-sampled data with indices that satisfy the equation

xarray/tests/test_groupby.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,17 +2037,17 @@ def test_upsample_tolerance(self) -> None:
20372037
array = DataArray(np.arange(2), [("time", times)])
20382038

20392039
# Forward fill
2040-
actual = array.resample(time="6h").ffill(tolerance="12h") # type: ignore[arg-type] # TODO: tolerance also allows strings, same issue in .reindex.
2040+
actual = array.resample(time="6h").ffill(tolerance="12h")
20412041
expected = DataArray([0.0, 0.0, 0.0, np.nan, 1.0], [("time", times_upsampled)])
20422042
assert_identical(expected, actual)
20432043

20442044
# Backward fill
2045-
actual = array.resample(time="6h").bfill(tolerance="12h") # type: ignore[arg-type] # TODO: tolerance also allows strings, same issue in .reindex.
2045+
actual = array.resample(time="6h").bfill(tolerance="12h")
20462046
expected = DataArray([0.0, np.nan, 1.0, 1.0, 1.0], [("time", times_upsampled)])
20472047
assert_identical(expected, actual)
20482048

20492049
# Nearest
2050-
actual = array.resample(time="6h").nearest(tolerance="6h") # type: ignore[arg-type] # TODO: tolerance also allows strings, same issue in .reindex.
2050+
actual = array.resample(time="6h").nearest(tolerance="6h")
20512051
expected = DataArray([0, 0, np.nan, 1, 1], [("time", times_upsampled)])
20522052
assert_identical(expected, actual)
20532053

0 commit comments

Comments
 (0)