Cannot concatenate object of type 'X'; only Series and DataFrame objs are valid

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Cannot concatenate object of type 'X'; only Series and DataFrame objs are valid

The Pandas TypeError "Cannot concatenate object of type 'X'; only Series and DataFrame objs are valid" occurs when you pass an invalid first argument to the pandas.concat() method.

To solve the error, make sure to pass a list of DataFrame or Series to the pandas.concat() method.

Here is an example of how the error occurs.

main.py
import pandas as pd df1 = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) df2 = pd.DataFrame({ 'A': [5, 6], 'B': [7, 8] }) dataframes = [df1, df2] # ⛔️ TypeError: cannot concatenate object of type '<class 'list'>'; only Series and DataFrame objs are valid df3 = pd.concat([dataframes], ignore_index=True) print(df3)

type error cannot conactenate object of type only series and dataframe

We have a dataframes variable that stores 2 DataFrame objects.

However, notice that we passed a list containing the variable to the pandas.concat method.

We are basically passing a list containing a list of DataFrames to the concat method.

To solve the issue in the example, pass a one-dimensional list of DataFrames to pandas.concat().

main.py
import pandas as pd df1 = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) df2 = pd.DataFrame({ 'A': [5, 6], 'B': [7, 8] }) dataframes = [df1, df2] df3 = pd.concat(dataframes, ignore_index=True) # A B # 0 1 3 # 1 2 4 # 2 5 7 # 3 6 8 print(df3)

call pandas concat correctly

The code for this article is available on GitHub

We could've also solved the error by passing an inline list containing the DataFrames to pandas.concat().

main.py
import pandas as pd df1 = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) df2 = pd.DataFrame({ 'A': [5, 6], 'B': [7, 8] }) df3 = pd.concat([df1, df2], ignore_index=True) # A B # 0 1 3 # 1 2 4 # 2 5 7 # 3 6 8 print(df3)

The pandas.concat() method concatenates pandas objects along a particular axis.

The first argument the method takes is a sequence or a mapping of DataFrame objects or Series.

# Solving the error when trying to concatenate dictionaries

You will also get the error if you pass a list containing dictionaries to the pandas.concat() method.

main.py
import pandas as pd dict1 = { 'A': [1, 2], 'B': [3, 4] } dict2 = { 'A': [5, 6], 'B': [7, 8] } # ⛔️ TypeError: cannot concatenate object of type '<class 'dict'>'; only Series and DataFrame objs are valid df3 = pd.concat([dict1, dict2], ignore_index=True)

To solve the error in this case, convert the dictionaries to DataFrame objects by using the pd.DataFrame() constructor.

main.py
import pandas as pd dict1 = { 'A': [1, 2], 'B': [3, 4] } dict2 = { 'A': [5, 6], 'B': [7, 8] } df3 = pd.concat( [pd.DataFrame(dict1), pd.DataFrame(dict2)], ignore_index=True ) # A B # 0 1 3 # 1 2 4 # 2 5 7 # 3 6 8 print(df3)
The code for this article is available on GitHub

We could've also converted the dictionaries to DataFrame objects directly.

main.py
import pandas as pd df1 = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) df2 = pd.DataFrame({ 'A': [5, 6], 'B': [7, 8] }) df3 = pd.concat( [df1, df2], ignore_index=True ) # A B # 0 1 3 # 1 2 4 # 2 5 7 # 3 6 8 print(df3)

# Make sure you aren't calling pandas.concat with a list of strings

You should also ensure that you aren't calling the pandas.concat() method with a list containing strings.

main.py
import pandas as pd df1 = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) df2 = pd.DataFrame({ 'A': [5, 6], 'B': [7, 8] }) # ⛔️ TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid df3 = pd.concat( ["df1", "df2"], ignore_index=True )

The issue in the code sample is that the variable names "df1" and "df2" are enclosed in quotes in the list we passed to pandas.concat.

Remove the quotation marks to solve the error.

main.py
import pandas as pd df1 = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) df2 = pd.DataFrame({ 'A': [5, 6], 'B': [7, 8] }) df3 = pd.concat( [df1, df2], ignore_index=True ) # A B # 0 1 3 # 1 2 4 # 2 5 7 # 3 6 8 print(df3)
The code for this article is available on GitHub

# Calling pandas.concat() with Series objects

You can also call the pandas.concat() method with Series objects.

main.py
import pandas as pd s1 = pd.Series(['a', 'b']) s2 = pd.Series(['c', 'd']) s3 = pd.concat( [s1, s2], ignore_index=True ) # 0 a # 1 b # 2 c # 3 d # dtype: object print(s3)

If you want to construct a hierarchical index, pass the keys argument.

main.py
import pandas as pd s1 = pd.Series(['a', 'b']) s2 = pd.Series(['c', 'd']) df3 = pd.concat( [s1, s2], keys=['s1', 's2'] ) # s1 0 a # 1 b # s2 0 c # 1 d # dtype: object print(df3)
The code for this article is available on GitHub

When concatenating all Series along the index (axis=0), a Series object is returned.

If the supplied list (the first argument of concat) contains at least one DataFrame, a DataFrame is returned.

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