You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In my codes I used the implicit cast from bool to int that xarray/numpy perform for certain operations. This is the case for sum. A resampling sum on a boolean array actually returns the number of True values and not the OR of all values.
However, when flox is activated, it does return the OR of all values. Digging a bit, I see that the flox aggregation uses np.add and not np.sum. So, this may in fact be an issue for flox? It felt the xarray devs should know about this potential regression anyway.
What did you expect to happen?
I expected a sum of boolean to actually be the count of True values.
Minimal Complete Verifiable Example
importxarrayasxrds=xr.tutorial.open_dataset("air_temperature")
# Count the monthly number of 6-hour periods with tas over 300Kwithxr.set_options(use_flox=False):
# this works as expectedoutOLD= (ds.air>300).resample(time='MS').sum()
withxr.set_options(use_flox=True):
# this doesn't fail, but return True or False :# the OR and not the expected sum.outFLOX= (ds.air>300).resample(time='MS').sum()
MVCE confirmation
Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
Complete example — the example is self-contained, including all data and the text of any traceback.
Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
New issue — a search of GitHub Issues suggests this is not a duplicate.
Relevant log output
No response
Anything else we need to know?
I wrote a quick test for basic operations and sum seems the only really problematic one. prod does return a different dtype, but the values are not impacted.
for op in ['any', 'all', 'count', 'sum', 'prod', 'mean', 'var', 'std', 'max', 'min']:
with xr.set_options(use_flox=False):
outO = getattr((ds.air > 300).resample(time='YS'), op)()
with xr.set_options(use_flox=True):
outF = getattr((ds.air > 300).resample(time='YS'), op)()
print(op, outO.dtype, outF.dtype, outO.equals(outF)))
returns
any bool bool True
all bool bool True
count int64 int64 True
sum int64 bool False
prod int64 bool True
mean float64 float64 True
var float64 float64 True
std float64 float64 True
max bool bool True
min bool bool True
What happened?
In my codes I used the implicit cast from bool to int that xarray/numpy perform for certain operations. This is the case for
sum
. A resampling sum on a boolean array actually returns the number of True values and not the OR of all values.However, when flox is activated, it does return the OR of all values. Digging a bit, I see that the flox aggregation uses
np.add
and notnp.sum
. So, this may in fact be an issue for flox? It felt the xarray devs should know about this potential regression anyway.What did you expect to happen?
I expected a sum of boolean to actually be the count of True values.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
No response
Anything else we need to know?
I wrote a quick test for basic operations and
sum
seems the only really problematic one.prod
does return a different dtype, but the values are not impacted.returns
Environment
INSTALLED VERSIONS
commit: None
python: 3.9.6 | packaged by conda-forge | (default, Jul 11 2021, 03:39:48)
[GCC 9.3.0]
python-bits: 64
OS: Linux
OS-release: 5.17.5-arch1-2
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: fr_CA.utf8
LOCALE: ('fr_CA', 'UTF-8')
libhdf5: 1.12.0
libnetcdf: 4.7.4
xarray: 2022.3.1.dev16+g3ead17ea
pandas: 1.4.2
numpy: 1.21.6
scipy: 1.7.1
netCDF4: 1.5.7
pydap: None
h5netcdf: 0.11.0
h5py: 3.4.0
Nio: None
zarr: 2.10.0
cftime: 1.5.0
nc_time_axis: 1.3.1
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: 2022.04.1
distributed: 2022.4.1
matplotlib: 3.4.3
cartopy: None
seaborn: None
numbagg: None
fsspec: 2021.07.0
cupy: None
pint: 0.18
sparse: None
flox: 0.5.1
numpy_groupies: 0.9.16
setuptools: 57.4.0
pip: 21.2.4
conda: None
pytest: 6.2.5
IPython: 8.2.0
sphinx: 4.1.2
The text was updated successfully, but these errors were encountered: