votes up 6

Input array must be 1 dimensional

Package:
pandas
github stars 30911
Exception Class:
ValueError

Raise code

""" 
    """
    # Check that the passed array is a Pandas or Numpy object
    # We don't want to strip away a Pandas data-type here (e.g. datetimetz)
    ndim = getattr(x, "ndim", None)
    if ndim is None:
        x = np.asarray(x)
    if x.ndim != 1:
        raise ValueError("Input array must be 1 dimensional")

    return x


def _postprocess_for_cut(fac, bins, retbins: bool, dtype, original):
    """
    handles post processing for the cut method where """

Ways to fix

votes up 3 votes down

pandas.cut is used to segment and sort data values into bins. This function is also useful for going from a continuous variable to a categorical variable.

The input array that is given to this function should be a 1D in shape. If a 2D shaped array is given this error is raised.

Reproducing the error:

pipenv install pandas

import pandas as pd
df=pd.cut(np.array([[1, 7, 5, 4, 6, 3]]),
          4, 
          labels=["bad", "medium", "good","very good",],
          ordered=False)
print(df)

The error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-29-b490473da29a> in <module>()
      4           4,
      5           labels=["bad", "medium", "good","very good",],
----> 6           ordered=False)
      7 print(df)

/usr/local/lib/python3.7/dist-packages/pandas/core/reshape/tile.py in cut(x, bins, right, labels, retbins, precision, include_lowest, duplicates, ordered)
    222 
    223     original = x
--> 224     x = _preprocess_for_cut(x)
    225     x, dtype = _coerce_to_type(x)
    226 

/usr/local/lib/python3.7/dist-packages/pandas/core/reshape/tile.py in _preprocess_for_cut(x)
    578         x = np.asarray(x)
    579     if x.ndim != 1:
--> 580         raise ValueError("Input array must be 1 dimensional")
    581 
    582     return x

ValueError: Input array must be 1 dimensional

Fixed version of the code:

import pandas as pd

df=pd.cut(np.array([1, 7, 5, 4, 6, 3]),
          4, 
          labels=["bad", "medium", "good","very good",],
          ordered=False)
print(df)

Output:

['bad', 'very good', 'good', 'medium', 'very good', 'medium']
Categories (4, object): ['bad', 'medium', 'good', 'very good']

Jul 17, 2021 kellemnegasi answer
kellemnegasi 11.7k

Add a possible fix

Please authorize to post fix