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

ffill's tolerance argument can be strings #8892

Closed
Illviljan opened this issue Mar 29, 2024 · 1 comment · Fixed by #9194
Closed

ffill's tolerance argument can be strings #8892

Illviljan opened this issue Mar 29, 2024 · 1 comment · Fixed by #9194

Comments

@Illviljan
Copy link
Contributor

Illviljan commented Mar 29, 2024

What happened?

ffill, bfill reindex etc. have tolerance arguments that also supports strings. And we test for it here:

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:

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

@Illviljan Illviljan added bug needs triage Issue that has not been reviewed by xarray team member labels Mar 29, 2024
Copy link

welcome bot commented Mar 29, 2024

Thanks for opening your first issue here at xarray! Be sure to follow the issue template!
If you have an idea for a solution, we would really welcome a Pull Request with proposed changes.
See the Contributing Guide for more.
It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better.
Thank you!

@Illviljan Illviljan changed the title tolerance arguments can be strings ffill's tolerance argument can be strings Mar 29, 2024
@dcherian dcherian removed the needs triage Issue that has not been reviewed by xarray team member label Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants