Skip to content

Commit e4df678

Browse files
authored
BUG: to_coo raising Systemerror for EA (#51007)
1 parent 66de372 commit e4df678

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

doc/source/whatsnew/v2.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ Sparse
11601160
^^^^^^
11611161
- Bug in :meth:`Series.astype` when converting a ``SparseDtype`` with ``datetime64[ns]`` subtype to ``int64`` dtype raising, inconsistent with the non-sparse behavior (:issue:`49631`,:issue:`50087`)
11621162
- Bug in :meth:`Series.astype` when converting a from ``datetime64[ns]`` to ``Sparse[datetime64[ns]]`` incorrectly raising (:issue:`50082`)
1163-
-
1163+
- Bug in :meth:`Series.sparse.to_coo` raising ``SystemError`` when :class:`MultiIndex` contains a ``ExtensionArray`` (:issue:`50996`)
11641164

11651165
ExtensionArray
11661166
^^^^^^^^^^^^^^

pandas/core/arrays/sparse/scipy_sparse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def _levels_to_axis(
7373

7474
else:
7575
levels_values = lib.fast_zip(
76-
[ss.index.get_level_values(lvl).values for lvl in levels]
76+
[ss.index.get_level_values(lvl).to_numpy() for lvl in levels]
7777
)
7878
codes, ax_labels = factorize(levels_values, sort=sort_labels)
7979
ax_coords = codes[valid_ilocs]

pandas/tests/arrays/sparse/test_accessor.py

+19
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,25 @@ def test_to_coo_nonzero_fill_val_raises(self, fill_value):
183183
with pytest.raises(ValueError, match="fill value must be 0"):
184184
df.sparse.to_coo()
185185

186+
@td.skip_if_no_scipy
187+
def test_to_coo_midx_categorical(self):
188+
# GH#50996
189+
import scipy.sparse
190+
191+
midx = pd.MultiIndex.from_arrays(
192+
[
193+
pd.CategoricalIndex(list("ab"), name="x"),
194+
pd.CategoricalIndex([0, 1], name="y"),
195+
]
196+
)
197+
198+
ser = pd.Series(1, index=midx, dtype="Sparse[int]")
199+
result = ser.sparse.to_coo(row_levels=["x"], column_levels=["y"])[0]
200+
expected = scipy.sparse.coo_matrix(
201+
(np.array([1, 1]), (np.array([0, 1]), np.array([0, 1]))), shape=(2, 2)
202+
)
203+
assert (result != expected).nnz == 0
204+
186205
def test_to_dense(self):
187206
df = pd.DataFrame(
188207
{

0 commit comments

Comments
 (0)