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

xarray polyfit time coordinate treatment #293

Closed
JGuetschow opened this issue Nov 12, 2024 · 3 comments
Closed

xarray polyfit time coordinate treatment #293

JGuetschow opened this issue Nov 12, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@JGuetschow
Copy link
Contributor

When trying to solve #292 I found an unexpected behaviour in xarray's polyfit routine. This issue is to discuss how we deal with that

In issue #292 I found the reason for the problem with using xr.polyfit and xr.polyval. It's not (only) the time resolution as I first suspected but a coordinate recomputation in xr.polyfit. The function _floatize_x (https://github.com/pydata/xarray/blob/91962d6aec380cb83fe80b2afdfa556efdd817a3/xarray/core/missing.py#L585) shifts the time coordinate such that the minimal value is 0 to avoid accuracy problems as np.datetime64 uses 64 bit integers which can not be represented by 64 bit floats with the necessary accuracy. All further computations are done with the shifted coordinates and thus the coefficients are also computed in relation to the shifted coordinates. 6As 1970 is zero this case worked while other dates did not work. The following code takes the shifting of _floatize_x into account:

def test_temp_polyval():
    test_ts = xr.DataArray(
        np.linspace(6, 12, 11),
        coords={"time": pd.date_range("1956-01-01", "1966-01-01", freq="YS")},
        dims="time",
        name="test_ts",
    )

    time_to_eval = np.datetime64('1957-01-01')

    fit = test_ts.polyfit(dim='time', deg=1, skipna=True)
    value = xr.polyval(
        test_ts.coords['time'].loc[{'time': [time_to_eval]}]-test_ts.coords['time'].data[0],
        fit.polyfit_coefficients
    )

    value.name='test_ts' # for assertion

    assert_aligned_equal(
        test_ts.loc[{'time': [time_to_eval]}],
        value,
        rtol=1e-03,
    )
    return None

I still think this is very weird behaviour. Maybe I missed a hint when reading the docs.

Originally posted by @JGuetschow in #292 (comment)

I personally think this is bug in xarray, but before I open an issue, maybe you can add your view @mikapfl?

@JGuetschow JGuetschow added the bug Something isn't working label Nov 12, 2024
@JGuetschow JGuetschow added this to the Composite Source Generator milestone Nov 12, 2024
@JGuetschow
Copy link
Contributor Author

This behaviour is only in xarray 2024.10.0 and not in 2024.9.0. I have excluded 2024.10.0 for now.

@JGuetschow
Copy link
Contributor Author

Already fixed in xarray just not released yet. As we have to change the allowed xarray versions I'll keep this open until the bug has been released.

@JGuetschow
Copy link
Contributor Author

I've now masked only the problematic version and everything should work fine. So I'll close the issue now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant