In Pandas, a series is a one-dimensional annotated array that can store information of any type.
Like a column in an Excel spreadsheet, Pandas Series can be considered a time series.
You can create a Pandas Series object by passing a list, numpy array, or scalar value to the pandas.Series() constructor. The resulting Series will have an index, a label for each element in the Series.
What is AttributeError in Python
AttributeError occurs when we try to access an attribute (method or property) that does not exist for a specific object.
The object can be a list, tuple, string, set, DataFrame, or series.
In this example, we will talk about the Series object and related to its AttributeError and how to fix it.
AttributeError: ‘series’ object has no attribute ‘split’
The AttributeError: ‘series’ object has no attribute ‘split’ error occurs when the object referenced is a Pandas Series object, which does not have a method called “split.”
When the developer is attempting to use the string “split()” method on a Pandas Series object which does not have the split() method.
The split() method belongs to the string type and splits a string into a list of strings.
Check out the below code that raises this type of error.
import pandas as pd
# Create an example dataframe
df = pd.DataFrame({'company': ['Apple', 'Microsoft', 'Alphabet', 'Amazon']})
# Attempt to use the string method split on a Series object
df['split_name'] = df['company'].split(' ')
print(df)
Output
AttributeError: 'Series' object has no attribute 'split'
If you run the above code, you will the above error.
The error occurs because we apply a split() method on the series object, and the split() method does not exist in the series object.
How to Fix AttributeError: ‘series’ object has no attribute ‘split’
The easiest way to fix AttributeError: ‘series’ object has no attribute ‘split’ error is to use the str.split() method, which is available for Pandas Series objects.
import pandas as pd
# Create an example dataframe
df = pd.DataFrame({'company': ['Apple', 'Microsoft', 'Alphabet', 'Amazon']})
# Attempt to use the str.split() method split on a Series object
df['split_name'] = df['company'].str.split(' ')
print(df)
Output
company split_name
0 Apple [Apple]
1 Microsoft [Microsoft]
2 Alphabet [Alphabet]
3 Amazon [Amazon]
And we solved the AttributeError.
Alternative solutions
Another effective solution is to convert the Series object to a string using the astype() function and then use the string method split().
import pandas as pd
df = pd.DataFrame({'company': ['Apple', 'Microsoft', 'Alphabet', 'Amazon']})
df['split_name'] = df['company'].astype(str).str.split(' ')
print(df)
Output
company split_name
0 Apple [Apple]
1 Microsoft [Microsoft]
2 Alphabet [Alphabet]
3 Amazon [Amazon]
The code first creates a DataFrame called df with a single column called ‘company’.
Then, it creates a new column called ‘split_name’ by using the str.split() method on the ‘company’ column.
The str.split() method is used to split the string by a specified delimiter, in this case, by space.
The resulting Dataframe will have the ‘split_name’ column containing the split strings of the ‘company’ column.
The above code correctly uses the str.split() method available for pandas Series objects.
The astype(str) function converts the ‘company’ column to a string before splitting, as the ‘split()’ method is unavailable to non-string data types.
If you are still facing an error, then check out the following things.
- Make sure the column you are trying to split is in string format and not in integer or float format.
- Ensure that the column you are trying to access exists in the Dataframe and that you are accessing it correctly.
Closing thoughts
Using the str.split() function, you can solve AttributeError: ‘series’ object has no attribute ‘split’ error.
The split() function is the property of a string and not a series object.
The str.split() function is the property of a series object, so you can use that.
That’s pretty much it for this tutorial.
Further reading
AttributeError: module ‘collections’ has no attribute ‘mutablemapping’
AttributeError: can only use .str accessor with string values
AttributeError: str object has no attribute decode in Python