How to convert Python datetime dates to decimal/float years

前端 未结 7 1386
感动是毒
感动是毒 2020-12-09 18:18

I am looking for a way to convert datetime objects to decimal(/float) year, including fractional part. Example:

>>> obj = SomeObjet()
>>> o         


        
7条回答
  •  半阙折子戏
    2020-12-09 19:09

    It's possible to calculate decimal date by using Pandas's julian date and the following formulas.

    In the case where your pandas dataframe has an index that is date-time:

    JD=dat.index.to_julian_date() #create julian date
    L= JD+68569
    N= 4*L/146097
    L= L-(146097*N+3)/4
    I= 4000*(L+1)/1461001
    L= L-1461*I/4+31
    J= 80*L/2447
    K= L-2447*J/80
    L= J/11
    J= J+2-12*L
    decimal_date= 100*(N-49)+I+L
    

    decimal_date is a series of your date (in the same TZ as the dataframe index) in form of something like 2007.123452.

    Adapted from this post.

提交回复
热议问题