[Scipy-tickets] [SciPy] #1533: toarray on sparse matrices can't handle dtype=bool

SciPy Trac scipy-tickets@scipy....
Tue Apr 16 21:15:18 CDT 2013

#1533: toarray on sparse matrices can't handle dtype=bool
 Reporter:  larsmans             |       Owner:  wnbell     
     Type:  defect               |      Status:  new        
 Priority:  high                 |   Milestone:  Unscheduled
Component:  scipy.sparse         |     Version:  0.9.0      
 Keywords:  bool, dtype, sparse  |  
Changes (by cowlicks):

  * keywords:  => bool, dtype, sparse


 If you instantiate a {{{csr_matrix}}} with a {{{ndarray}}} what basically
 happens is:

 class csr_matrix(_cs_matrix):

 class _cs_matrix(...):
     def __init__(self, arg1, ...):
         self.format = 'csr'
         if isspmatrix(arg1):
             if arg1.format == self.format and copy:
                 arg1 = arg1.asformat(self.format)
             self._set_self( arg1 )
         self._set_self( self.__class__(coo_matrix(arg1, dtype=dtype)) )

 So the ndarray is passed through {{{_cs_matrix}}} where it becomes a
 {{{coo_matrix}}}, and is then passed through {{{_cs_matrix again}}} again.
 Then since {{{coo_matrix.format == self.format}}} is False, the
 {{{coo_matrix}}} is cast as {{{csr_matrix}}} by
 {{{arg1.asformat(self.format)}}}. Where {{{asformat}}} uses {{{to_csr}}}
 which is where this happens:

 data    = np.empty(self.nnz, dtype=upcast(self.dtype))
 return csr_matrix((data, ...))
 Where the {{{upcast}}} changes the {{{dtype}}} from {{{bool}}} to

 This is odd behavior, why is the dtype being changed? Is there a reason a
 {{{csr_matrix}}} should not have a {{{bool}}} dtype? Or is this just a
 good example of why we need to specify boolean data type handling?

Ticket URL: <http://projects.scipy.org/scipy/ticket/1533#comment:2>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.

More information about the Scipy-tickets mailing list