Reputation: 189
Here are the dtypes of the dataframe. I want to convert it into a sparse dataframe, but for some reason it returns an error. What is wrong with my code?
>X[nums].dtypes
bin_1 float64
bin_2 float64
bin_3 float64
bin_4 float64
ord_0 float64
ord_1 float64
ord_2 float64
ord_3 float64
ord_4 float64
day_sin float64
day_cos float64
month_sin float64
month_cos float64
ord_5 float64
dtype: object
And here is the error when I try to convert it into sparse matrix. I tried it with pandas sparse too and it returned the same result. Why does it say TypeError: no supported conversion for types: (dtype('O'),)
>scipy.sparse.csr_matrix(X[nums].values)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/scipy/sparse/base.py in asformat(self, format, copy)
326 try:
--> 327 return convert_method(copy=copy)
328 except TypeError:
/opt/conda/lib/python3.6/site-packages/scipy/sparse/coo.py in tocsr(self, copy)
399 indices = np.empty_like(col, dtype=idx_dtype)
--> 400 data = np.empty_like(self.data, dtype=upcast(self.dtype))
401
/opt/conda/lib/python3.6/site-packages/scipy/sparse/sputils.py in upcast(*args)
51
---> 52 raise TypeError('no supported conversion for types: %r' % (args,))
53
TypeError: no supported conversion for types: (dtype('O'),)
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-102-20bd81b47238> in <module>
----> 1 scipy.sparse.csr_matrix(X[notToDummy].values)
/opt/conda/lib/python3.6/site-packages/scipy/sparse/compressed.py in __init__(self, arg1, shape, dtype, copy)
81 "".format(self.format))
82 from .coo import coo_matrix
---> 83 self._set_self(self.__class__(coo_matrix(arg1, dtype=dtype)))
84
85 # Read matrix dimensions given, if any
/opt/conda/lib/python3.6/site-packages/scipy/sparse/compressed.py in __init__(self, arg1, shape, dtype, copy)
30 arg1 = arg1.copy()
31 else:
---> 32 arg1 = arg1.asformat(self.format)
33 self._set_self(arg1)
34
/opt/conda/lib/python3.6/site-packages/scipy/sparse/base.py in asformat(self, format, copy)
327 return convert_method(copy=copy)
328 except TypeError:
--> 329 return convert_method()
330
331 ###################################################################
/opt/conda/lib/python3.6/site-packages/scipy/sparse/coo.py in tocsr(self, copy)
398 indptr = np.empty(M + 1, dtype=idx_dtype)
399 indices = np.empty_like(col, dtype=idx_dtype)
--> 400 data = np.empty_like(self.data, dtype=upcast(self.dtype))
401
402 coo_tocsr(M, N, self.nnz, row, col, self.data,
/opt/conda/lib/python3.6/site-packages/scipy/sparse/sputils.py in upcast(*args)
50 return t
51
---> 52 raise TypeError('no supported conversion for types: %r' % (args,))
53
54
TypeError: no supported conversion for types: (dtype('O'),)
Upvotes: 1
Views: 612
Reputation: 31780
As specified by @bglbrt in this comment, use:
scipy.sparse.csr_matrix(X[nums].values.astype(float))
This answer was posted as an edit to the question TypeError when converting to sparse matrix by the OP Elizabeth McBeth under CC BY-SA 4.0.
Upvotes: 0