ValueError: NaTType does not support strftime [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. ValueError: NaTType does not support strftime
  2. DateParseError: day is out of range for month

Note: If you got the error DateParseError: day is out of range for month, click on the second subheading.

# ValueError: NaTType does not support strftime [Solved]

The Pandas "ValueError: NaTType does not support strftime" occurs when you incorrectly convert a value to a datetime object, e.g. when iterating or when iterating in a for loop or when using DataFrame.apply().

To solve the error, call the pandas.to_datetime() method with the specific DataFrame column.

Here is an example of how the error occurs.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'date_joined': ['2022-01-25', '2022-02-31', '2023-01-01'] }) for date_joined in df['date_joined']: date = pd.to_datetime(date_joined, errors='coerce').strftime('%Y-%m-%d') # ⛔️ ValueError: NaTType does not support strftime print(date)

value error nat type does not support strftime

Notice that we tried using the pandas.to_datetime() method in a for loop.

This is not necessary and caused the error.

# Call the pandas.to_datetime() method outside a loop

To solve the error, call the pandas.to_datetime() method directly.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'date_joined': ['2022-01-25', '2022-02-31', '2023-01-01'] }) date = pd.to_datetime( df['date_joined'], errors='coerce' ).dt.strftime('%Y-%m-%d') print(date)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
0 2022-01-25 1 NaN 2 2023-01-01 Name: date_joined, dtype: object

call pandas to datetime method directly

  1. Notice that we passed the specific date column as the first argument to pandas.to_datetime().
  2. We also set the errors argument to "coerce" so that invalid parsing gets set as NaT.
  3. We then accessed the .dt attribute before calling strftime().
main.py
date = pd.to_datetime( df['date_joined'], errors='coerce' ).dt.strftime('%Y-%m-%d')

The DataFrame has an invalid date (2022-02-31), so a NaN value gets returned.

If you meant to overwrite the specific date column, use bracket notation.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'date_joined': ['2022-01-25', '2022-02-31', '2023-01-01'] }) df['date_joined'] = pd.to_datetime( df['date_joined'], errors='coerce' ).dt.strftime('%Y-%m-%d') # name salary date_joined # 0 Alice 175.1 2022-01-25 # 1 Bobby 180.2 NaN # 2 Carl 190.3 2023-01-01 print(df)

use bracket notation to overwrite specific date column

The code for this article is available on GitHub

The code sample converts the values in the date_joined column to datetime objects.

You will also get the error if you try to use the DataFrame.apply() method to call a function for each row/column or use a list comprehension to iterate over the values in the column and then call pandas.to_datetime().

Therefore, make sure to only call the pandas.to_datetime() method directly with the DataFrame column that stores dates without iterating over the column.

# Pandas DateParseError: day is out of range for month

The Pandas "DateParseError: day is out of range for month" error occurs for 2 main reasons:

  1. Using DataFrame.apply() before calling pandas.to_datetime().
  2. Having a date string formatted with the days at the start without indicating to Pandas that this is expected.

Here is an example of how the error occurs.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'date_joined': ['2022-01-25', '2022-02-31', '2023-01-01'] }) # ⛔️ pandas._libs.tslibs.parsing.DateParseError: day is out of range for month: 2022-02-31, at position 0 df['date_joined'] = df['date_joined'].apply( lambda x: pd.to_datetime(x).strftime('%Y-%m-%d'))

pandas date parse error day is out of range for month

The error in the example occurred because we used the DataFrame.apply() method instead of using pandas.to_datetime() directly with the date column.

Here is how we'd resolve the issue.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'date_joined': ['2022-01-25', '2022-02-31', '2023-01-01'] }) df['date_joined'] = pd.to_datetime( df['date_joined'], errors='coerce' ).dt.strftime('%Y-%m-%d') # name salary date_joined # 0 Alice 175.1 2022-01-25 # 1 Bobby 180.2 NaN # 2 Carl 190.3 2023-01-01 print(df)

resolve issue by calling to datetime directly

The code for this article is available on GitHub

# If your date format strings start with the day, set dayfirst to True

If your date format strings start with the day (e.g. DD-MM-YYYY), set the dayfirst argument to True when calling pandas.to_datetime().

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'date_joined': ['25-01-2022', '31-02-2022', '01-01-2023'] }) df['date_joined'] = pd.to_datetime( df['date_joined'], errors='coerce', dayfirst=True ).dt.strftime('%Y-%m-%d') # name salary date_joined # 0 Alice 175.1 2022-01-25 # 1 Bobby 180.2 NaN # 2 Carl 190.3 2023-01-01 print(df)

set dayfirst argument to true when calling datetime

The code for this article is available on GitHub

The boolean dayfirst argument defaults to False.

The argument specifies the date parsing order.

If it is set to True, the date is parsed with the day first, e.g. 20/02/2023 is parsed as 2023-02-20.

The errors argument is set to "coerce" so that values that cannot be parsed get set as NaT.

You can view the other possible values in this section of the docs.

# Passing the format argument to the pandas.to_datetime() method

Note that we can also pass a format argument to the pandas.to_datetime() method instead of calling .dt.strftime().

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'date_joined': ['25-01-2022', '31-02-2022', '01-01-2023'] }) df['date_joined'] = pd.to_datetime( df['date_joined'], errors='coerce', format='%d-%m-%Y', dayfirst=True ) # name salary date_joined # 0 Alice 175.1 2022-01-25 # 1 Bobby 180.2 NaT # 2 Carl 190.3 2023-01-01 print(df)

passing format argument when calling to datetime

The code for this article is available on GitHub

The format argument is a string that is used to specify the parsing format.

You can view the available directives in this section of the docs.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.