Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find total number of days in a year (pandas)

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.

like image 946
SunnyBoiz Avatar asked Dec 22 '25 12:12

SunnyBoiz


2 Answers

import datetime
import calendar
    
def days_in_year(year=datetime.datetime.now().year):
    return 365 + calendar.isleap(year)
like image 80
toomas Avatar answered Dec 24 '25 04:12

toomas


year = 2021
pd.Timestamp(year, 12, 31).dayofyear
like image 25
victor Avatar answered Dec 24 '25 04:12

victor