Select Page

How to Solve Python AttributeError: ‘Series’ object has no attribute ‘split’

by | Programming, Python, Tips

In Python, a Pandas Series is a one-dimensional labelled array capable of holding data of any type. Pandas Series is the same as a column in an Excel spreadsheet. If you have string entries in a Series object that you want to split, you cannot use the string method split(). If you try to call the split() method on a Series object, you will raise the AttributeError: ‘Series’ object has no attribute ‘split.’

To solve this error, you need to use the Series method pandas.Series.str.split(). Pandas Series has its own set of methods under str, which are equivalent to the string methods.

This tutorial will go through the error in detail and how to solve it with code examples.


AttributeError: ‘Series’ object has no attribute ‘split’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘Series’ object has no attribute ‘split’” tells us that the Series object we are handling does not have the split attribute. The split() method belongs to the string data type and splits a string into a list of strings. Pandas Series has its equivalent split() method under str.split(). The syntax for str.split() is as follows:

Series.str.split(pat=None, n=- 1, expand=False, *, regex=None

Parameters

  • pat: Optional. The separator to use when splitting the string. Default is whitespace.
  • n: Optional. How many splits to perform. Default is -1, which is “all occurrences”. None, 0, and -1 all interepreted as return all splits.
  • expand: Optional. Exapand the split strings into separate columns. If True return DataFrame/MultiIndex expanding dimensionality. If False, return Series/Index, containing lists of strings.

Example

Let’s look at an example where we want to import and format data from a JSON file. The first column of the JSON contains the email addresses of senders of an email, and the second column contains the email addresses of the recipients of an email.

The JSON file looks like this:

{"SENDFROM":{"0":"[email protected]","1":"[email protected]"},"RECEIVED":{"0":"[email protected];[email protected];[email protected]","1":"[email protected];[email protected];[email protected]"}}

We will save the file under test.json.

Next, we will write a program that changes the separators of the recipient email addresses from semi-colons to commas and whitespace. We can use Pandas to load the JSON into the program with the read_json() function. Let’s look at this step:

import pandas as pd

df = pd.read_json('test.json')

print(df)
       SENDFROM                                    RECEIVED
0  [email protected]  [email protected];[email protected];[email protected]
1    [email protected]       [email protected];[email protected];[email protected]

Then we will call the split() function to split the entries in the RECEIVED column using semi-colons as the separator. Then we will call the join() function to convert the entries to strings.

print(type(df['RECEIVED']))

df['RECEIVED'] = df['RECEIVED'].split(";").join(',')

Let’s run the code to see the result:

<class 'pandas.core.series.Series'>

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-bb36c75ff7c9> in <module>
      1 print(type(df['RECEIVED']))
      2 
----> 3 df['RECEIVED'] = df['RECEIVED'].split(";").join(',')

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5581         ):
   5582             return self[name]
-> 5583         return object.__getattribute__(self, name)
   5584 
   5585     def __setattr__(self, name: str, value) -> None:

AttributeError: 'Series' object has no attribute 'split'

The error occurs because we called the split() method directly on the Test column, which is a Series, not a string and has its own split() method under str.

Solution

To solve this error, we can use the pandas.Series.str.split function to split strings in the Series by the semi-colon separator. We can then use the pandas.Series.str.join function to join the lists contained as elements in the Series with the comma delimiter. These two functions are distinct from the string methods split and join in that they operate on Series objects. Let’s look at the revised code:

import pandas as pd

df = pd.read_json('test.json')

df['RECEIVED'] = df['RECEIVED'].str.split(';').str.join(', ')

print(df.to_string())

Let’s run the code to get the result:

       SENDFROM                                      RECEIVED
0  [email protected]  [email protected], [email protected], [email protected]
1    [email protected]       [email protected], [email protected], [email protected]

If we set expand=True in the split() method, the split elements will expand out into separate columns. Let’s look at how to do that with our df[‘RECEIVED’] Series.

s = df['RECEIVED']

s= s.str.split(';', expand=True)

print(s)
              0              1                2
0  [email protected]  [email protected]  [email protected]
1   [email protected]  [email protected]      [email protected]

Summary

Congratulations on reading to the end of this tutorial! The AttributeError ‘Series’ object has no attribute ‘split’ occurs when you try to call the split() method on a Series object as if it were a string. To solve this error, you can use Pandas’ method pandas.Series.str.split() to split the string entries in a Series with the specified separator.

For further reading on AttributeErrors involving the list object, go to the articles:

for further reading on pandas Series, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!