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

Use the same function to floatize coords in polyfit and polyval #9691

Merged
merged 7 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
_contains_datetime_like_objects,
get_chunksizes,
)
from xarray.core.computation import unify_chunks
from xarray.core.computation import _ensure_numeric, unify_chunks
from xarray.core.coordinates import (
Coordinates,
DatasetCoordinates,
Expand All @@ -87,7 +87,6 @@
merge_coordinates_without_align,
merge_core,
)
from xarray.core.missing import _floatize_x
from xarray.core.options import OPTIONS, _get_keep_attrs
from xarray.core.types import (
Bins,
Expand Down Expand Up @@ -9066,15 +9065,7 @@ def polyfit(
variables = {}
skipna_da = skipna

x: Any = self.coords[dim].variable
x = _floatize_x((x,), (x,))[0][0]

try:
x = x.values.astype(np.float64)
except TypeError as e:
raise TypeError(
f"Dim {dim!r} must be castable to float64, got {type(x).__name__}."
) from e
x: Any = _ensure_numeric(self.coords[dim]).astype(np.float64)
aulemahal marked this conversation as resolved.
Show resolved Hide resolved

xname = f"{self[dim].name}_"
order = int(deg) + 1
Expand Down
26 changes: 26 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6720,6 +6720,32 @@ def test_polyfit_warnings(self) -> None:
ds.var1.polyfit("dim2", 10, full=True)
assert len(ws) == 1

def test_polyfit_polyval(self) -> None:
da = xr.DataArray([1.0, 2.0, 3.0], dims=["x"], coords=dict(x=[0, 1, 2]))

out = da.polyfit("x", 3, full=False)
da_fitval = xr.polyval(da.x, out.polyfit_coefficients)
# polyval introduces very small errors (1e-16 here)
np.testing.assert_allclose(da_fitval, da)
aulemahal marked this conversation as resolved.
Show resolved Hide resolved

da = da.assign_coords(x=xr.date_range("2001-01-01", periods=3, freq="YS"))
out = da.polyfit("x", 3, full=False)
da_fitval = xr.polyval(da.x, out.polyfit_coefficients)
np.testing.assert_allclose(da_fitval, da)

@pytest.mark.skipif(not has_cftime, reason="Test requires cftime.")
aulemahal marked this conversation as resolved.
Show resolved Hide resolved
def test_polyfit_polyval_cftime(self) -> None:
da = xr.DataArray(
[1.0, 2.0, 3.0],
dims=["x"],
coords=dict(
x=xr.date_range("2001-01-01", periods=3, freq="YS", calendar="noleap")
),
)
out = da.polyfit("x", 3, full=False)
da_fitval = xr.polyval(da.x, out.polyfit_coefficients)
np.testing.assert_allclose(da_fitval, da)

@staticmethod
def _test_data_var_interior(
original_data_var, padded_data_var, padded_dim_name, expected_pad_values
Expand Down
Loading