From bdae2c5b6e6fd6e26eeac54405837e8da9710c51 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Fri, 18 Dec 2020 14:49:06 -0500 Subject: [PATCH 1/3] BUG: fix .sparse.to_coo() with numeric col index, no 0 --- pandas/core/arrays/sparse/accessor.py | 2 +- pandas/tests/arrays/sparse/test_accessor.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/core/arrays/sparse/accessor.py b/pandas/core/arrays/sparse/accessor.py index ec4b0fd89860c..12f29faab9574 100644 --- a/pandas/core/arrays/sparse/accessor.py +++ b/pandas/core/arrays/sparse/accessor.py @@ -329,7 +329,7 @@ def to_coo(self): import_optional_dependency("scipy") from scipy.sparse import coo_matrix - dtype = find_common_type(self._parent.dtypes) + dtype = find_common_type(self._parent.dtypes.to_list()) if isinstance(dtype, SparseDtype): dtype = dtype.subtype diff --git a/pandas/tests/arrays/sparse/test_accessor.py b/pandas/tests/arrays/sparse/test_accessor.py index 2a81b94ce779c..ae6afc256e3b5 100644 --- a/pandas/tests/arrays/sparse/test_accessor.py +++ b/pandas/tests/arrays/sparse/test_accessor.py @@ -69,10 +69,13 @@ def test_from_spmatrix_columns(self, columns): tm.assert_frame_equal(result, expected) @td.skip_if_no_scipy - def test_to_coo(self): + @pytest.mark.parametrize("colnames", [("A", "B"), (1, 2), (1, pd.NA), (0.1, 0.2)]) + def test_to_coo(self, colnames): import scipy.sparse - df = pd.DataFrame({"A": [0, 1, 0], "B": [1, 0, 0]}, dtype="Sparse[int64, 0]") + df = pd.DataFrame( + {colnames[0]: [0, 1, 0], colnames[1]: [1, 0, 0]}, dtype="Sparse[int64, 0]" + ) result = df.sparse.to_coo() expected = scipy.sparse.coo_matrix(np.asarray(df)) assert (result != expected).nnz == 0 From fb00abc5df24de89f656399514c1a112d4585011 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Fri, 18 Dec 2020 14:52:09 -0500 Subject: [PATCH 2/3] Keep consistent decorator ordering --- pandas/tests/arrays/sparse/test_accessor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/arrays/sparse/test_accessor.py b/pandas/tests/arrays/sparse/test_accessor.py index ae6afc256e3b5..4c6781509f8af 100644 --- a/pandas/tests/arrays/sparse/test_accessor.py +++ b/pandas/tests/arrays/sparse/test_accessor.py @@ -68,8 +68,8 @@ def test_from_spmatrix_columns(self, columns): expected = pd.DataFrame(mat.toarray(), columns=columns).astype(dtype) tm.assert_frame_equal(result, expected) - @td.skip_if_no_scipy @pytest.mark.parametrize("colnames", [("A", "B"), (1, 2), (1, pd.NA), (0.1, 0.2)]) + @td.skip_if_no_scipy def test_to_coo(self, colnames): import scipy.sparse From 942b268ffe7a523871d9f5441adbf23dbf30f61b Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Fri, 18 Dec 2020 15:06:20 -0500 Subject: [PATCH 3/3] Add whatsnew --- doc/source/whatsnew/v1.3.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 3545dd8a89159..72317b1b7da63 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -270,6 +270,7 @@ Reshaping Sparse ^^^^^^ +- Bug in :meth:`DataFrame.sparse.to_coo` raising ``KeyError`` with columns that are a numeric :class:`Index` without a 0 (:issue:`18414`) - -