6.9 C
London
Thursday, December 19, 2024
HomePythonFix Common Errors in PythonHow to Fix in Pandas: Out of bounds nanosecond timestamp

How to Fix in Pandas: Out of bounds nanosecond timestamp

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 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.

Additional Resources

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories