Home > Back-end >  How to convert year-month-day to just years in python?
How to convert year-month-day to just years in python?

Time:04-14

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)

CodePudding user response:

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
  • Related