Pandas cannot merge a series without a name

In this short guide, I'll show you how to solve Pandas error:

ValueError: Cannot merge a Series without a name

The error appears when we try to merge two Pandas series without a name. To solve the error we can set name to the series either by:

(1) Rename series for the merge

pd.merge(s1.rename('old'), s2.rename('new'), left_index=True, right_index=True)

(2) Set name for Series

s1.name = 'Series 1'

ValueError: Cannot merge a Series without a name

To reproduce the error we will create two Pandas Series and try to merge them:

import pandas as pd

s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])

result = pd.merge(s1, s2, left_index=True, right_index=True)

This results into Pandas error:

ValueError: Cannot merge a Series without a name

Fix - ValueError: Cannot merge a Series without a name

To fix this error we can apply two solutions:

Fix set new name

Set permanent name for the Pandas series:

import pandas as pd

s1 = pd.Series([1, 2, 3])
s1.name = 'old'

s2 = pd.Series([4, 5, 6])
s2.name = 'new'

result = pd.merge(s1, s2, left_index=True, right_index=True)
result

This will change the Series:

0	1
1	2
2	3
Name: old, dtype: int64

The result is new DataFrame:

old new
0 1 4
1 2 5
2 3 6

Temporary rename series

We can solve the error without changing the series by using method rename():

import pandas as pd

s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])

result = pd.merge(s1.rename('old'), s2.rename('new'), left_index=True, right_index=True)
result

At the end the series is the same:

0	1
1	2
2	3
dtype: int64