How can I fix the “Out of bounds nanosecond timestamp” error in Pandas?

How can I fix the “Out of bounds nanosecond timestamp” error in Pandas?

The “Out of bounds nanosecond timestamp” error in Pandas is typically caused by a timestamp value that is outside the range of valid nanoseconds. This can be fixed by converting the timestamp to a different data type or by adjusting the data to fit within the valid range of nanoseconds. Additionally, checking for any missing or invalid data in the dataset may also help resolve this error. By addressing these issues, the “Out of bounds nanosecond timestamp” error can be fixed, allowing for smooth and accurate data analysis in Pandas.

Fix in Pandas: Out of bounds nanosecond timestamp


One error you may encounter when using pandas is:

OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 2300-01-10 00:00:00

This error occurs when you attempt to create a timestamp that is outside of the following range:

import pandas as pd

#display minimum timestamp allowed
print(pd.Timestamp.min)

1677-09-21 00:12:43.145224193

#display maximum timestamp allowed print(pd.Timestamp.max)

2262-04-11 23:47:16.854775807

The following example shows how to fix this error in practice.

How to Reproduce the Error

Suppose we attempt to create a date range in pandas that contains the following three dates:

  • 1/1/2020
  • 1/1/2150
  • 1/1/2300

We can use the function to attempt to create this date range:

import pandas as pd

#attempt to create date range
some_dates = pd.date_range(start='1/1/2000', end='1/1/2300', periods=3)

OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 2300-01-10 00:00:00

We receive the OutOfBoundsDatetime error because the timestamp 1/1/2300 is greater than the max nanosecond timestamp allowed by pandas.

Even if you don’t want to store the timestamp using nanoseconds as the unit, pandas will automatically do so.

How to Fix the Error

The easiest way to get around this error is to use the errors = ‘coerce’ argument, which coerces any timestamps outside of the minimum or maximum range to NaT values.

For example, we can use the following code to create a date range and automatically coerce any timestamps outside of the allowable range to NaT values:

import pandas as pd

#create date range
some_dates = ['1/1/2000', '1/1/2150', '1/1/2300']
#convert date range to datetime and automatically coerce errors
some_dates = pd.to_datetime(some_dates, errors = 'coerce')

#show datetimes
print(some_dates)

DatetimeIndex(['2000-01-01', '2150-01-01', 'NaT'], dtype='datetime64[ns]', freq=None)

The result is a date range with three datetime values and the last datetime is NaT since it exceeded the max value allowed by pandas.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

Cite this article

stats writer (2024). How can I fix the “Out of bounds nanosecond timestamp” error in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-fix-the-out-of-bounds-nanosecond-timestamp-error-in-pandas/

stats writer. "How can I fix the “Out of bounds nanosecond timestamp” error in Pandas?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-fix-the-out-of-bounds-nanosecond-timestamp-error-in-pandas/.

stats writer. "How can I fix the “Out of bounds nanosecond timestamp” error in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-fix-the-out-of-bounds-nanosecond-timestamp-error-in-pandas/.

stats writer (2024) 'How can I fix the “Out of bounds nanosecond timestamp” error in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-fix-the-out-of-bounds-nanosecond-timestamp-error-in-pandas/.

[1] stats writer, "How can I fix the “Out of bounds nanosecond timestamp” error in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I fix the “Out of bounds nanosecond timestamp” error in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top