-1

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)
Sara Silva
  • 19
  • 7
  • 2
    Where does the `.56` come from in your example? – Iain Shelvington Apr 13 '22 at 09:11
  • There are multiple ways of doing it. One would be: `df['year'] = [date.split('-')[0] for date in df['column_name']]` – Pablompg Apr 13 '22 at 09:14
  • @IainShelvington it's just an example! It's not necessarily the answer – Sara Silva Apr 13 '22 at 09:17
  • @SaraSilva but what is the number after the period supposed to represent or be calculated from? It's not clear what this means "convert the day and month to year time" – Iain Shelvington Apr 13 '22 at 09:20
  • for example, you can pass a year into days only and you know thats 365. So a day into a year would be 1/365...The same goes for month. Like, the date 13/04/2022 it's one year and something more (4 months ans 13 days), not just one exact year – Sara Silva Apr 13 '22 at 09:23
  • I don't understand what you are asking for other than to change the format. – RedRaptor Apr 13 '22 at 09:32
  • 1
    half a year could be represented as 6 month, or 0.5year. So let say a given date is 2020-06-01. At this date we are half way through the year 2020. One could say the current date is 2020.5 (half a year more than 2020). This can be generalized to any date or year, and that's what he want: convert 2020-06-01 to 2020.5 – jeandemeusy Apr 13 '22 at 09:43
  • 1
    What you are looking for is called a year fraction. When used in calculating interest payments, take the number of days accurately into account. That depends on the *daycount convention*, which varies with financial instrument and market, such as *Actual/Actual*, *Actual/365* (divisor ignores leap days), *30/360* (months count as 30 days), *30/360E* (same, but using European rules). To do arithmetic with the resulting numbers, split the intervening period at boundaries between common years and leap years. So your question is not nearly as simple as you seem to think. – BoarGules Apr 13 '22 at 09:49
  • @jeandemeusy exactly!!!!!! – Sara Silva Apr 13 '22 at 09:50
  • @BoarGules could you help me?? – Sara Silva Apr 13 '22 at 10:03

1 Answers1

1

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
Mr. T
  • 11,960
  • 10
  • 32
  • 54