Create a Pandas Timestamp Object
Let's find out how pandas Timestamp works in Python.
We'll cover the following
Try it yourself
Try executing the code below to see the result.
import pandas as pdy3k = pd.Timestamp(3000, 1, 1)print(f'They arrived to Earth on {y3k:%B %d}.')
Explanation
Computers and time have a complicated relationship because of timezones, leap years, and in some places, daylight saving times. Computers store time information by recording the number of seconds since January 1, 1970, GMT, a process known as epoch time.
Note: Because of how computers store time, time will overflow on 32-bit machines in 2038, a formatting bug known as the Year 2038 problem. Ouch!
Python’s datetime
and pandas.Timestamp
are designed around the way computers currently store time and are mostly written in C. There is also a fixed amount of space for storing time information. This
means there’s a maximum and minimum value to datetime
.
In [1]: pd.Timestamp.min
Out[1]: Timestamp('1677-09-21 00:12:43.145225')
In [2]: pd.Timestamp.max
Out[2]: Timestamp('2262-04-11 23:47:16.854775807')
The date in this teaser is more than the maximum pandas.Timestamp
value. This is documented under the “Timestamp Limitations” section in the pandas documentation.
Solution
import pandas as pdy3k = pd.Timestamp(2023, 1, 1)print(f'They arrived to Earth on {y3k:%B %d}.')
Get hands-on with 1300+ tech skills courses.