Index(...) must be called with a collection of some kind

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
3 min

banner

# Table of Contents

  1. Index(...) must be called with a collection of some kind
  2. Creating a Series object incorrectly causes the error
  3. Trying to remove the index from a DataFrame

# Index(...) must be called with a collection of some kind

The 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.

main.py
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)

type error index must be called with collection of some kind

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.

main.py
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)

set columns argument to list

The code for this article is available on GitHub

Make sure to specify exactly as many columns in the list as there are in your DataFrame.

In other words, you have to replace columns='A' with columns=['A'] when calling pd.DataFrame.

# Creating a Series object incorrectly causes the error

You might also get the error when creating a Series object incorrectly.

main.py
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.

main.py
import pandas as pd series = pd.Series(['Alice', 'Bobby', 'Carl']) # 0 Alice # 1 Bobby # 2 Carl # dtype: object print(series)

creating series object correctly

The code for this article is available on GitHub

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.

# Trying to remove the index from a DataFrame

If 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.

main.py
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)
The code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.