Is there a way to calculate the total number of days in a year given a datetime format of YYYY-MM-DD in Pandas? I have explored dt.dayofyear but this function seem to provide the number of days from the start of the year until my datetime variable.
As an example, if my date is 2020-03-30, I should know that this is the year of 2020 and there are 366 days (leap year). If it is 2019-03-30, then it should return 365 days (non-leap year).
Tried to search through the pandas dt documentation but can't seem to find any.
Thanks.
import datetime
import calendar
def days_in_year(year=datetime.datetime.now().year):
return 365 + calendar.isleap(year)
year = 2021
pd.Timestamp(year, 12, 31).dayofyear
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With