Skip to content
Advertisement

Does Pandas account for leap years when calculating dates

I am trying to add 148.328971 years precisely from the day 01.01.2000 using pandas. I first converted this to days by multiplying it by 365. So here is my question, albeit probably a dumb one.

Does pandas consider leap years when calculating days? The obvious answer is yes because it is calculating days but I have to make sure, precision of dates and time is important in the analysis I am trying to do.

I get this result when calculating with pandas which I am not sure is entirely correct:

03.25.2148 1:47:09 AM

code being used:

import pandas as pd
start = "01/01/2000"
end = pd.to_datetime(start) + pd.DateOffset(days=54140.074415)
print(end)

Any help would be greatly appreciated! Sorry in advance if this seems to be basic knowledge but I have to be certain

Advertisement

Answer

Yes, it does. However, your conversion from years to days is already ignoring the leap years. You can multiply by 365.25 (365.242, as suggested in the comments) which gives better results.

You can check the accuracy of the results on wolfram alpha: https://www.wolframalpha.com/input/?i=148.328971+years++from+01%2F01%2F2000

In addition, you can use pandas DateOffset with years. However, currently only integer values are supported.

import pandas as pd
start = "01/01/2000"
end = pd.to_datetime(start) + pd.DateOffset(years =148, days =0.328971*365.242)
print(end)

# 2148-04-30 03:45:35.229600

It seems to work well but misses by few hours.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement