n 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 values in a Series that you want to convert to datetime, you cannot use to_datetime()
directly on the Series. If you try to call to_datetime()
directly on a Series object, you will raise the AttributeError: ‘Series’ object has no attribute ‘to_datetime’. to_datetime()
is a built-in method to Pandas, which can accept a Series object as an argument, for example, pandas.to_datetime(series).
This tutorial will go through the error in detail and how to solve it with code examples.
Table of contents
AttributeError: ‘Series’ object has no attribute ‘to_datetime’
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 ‘to_datetime’” tells us that the Series object we are handling does not have the to_datetime attribute. The to_datetime()
method is a built-in Pandas method that we can use to convert a Series argument to a datetime type. We cannot call to_datetime on a Series like series.to_datetime()
. Instead, we have to pass the Series to the to_datetime()
method.
Example
Let’s look at an example with a Series containing epoch timestamp values. We want to use to_datetime()
method to convert the values to human-readable dates. Let’s look at the original Series object:
import pandas as pd s = pd.Series([1490195805, 1598495821, 1237495321, 1444899912]) print(s)
0 1490195805 1 1598495821 2 1237495321 3 1444899912 dtype: int64
The Series contains integer values representing epoch timestamp values. Let’s look at the code to convert the values to datetime:
s_datetime = s.to_datetime(unit='s') print(s_datetime)
Let’s run the code to see the result:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-6-0b06c7ebfe5b> in <module> 3 s = pd.Series([1490195805, 1598495821, 1237495321, 1444899912]) 4 ----> 5 s_datetime = s.to_datetime(unit='s') 6 7 print(s_datetime) ~/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 'to_datetime'
The Python interpreter raises the AttributeError because to_datetime is an attribute of Pandas, not Series.
Solution
We need to pass the Series to the to_datetime()
method as an argument to solve this error. Let’s look at the revised code:
import pandas as pd s = pd.Series([1490195805, 1598495821, 1237495321, 1444899912]) # Unit of the epoch timestamps is in seconds, set unit to 's' s_datetime = pd.to_datetime(s, unit='s') print(s_datetime)
Let’s run the code to see the result:
0 2017-03-22 15:16:45 1 2020-08-27 02:37:01 2 2009-03-19 20:42:01 3 2015-10-15 09:05:12 dtype: datetime64[ns]
We converted the epoch timestamp values to human-readable dates using to_datetime()
. The dtype of the Series object is datetime64
.
Summary
Congratulations on reading to the end of this tutorial! The AttributeError: ‘Series’ object has no attribute ‘to_datetime’ occurs when you try to call the to_datetime()
method on a Series object. to_datetime()
is a built-in method, which means you can call it directly once you have imported Pandas. For example:
import pandas as pd
pd.to_datetime(...)
You can find out more about the uses of pandas.to_datetime() by reading the Pandas documentation.
For further reading on Series, go to the articles:
- How to Solve Python AttributeError: ‘str’ object has no attribute ‘contains’.
- How to Solve Python AttributeError: ‘Series’ object has no attribute ‘lower’.
- How to Solve Python AttributeError: ‘DataFrame’ object has no attribute ‘sort’
For further reading on the datetime module, go to the article:
How to Solve Python AttributeError: ‘datetime.datetime’ has no attribute ‘datetime’
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!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.