xarray icon indicating copy to clipboard operation
xarray copied to clipboard

DataArrayResample.interpolate and passing kwargs

Open stackjohn opened this issue 1 year ago • 2 comments

What is your issue?

The docs here state that it is possible to use polynomial interpolation when resampling.

If you try this though:

data_per_minute = ds.resample(
    valid_time="1min"
).interpolate("polynomial")

You get the following:

ValueError: order is required when method=polynomial

The docs here state that you need to pass in order, how is this possible?

If you then try:

data_per_minute = ds.resample(
    valid_time="1min"
).interpolate(kind="polynomial", order=3)

You get:

TypeError: Resample.interpolate() got an unexpected keyword argument 'order'

How can the order be specified? Thank you in advance.

stackjohn avatar Feb 17 '24 20:02 stackjohn

import xarray as xr
import pandas as pd 
import numpy as np

Reproduce the bug

Example taken from DataArray.resample, adapted to name the dimension valid_time

xda = xr.DataArray(
    np.linspace(0, 11, num=12),
    coords={"valid_time":
        pd.date_range(
            "1999-12-15",
            periods=12,
            freq=pd.DateOffset(months=1),
        )
    },
)
print(xda)
try:
    data_per_day = xda.resample(valid_time="1D").interpolate("polynomial")
except ValueError as err:
    assert str(err) == "order is required when method=polynomial"
try:
    data_per_day = xda.resample(valid_time="1D").interpolate(kind="polynomial", order=3)
except TypeError as err:
    assert (
        str(err) == "Resample.interpolate() got an unexpected keyword argument 'order'"
    )

Technical Analysis

First error message

"order is required when method=polynomial"

Source: https://github.com/pydata/xarray/blob/main/xarray/core/missing.py#L153

It seems that the method argument becomes the order one when method == 'polynomial'. It is unclear to me what the order argument is supposed to contain. Maybe @jhamman have an idea? This seems to have been introduced in https://github.com/pydata/xarray/pull/1640

Second error message:

"Resample.interpolate() got an unexpected keyword argument 'order'"

Check https://docs.xarray.dev/en/stable/generated/xarray.core.resample.DataArrayResample.interpolate.html

Source: https://github.com/pydata/xarray/blob/main/xarray/core/resample.py#L143-L171

We can see that the function signature only allows for a single kind keyword, hence the TypeError: Resample.interpolate() got an unexpected keyword argument 'order' is the direct consequence.

It uses interp1d. :warning: This function is declared as legacy, so maybe xarray should move away from its use?

xr.show_versions()

import warnings

warnings.filterwarnings("ignore")
xr.show_versions()

etienneschalk avatar Feb 18 '24 14:02 etienneschalk

@etienneschalk Thank you for adding much more detail 🙇

stackjohn avatar Feb 18 '24 19:02 stackjohn