How can I convert 23 years 0 months into numerical value so that I can use it in Predictive Modelling - excel

I have a dataset in which values are in this form
23 years 0 months or
2 years 6 months
How can I convert it in numerical data or any other form so that it can be used in predictive modelling , Using pandas

Based on what ALollz said in the comments, you could split the line by spaces and add the months as a percentage of a year
a = "23 years 6 months"
b = a.split(" ")
print(str(float(b[0]) + float(b[2])/12))
Output: 23.5

I'm a fan of coding to let me easily change the output in the future so this is what I came up with:
convs = {'ye': 1, 'mo': 12, 'we': 52.1429, 'da': 365} # conversion rates to a year
conv_to = 'ye' # choose your desired unit!
times = {i: convs[conv_to] / float(convs[i]) for i in convs}
s = "1 year 1 month 1 week 1 day"
s = s.split(" ")
s = list(zip(s[0::2], s[1::2]))
converted_amt = sum([float(times[i[1].lower()[:2]]) * float(i[0]) for i in s])
print(converted_amt)
With that you can easily add more granularity to support hours minutes, and more.
Input:
conv_to = 'mo'
s = "23 years 2 months 3 weeks 24 days"
Output:
279.4794514873339
Input:
conv_to = 'ye'
s = "8 years 9 months 0 weeks 3 day"
Output:
3196.75
Input (works with any order):
conv_to = 'da'
s = "2 days 1 week"
Output:
8.999994246580073
As you can see it won't be totally accurate converting to days when weeks are used, as the value I use is an average found on Google. Rounding should help this.

Related

Split the date into 1st, 2nd, 3rd, 4th week of the month

I have a data frame of dates:
id|date
13|2017-01-31
12|2016-12-07
11|2013-03-19
I want to classify the dates into weather it is the 1st, 2nd, 3rd or 4th week of the month.
I am using this function:
calendar.setfirstweekday(calendar.SUNDAY)
def get_week_of_month(dt):
year = dt.year
month = dt.month
day = dt.day
x = np.array(calendar.monthcalendar(year, month))
week_of_month = np.where(x==day)[0][0] + 1
return(week_of_month)
df['week_month'] = df['date'].apply(get_week_of_month)
However I am getting 6 possible weeks instead of 4. Please help
Like this:
In [1036]: df['date']= pd.to_datetime(df['date'])
In [1044]: df['week_of_month'] = ((df['date'].dt.day-1)//7)+1
In [1045]: df
Out[1045]:
id date week_of_month
0 13 2017-01-31 5
1 12 2016-12-07 1
2 11 2013-03-19 3

how to get difference between two dates in sharepoint calculated column

how to get different between two dates: column1 "joining date" column2 "leaving date", in sharepoint calculated column as "total experience of an employee"?
The desired output I'm searching is in this format for example: "2 days 2 months 2 years".
this formula should give you the answers that you are after
= DATEDIF([Date 1],[Date 2],"MD")&" Days "& DATEDIF([Date 1],[Date 2],"YM") &" Months "& DATEDIF([Date 1],[Date 2],"Y") &" Years"
Steps:
Create a calculated column of type Single Line of text
Use the formula above.
Explanation:
Formula goes as
DATEDIF(Start_date,End_date,"Interval")
Where interval can be:
Y Calculate the number of complete years
M Calculate the number of complete months
D Calculate the number of days
YM Calculate the number of months excluding years
MD Calculate the number of days excluding years and months
YD Calculate the number of days excluding years
For 2 dates like Date 1 (31/01/2020) and Date 2 (14/05/2020), the results will be:
14 Days 3 Months 0 Years

Calculating time to earn a specific amount as interest

I cannot figure out the approach to this as the principle amount shall change after every year(if calculated annually, which shall be the easiest). Eventual goal is to calculate exact number of years, months and days to earn say 150000 as interest on a deposit of 1000000 at an interest rate of say 6.5%. I have tried but cannot seem to figure out how to increment the year/month/day in the loop. I don't mind if this is down voted because I have not posted any code(Well, they are wrong). This is not as simple as it might seem to beginners here.
It is a pure maths question. Compound interest is calculated as follows:
Ptotal = Pinitial*(1+rate/100)time
where Ptotal is the new total. rate is usually given in percentages so divide by 100; time is in years. You are interested in the difference, though, so use
interest = Pinitial*(1+rate/100)time – Pinitial
instead, which is in Python:
def compound_interest(P,rate,time):
interest = P*(1+rate/100)**time - P
return interest
A basic inversion of this to yield time, given P, r, and target instead, is
time = log((target+Pinitial)/Pinitial)/log(1+rate/100)
and this will immediately return the number of years. Converting the fraction to days is simple – an average year has 365.25 days – but for months you'll have to approximate.
At the bottom, the result is fed back into the standard compound interest formula to show it indeed returns the expected yield.
import math
def reverse_compound_interest(P,rate,target):
time = math.log((target+P)/P)/math.log(1+rate/100)
return time
timespan = reverse_compound_interest(2500000, 6.5, 400000)
print ('time in years',timespan)
years = math.floor(timespan)
months = math.floor(12*(timespan - years))
days = math.floor(365.25*(timespan - years - months/12))
print (years,'y',months,'m',days,'d')
print (compound_interest(2500000, 6.5, timespan))
will output
time in years 2.356815854829652
2 y 4 m 8 d
400000.0
Can we do better? Yes. datetime allows arbitrary numbers added to the current date, so assuming you start earning today (now), you can immediately get your date of $$$:
from datetime import datetime,timedelta
# ... original script here ...
timespan *= 31556926 # the number of seconds in a year
print ('time in seconds',timespan)
print (datetime.now() + timedelta(seconds=timespan))
which shows for me (your target date will differ):
time in years 2.356815854829652
time in seconds 74373863.52648607
2022-08-08 17:02:54.819492
You could do something like
def how_long_till_i_am_rich(investment, profit_goal, interest_rate):
profit = 0
days = 0
daily_interest = interest_rate / 100 / 365
while profit < profit_goal:
days += 1
profit += (investment + profit) * daily_interest
years = days // 365
months = days % 365 // 30
days = days - (months * 30) - (years * 365)
return years, months, days
years, months, days = how_long_till_i_am_rich(2500000, 400000, 8)
print(f"It would take {years} years, {months} months, and {days} days")
OUTPUT
It would take 1 years, 10 months, and 13 days

In Excel, how do I calculate days between two dates ignoring full years?

How do I calculate relative years + days between two dates in excel? I want to know the number of years and any additional days (outside of the full years). Dividing the total days by 365 doesn't work because of leap years. For example:
Cell A1 = '1/31/2015'
Cell B1 = '2/1/2025'
Cell C1 = '2/11/2025'
=(B1-A1)/365 # 10.0109 = 10 years + 4 days, looking for 10 years + 0 days
=(C1-A1)/365 # 10.0383 = 10 years + 14 days, looking for 10 years + 10 days
Is there an easy way to calculate this?
Note - this question is not a duplicate of How to find the difference between dates in VBA -- which is a question about calculating the difference between dates. This question is asking how to calculate the difference of both years and days, such that the difference in days is for the final year only, and are not incorrectly including leap year days for previous years.
You can use the documented function DATEDIF
=DATEDIF(startdate,enddate,"Y")
This will give you the difference in whole years
“Y” Returns the period difference as complete years.
“M” Returns the period difference as complete months.
“D” Returns the number of days in the period.
“MD” Returns the difference between the days in ‘Start_Date’ and ‘End_Date’. Here the months and years of the dates are ignored.
“YM” Returns the difference between the months in ‘Start_Date’ and ‘End_Date’. Here the days and years of the dates are ignored
“YD” Returns the difference between the days of ‘Start_Date’ and ‘End_Date’. Here the years of the dates are ignored.
Excel Solution :
=DATEDIF(A1,A2,"y") & " years, " & DATEDIF(A1,A2,"ym") & " months, " & DATEDIF(A1,A2,"md") & " days"
Please refer this link

How to calculate total number of days in between two dates in groovy [duplicate]

This question already has answers here:
Duration between two dates in Groovy
(2 answers)
Closed 9 years ago.
How to calculate total number of months and year in between two dates in groovy
date1 =2012 nov 1
date2 =2013 feb 1
required output is year = 0 and month =3.
Any answer is helpful
I think this should do:
monthBetween = (start[Calendar.MONTH] - end[Calendar.MONTH]) + 1
yearsBetween = start[Calendar.YEAR] - end[Calendar.YEAR]
months = monthBetween + (yearsBetween * 12)
Taken from Calculate difference in months between two dates with Groovy

Resources