Stream: xarray

Topic: polyfit


view this post on Zulip Katie Dagon (May 11 2021 at 23:45):

Does anyone have a working example using xarray.DataArray.polyfit (https://xarray.pydata.org/en/latest/generated/xarray.DataArray.polyfit.html), especially when the time coordinate uses cftime.Datetime? I'm trying to calculate the linear trend of temperature over time at each lat/lon gridpoint. I have monthly temperature data (K) so I assumed the coefficients would output as K/month, but the first degree polyfit_coefficients are order 10^-18 which is way too small. I'm wondering if cftime is using some base time units or reference time that causes the magnitude to be small, because the spatial pattern actually looks ok. I'm using xarray version 17.0 which seems to have polyfit implemented accounting for datetime (see also https://github.com/pydata/xarray/issues/3349).

view this post on Zulip Katie Dagon (May 12 2021 at 00:13):

As an update, it appears the baseunits are in nanoseconds(!), which I figured out by manually differencing the first and last time points. Once I convert from K/ns to K/month the magnitude of the coefficients look reasonable.
The output of polyfit doesn't have any units attribute, which is why this was a bit tricky to interpret. cc @Deepak Cherian

view this post on Zulip Deepak Cherian (May 12 2021 at 15:29):

:see_no_evil: It's hard to set units on the output because we need units on the input . This should be in the docstring though (https://github.com/pydata/xarray/issues/4455). This would be a great first pull request!

view this post on Zulip Katie Dagon (May 12 2021 at 15:33):

Thanks @Deepak Cherian , I can't believe I didn't find this issue before, but it is describing exactly what I was trying to figure out :slight_smile:


Last updated: May 16 2025 at 17:14 UTC