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:
We can use the date_range() 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.
Notice that we don’t receive any error this time when creating the date range.
The following tutorials explain how to fix other common errors in Python:
How to Fix: columns overlap but no suffix specified
How to Fix: ‘numpy.ndarray’ object has no attribute ‘append’
How to Fix: if using all scalar values, you must pass an index