How to Solve Python ValueError: year is out of range

by | Programming, Python, Tips

If you try to pass a timestamp to the datetime fromtimestamp() method that is out of range, you will raise the ValueError: year is out of range. This error typically is a result of passing a timestamp in milliseconds, while the fromtimestamp() method takes the timestamp in seconds.

You can solve this error by dividing the timestamp in milliseconds by 1000 to get the timestamp in seconds. For example,

from datetime import datetime

timestamp_ms = 1657577891147

timestamp_sec = timestamp_ms / 1000

current_dt = datetime.fromtimestamp(timestamp_sec)

This tutorial will go through the error in detail and how to solve it with code examples.


Python ValueError: year is out of range

In Python, a value is information stored within a particular object. We will encounter a ValueError in Python when we use an operation or function that receives an argument with the right type but an inappropriate value.

An integer representing timestamp is a suitable type for the datetime.fromtimestamp() method, but expects values between the years 1970 and 2038 (at the time of writing this article). If we pass a timestamp outside of this range, the value is inappropriate, and the Python interpreter will raise the ValueError.

Example

Let’s look at an example of trying to convert a timestamp to a datetime object. First, we will get the current time in milliseconds.

current_time_in_ms = 1657578261543

Next, we will attempt to pass this variable as the argument for the datetime.fromtimestamp() method, to convert it into a datetime.

from datetime import datetime

current_dt = datetime.fromtimestamp(current_time_in_ms)

print(current_dt)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [19], in <cell line: 3>()
      1 from datetime import datetime
----> 3 current_dt = datetime.fromtimestamp(current_time_in_ms)
      5 print(current_dt)

ValueError: year 54496 is out of range

The error occurs because we passed a timestamp value in milliseconds, whereas the fromtimestamp() method expects the timestamp in seconds.

Solution

We can solve this error by dividing the timestamp value by 1000 to convert it to seconds.

Let’s look at the revised code:

from datetime import datetime

current_time_in_ms = 1657578261543

current_time_in_s = current_time_in_ms / 1000

current_dt = datetime.fromtimestamp(current_time_in_s)

print(current_dt)

Let’s run the code to get the result:

2022-07-11 23:24:21.543000

We successfully retrieved the local datetime corresponding to the timestamp in seconds.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on Python ValueErrors, go to the article:

How to Solve Python ValueError: empty separator

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!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!