Last updated: Apr 11, 2024
Reading time·3 min

Series object incorrectly causes the errorDataFrameThe Pandas "TypeError: Index(...) must be called with a collection of some
kind, 'X' was passed" most commonly occurs when you set the columns argument
to a primitive value (an integer or a string) when calling pd.DataFrame().
To solve the error, set the columns argument to a list instead.
Here is an example of how the error occurs.
import pandas as pd import numpy as np df = pd.DataFrame( np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns='A' ) # ⛔️ TypeError: Index(...) must be called with a collection of some kind, 'A' was passed print(df)

The columns argument
is used to specify the column labels to use for the resulting
DataFrame
when the supplied data doesn't have them.
The argument defaults to a RangeIndex(0, 1, 2, ..., n).
If the supplied data contains column labels, column selection is performed
instead.
To solve the error, set the columns argument to a list.
import pandas as pd import numpy as np df = pd.DataFrame( np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=['A', 'B', 'C'] ) # A B C # 0 1 2 3 # 1 4 5 6 # 2 7 8 9 print(df)

Make sure to specify exactly as many columns in the list as there are in your
DataFrame.
columns='A' with columns=['A'] when calling pd.DataFrame.Series object incorrectly causes the errorYou might also get the error when creating a Series object incorrectly.
import pandas as pd series = pd.Series('Alice', 'Bobby', 'Carl') # ⛔️ TypeError: Index(...) must be called with a collection of some kind, 'Bobby' was passed print(series)
The
pandas.Series()
method creates a one-dimensional ndarray with axis labels.
The first argument the method takes is an array-like object, an iterable, a
dict or scalar values.
The second argument the method takes is the index.
To solve the error, make sure to pass the data argument as a list (or any
other iterable) containing your initial values.
import pandas as pd series = pd.Series(['Alice', 'Bobby', 'Carl']) # 0 Alice # 1 Bobby # 2 Carl # dtype: object print(series)

Notice that we passed a list to the pandas.Series() constructor, and not
multiple, comma-separated values.
The error simply means that a collection argument is expected instead of a
primitive value, so instead of passing "A", try to pass ["A"] to the
method.
DataFrameIf you got the error when trying to remove the index from a DataFrame when
parsing an input string, use the pandas.read_csv() method and the StringIO
class.
from io import StringIO import pandas as pd string = """A,B,C 0,10,44 1,15,100 2,20,150 3,35,180 """ df = pd.read_csv( StringIO(string), sep=',', index_col=0 ) # B C # A # 0 10 44 # 1 15 100 # 2 20 150 # 3 35 180 print(df)
We used the StringIO() class to convert the string to a StringIO object that
can be passed to the
pandas.read_csv()
method.
The index_col parameter determines the column(s) that should be used as the
row labels of the DataFrame.
You can learn more about the related topics by checking out the following tutorials: