-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed
Labels
Description
What happened?
ffill, bfill reindex etc. have tolerance arguments that also supports strings. And we test for it here:
xarray/xarray/tests/test_groupby.py
Lines 2016 to 2025 in 2120808
| def test_upsample_tolerance(self): | |
| # Test tolerance keyword for upsample methods bfill, pad, nearest | |
| times = pd.date_range("2000-01-01", freq="1D", periods=2) | |
| times_upsampled = pd.date_range("2000-01-01", freq="6h", periods=5) | |
| array = DataArray(np.arange(2), [("time", times)]) | |
| # Forward fill | |
| 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) |
But our typing assumes it's floats only:
xarray/xarray/core/resample.py
Lines 69 to 94 in 2120808
| def pad(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray: | |
| """Forward fill new values at up-sampled frequency. | |
| Parameters | |
| ---------- | |
| tolerance : float | Iterable[float] | 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 | |
| ``abs(index[indexer] - target) <= tolerance`` are filled by | |
| new values. Data with indices that are outside the given | |
| tolerance are filled with ``NaN`` s. | |
| Returns | |
| ------- | |
| padded : DataArray or Dataset | |
| """ | |
| obj = self._drop_coords() | |
| (grouper,) = self.groupers | |
| return obj.reindex( | |
| {self._dim: grouper.full_index}, method="pad", tolerance=tolerance | |
| ) | |
| ffill = pad | |
| def backfill(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray: |
What did you expect to happen?
Since our pytests pass, mypy should pass as well.
Minimal Complete Verifiable Example
import numpy as np
import pandas as pd
import xarray as xr
# https://github.com/pydata/xarray/blob/2120808bbe45f3d4f0b6a01cd43bac4df4039092/xarray/tests/test_groupby.py#L2016
# Test tolerance keyword for upsample methods bfill, pad, nearest
times = pd.date_range("2000-01-01", freq="1D", periods=2)
times_upsampled = pd.date_range("2000-01-01", freq="6h", periods=5)
array = xr.DataArray(np.arange(2), [("time", times)])
# Forward fill
actual = array.resample(time="6h").ffill(tolerance="12h")
expected = xr.DataArray([0.0, 0.0, 0.0, np.nan, 1.0], [("time", times_upsampled)])
xr.testing.assert_identical(expected, actual)Environment
master