-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 rolling does not match pandas when using min_periods and reduce #3066
Comments
@mrezak Thanks for the report and the clear example! Certainly this is an annoying inconsistency. I'm trying to figure out whether this is also a bug or not. I think the difference comes down to how pandas and xarray pass data into the import numpy as np
import pandas as pd
import xarray
def custom(x, axis=0):
print(x)
return np.mean(x, axis)
print('pandas example')
d = pd.DataFrame(np.random.rand(11,3))
r = d.rolling(10, min_periods=5).apply(custom)
print(r.iloc[0:10,:])
print('\nxarray example')
xd = d.to_xarray().to_array()
r = xd.rolling(index=10, min_periods=5).reduce(custom)
print(r[:,0:10]) Output:
Xarray's version is certainly going to be way faster, but it has the downside of treating windows differently. One way to work around this would be to use cc @jhamman @fujiisoup who worked on this and may have ideas |
@shoyer thanks for looking into this. I also figured it later that I can just use np.nanmean (or nanmedian) but that function turns out to be much slower than np.mean (or np.median) version. As nans are only happening as the beginning and end of the sequence, is there any efficient way of using nanmean only for those segments and mean for the rest of the processing? My own thought is to have a check for nan in the custom function and apply mean or nanmean depending on the results of that check, but not sure if this can be done more efficiently. |
MCVE Code Sample
MCVE
Problem Description
I am applying a custom function on rolling windows with specific
min_periods
. The output ofpandas..rolling.apply
matches what I expect; however, the output ofxarray..rolling.reduce
doesn't seem to takemin_periods
into account.Expected Output and Actual Output
Output of
xr.show_versions()
xarray: 0.12.1
pandas: 0.24.2
numpy: 1.16.4
scipy: 1.2.1
netCDF4: 1.4.2
pydap: None
h5netcdf: None
h5py: 2.9.0
Nio: None
zarr: None
cftime: 1.0.3.4
nc_time_axis: None
PseudonetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.2.1
dask: 2.0.0
distributed: 2.0.1
matplotlib: 3.1.0
cartopy: None
seaborn: 0.9.0
setuptools: 41.0.1
pip: 19.1.1
conda: None
pytest: None
IPython: 7.5.0
sphinx: None
The text was updated successfully, but these errors were encountered: