Skip to content

how to remove polynomial fit over cftime dimension? #5016

Answered by spencerkclark
aaronspring asked this question in Q&A
Discussion options

You must be logged in to vote

I think xarray.polyval handles this gracefully. A bonus is that you also don't need to write the logic to evaluate the polynomials yourself. The one catch is that it only operates on DataArrays of coefficients, but that is straightforward to work around:

def rm_poly(ds, dim, deg=2, **kwargs):
    """Remove degree polynomial along dimension dim from ds."""
    coefficients = ds.polyfit(dim, deg=deg, **kwargs)
    coord = ds[dim]
    fits = []
    for v in coefficients:
        name = v.replace("_polyfit_coefficients", "")
        fit = xr.polyval(coord, coefficients[v]).rename(name)
        fits.append(fit)
    fits = xr.merge(fits)
    return ds - fits

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@aaronspring
Comment options

@spencerkclark
Comment options

@aaronspring
Comment options

@spencerkclark
Comment options

Answer selected by dcherian
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants