votes up 1

'data' must have a single column, not '(ncol)'

Package:
pandas
github stars 30911
Exception Class:
ValueError

Raise code

""" 
        Fill: 0.0
        IntIndex
        Indices: array([], dtype=int32)
        """
        length, ncol = data.shape

        if ncol != 1:
            raise ValueError(f"'data' must have a single column, not '{ncol}'")

        # our sparse index classes require that the positions be strictly
        # increasing. So we need to sort loc, and arr accordingly.
        data = data.tocsc()
        data.sort_indices()
        arr = data.data
        idx = data.indices
🙏 Scream for help to Ukraine
Today, 8th August 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 1 votes down

This happens when the function pandas,arrays.SparseArray.from_spmatrix() is given a sparse matrix with columns greater than one.

How to reproduce it:

import scipy.sparse
import pandas as pd

# Notice here that `mat` has 2 columns and 4 rows.
mat = scipy.sparse.coo_matrix((4, 2))
result = pd.arrays.SparseArray.from_spmatrix(mat)
print(result)

Output error:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-bbeedda2bc3e> in <module>()  3   4 mat = scipy.sparse.coo_matrix((4, 2)) ----> 5 result = pd.arrays.SparseArray.from_spmatrix(mat)  6 print(result) 
/usr/local/lib/python3.7/dist-packages/pandas/core/arrays/sparse/array.py in from_spmatrix(cls, data)  486   487 if ncol != 1: --> 488 raise ValueError(f"'data' must have a single column, not '{ncol}'")  489   490 # our sparse index classes require that the positions be strictly 
ValueError: 'data' must have a single column, not '2'

Fixed version of the code:

import scipy.sparse
import pandas as pd


mat = scipy.sparse.coo_matrix((4, 1))
result = pd.arrays.SparseArray.from_spmatrix(mat)
print(result)

Output:

[0.0, 0.0, 0.0, 0.0] 
Fill: 0.0 IntIndex 
Indices: array([], dtype=int32)

May 05, 2022 kellemnegasi answer
kellemnegasi 30.6k

Add a possible fix

Please authorize to post fix