Skip to content

Commit e5cc19c

Browse files
authored
Fix conversion of multiindexed pandas objects to sparse xarray objects (#4088)
* Fix conversion of multiindexed pandas objects to sparse xarray objects * lint * fix whats-new * fix test * minor whats-new
1 parent 864877c commit e5cc19c

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ Bug fixes
102102
~~~~~~~~~
103103
- Support dark mode in VS code (:issue:`4024`)
104104
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
105+
- Fix bug when converting multiindexed Pandas objects to sparse xarray objects. (:issue:`4019`)
106+
By `Deepak Cherian <https://github.com/dcherian>`_.
105107
- ``ValueError`` is raised when ``fill_value`` is not a scalar in :py:meth:`full_like`. (:issue:`3977`)
106108
By `Huite Bootsma <https://github.com/huite>`_.
107109
- Fix wrong order in converting a ``pd.Series`` with a MultiIndex to ``DataArray``. (:issue:`3951`)

xarray/core/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4537,7 +4537,7 @@ def _set_sparse_data_from_dataframe(
45374537
idx = dataframe.index
45384538
if isinstance(idx, pd.MultiIndex):
45394539
coords = np.stack([np.asarray(code) for code in idx.codes], axis=0)
4540-
is_sorted = idx.is_lexsorted
4540+
is_sorted = idx.is_lexsorted()
45414541
shape = tuple(lev.size for lev in idx.levels)
45424542
else:
45434543
coords = np.arange(idx.size).reshape(1, -1)

xarray/tests/test_dataarray.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3532,6 +3532,24 @@ def test_from_series_sparse(self):
35323532
actual_sparse.data = actual_sparse.data.todense()
35333533
assert_identical(actual_sparse, actual_dense)
35343534

3535+
@requires_sparse
3536+
def test_from_multiindex_series_sparse(self):
3537+
# regression test for GH4019
3538+
import sparse
3539+
3540+
idx = pd.MultiIndex.from_product([np.arange(3), np.arange(5)], names=["a", "b"])
3541+
series = pd.Series(np.random.RandomState(0).random(len(idx)), index=idx).sample(
3542+
n=5, random_state=3
3543+
)
3544+
3545+
dense = DataArray.from_series(series, sparse=False)
3546+
expected_coords = sparse.COO.from_numpy(dense.data, np.nan).coords
3547+
3548+
actual_sparse = xr.DataArray.from_series(series, sparse=True)
3549+
actual_coords = actual_sparse.data.coords
3550+
3551+
np.testing.assert_equal(actual_coords, expected_coords)
3552+
35353553
def test_to_and_from_empty_series(self):
35363554
# GH697
35373555
expected = pd.Series([], dtype=np.float64)

0 commit comments

Comments
 (0)