简体   繁体   中英

How to convert year-month-day to just years in python?

So I have this column in a dataframe that has 4896 rows of datatimes in this shape:

    2018-10-24
    2014-04-10
    2008-05-21
    ...

And I would like to transform or convert this data to years only (like 2018.15) -> convert the day and month to year time and have only the years!

How can I do so?? Thank you so much!!

In: 2018-10-24
Out: 2018.56 (for example)

Using just base Python (as you didn't specify that you have a pandas dataframe - pandas has specific functions to perform calculations with datetime objects):

from datetime import datetime
    
#takes as arguments the date as a string and an optional format string
def floatyear(str, fmtstr="%Y-%m-%d"):
    t = datetime.strptime(str, fmtstr)
    t_first = datetime(t.year, 1, 1, 0, 0, 0)
    t_last = datetime(t.year, 12, 31, 0, 0, 0)
    return t.year + ((t-t_first).days/(t_last-t_first).days)

print(floatyear("2018-10-24", "%Y-%m-%d"))

Sample output:

2018.8131868131868

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM