Reputation: 117
I have a variable "Age" with varying measures for age stored as a string. Example:
Age = ("3 weeks" , "2 years" , "1 day", "4 weeks")
I am interested in using the time measure (weeks, years, day)
to convert the variable to an integer expressing the number in the string as a fraction of a year. In other words, I want to convert 3 weeks into the equivalent of 3/52 in int form.
Any suggestions on how I can do this in pandas? Appreciate any advice that is forthcoming.
M
Upvotes: 0
Views: 530
Reputation: 879729
Using parsedatetime
,
import datetime as DT
import pandas as pd
import parsedatetime as pdt
today = DT.date.today()
def parse(x, p=pdt.Calendar()):
return DT.datetime(*p.parse(x, today.timetuple())[0][:6])
age = ("3 weeks" , "2 years" , "1 day", "4 weeks")
s = pd.Series(age)
s = s.map(parse) - today
s = s / pd.Timedelta(1, unit='Y')
print(s)
yields
0 0.057496
1 1.998672
2 0.002738
3 0.076661
dtype: float64
Upvotes: 2
Reputation: 231
This will do what you want, I think, using Python lists:
#function to convert each string to fraction in years
def word2time(strVal):
num,word = strVal.split()
num = int(num)
if word == 'weeks' or word == 'week':
return float(num)/52
elif word == 'days' or word == 'day':
return float(num)/365
elif word == 'years' or word == 'year':
return num
#demonstration on the input you provided
Age = ['3 weeks', '2 years', '1 day', '4 weeks']
ageInYrs = []
for strVal in Age:
ageInYrs.append(word2time(strVal))
print ageInYrs
Upvotes: 0
Reputation: 13800
This should work:
d = {"weeks":52,"years":1,"day":365}
[float(i.split(" ")[0])/d[i.split(" ")[1]] for i in Age]
Note that this assumes that all your data is split by a whitespace, and you only have "day" in the data set - if you have instances of "days" you'd have to add that to the dict.
Upvotes: 1