Select Page

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

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 a Series containing string values that you want to make all lowercase, you cannot call lower() on the Series object. If you try to call lower() directly on a Series object, you will raise the AttributeError: ‘Series’ object has no attribute ‘strftime’.

You can format the datetime objects with the Pandas dt accessor. For example:

df['Strings'] = df['Strings'].str.lower()

You can also call the apply() method on the Series object with a lambda function, for example

df['String'] = df['Strings'].apply(lambda x: x.lower()

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


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

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 ‘lower’” tells us that the Series object we are handling does not have the strftime attribute. lower() is a string method and returns a string where all characters are lower case. Pandas has an accessor object called str, which contains vectorized string functions for Series and Index patterned after Python’s built-in string methods. The Pandas lower() method is under str.lower().

Example

Let’s look at an example where we will use the lower() method to convert all the string values in a Series object to lower case. First, we will create the Series object using the Pandas library.

import pandas as pd

series = pd.Series(['LeARNINg', 'PYTHON', 'iS', 'ReALly', 'FUN!'])

print(series)
0    LeARNINg
1      PYTHON
2          iS
3      ReALly
4        FUN!
dtype: object

Let’s try to call the lower() method directly on the Series object:

series_lower = series.lower()

print(series_lower)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-667647a019d5> in <module>
----> 1 series_lower = series.lower()
      2 print(series_lower)

~/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 'lower'

The AttributeError occurs because we are trying to call the string lower() method on a Series object. The Series equivalent for lower() is under str.lower().

Solution #1: Use str.lower

To solve this error, we need to use str.lower() instead of lower() to convert the string values to lower case. Let’s look at the revised code:

series_lower = series.str.lower()
print(series_lower)

Let’s run the code to see the result:

0    learning
1      python
2          is
3      really
4        fun!
dtype: object

Solution #2: Use apply()

We can also use the Series method apply() to invoke the string lower() method on each of the string values in the Series. We will define a lambda function to call the string lower() method. Let’s look at the revised code:

series_lower = series.apply(lambda x: x.lower())

print(series_lower)

Let’s run the code to see the result:

0    learning
1      python
2          is
3      really
4        fun!
dtype: object

Summary

Congratulations on reading to the end of this tutorial! The AttributeError ‘Series’ object has no attribute ‘lower’ occurs when you try to call the string method lower() on a Series object.

To solve this error, you can use the Pandas method pandas.Series.str.lower().

You can also use apply() to apply the string lower() method to each string in the Series, for example:

dates.apply(lambda x: x.lower())

For further reading on Pandas, 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!