pydata / pydata/xarray

concat() fails when args have sparse.COO data and different fill values

Open
#3,381 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-arrays topic-combine
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

MCVE Code Sample
import numpy as np
import pandas as pd
import sparse
import xarray as xr

# Indices and raw data
foo = [f'foo{i}' for i in range(6)]
bar = [f'bar{i}' for i in range(6)]
raw = np.random.rand(len(foo) // 2, len(bar))

# DataArray
a = xr.DataArray(
    data=sparse.COO.from_numpy(raw),
    coords=[foo[:3], bar],
    dims=['foo', 'bar'])

print(a.data.fill_value)  # 0.0


# Created from a pd.Series
b_series = pd.DataFrame(raw, index=foo[3:], columns=bar) \
             .stack() \
             .rename_axis(index=['foo', 'bar'])
b = xr.DataArray.from_series(b_series, sparse=True)

print(b.data.fill_value)  # nan


# Works despite inconsistent fill-values
a + b
a * b


# Fails: complains about inconsistent fill-values
# xr.concat([a, b], dim='foo')  # ***

# The fill_value argument doesn't help
# xr.concat([a, b], dim='foo', fill_value=np.nan)


def fill_value(da):
    """Try to coerce one argument to a consistent fill-value."""
    return xr.DataArray(
        data=sparse.as_coo(da.data, fill_value=np.nan),
        coords=da.coords,
        dims=da.dims,
        name=da.name,
        attrs=da.attrs,
        )


# Fails: "Cannot provide a fill-value in combination with something that
# already has a fill-value"
# print(xr.concat([a.pipe(fill_value), b], dim='foo'))


# If we cheat by recreating 'a' from scratch, copying the fill value of the
# intended other argument, it works again:
a = xr.DataArray(
    data=sparse.COO.from_numpy(raw, fill_value=b.data.fill_value),
    coords=[foo[:3], bar],
    dims=['foo', 'bar'])
c = xr.concat([a, b], dim='foo')

print(c.data.fill_value)  # nan

# But simple operations again create objects with potentially incompatible
# fill-values
d = c.sum(dim='bar')
print(d.data.fill_value)  # 0.0
Expected

concat() can be used without having to create new objects; i.e. the line marked *** just works.

Problem Description

Some basic xarray manipulations don't work on sparse.COO-backed objects.

xarray should automatically coerce objects into a compatible state, or at least provide users with methods to do so. Behaviour should also be documented, e.g. in this instance, which operations (here, .sum()) modify the underlying storage format in ways that necessitate some kind of (re-)conversion.

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.3 (default, Aug 20 2019, 17:04:43)
[GCC 8.3.0]
python-bits: 64
OS: Linux
OS-release: 5.0.0-32-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_CA.UTF-8
LOCALE: en_CA.UTF-8
libhdf5: 1.10.4
libnetcdf: 4.6.2

xarray: 0.13.0
pandas: 0.25.0
numpy: 1.17.2
scipy: 1.2.1
netCDF4: 1.4.2
pydap: None
h5netcdf: 0.7.1
h5py: 2.8.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.1.0
distributed: None
matplotlib: 3.1.1
cartopy: 0.17.0
seaborn: 0.9.0
numbagg: None
setuptools: 40.8.0
pip: 19.2.3
conda: None
pytest: 5.0.1
IPython: 5.8.0
sphinx: 2.2.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the MCVE at the xr.concat entry point and inspect how sparse.COO fill_value differences are handled. Compare concat with the shown arithmetic operations and the fill_value argument; done means the marked concat call works for these inputs and the resulting fill-value behavior is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.