python - use data from two or more columns when using .map to apply a function -
i want calculate difference between 2 dates in format yyyy-mm-dd. when write df["diff"] = df["date1"] - df["date2"] value 75 days can't sort.
how can write such map function calc_diff(date1,date2) where
def calc_diff(date1,date2): x = float(date2-date1) return x
i.e. df["diff"] = df["date1"],df["date2"].map(calc_diff)
when write following, get:
df["until_payable"] = map(calc_diff, df["ex_div_date"], "date_payable") def calc_diff(d1,d2): y = pd.to_datetime(d1) x = pd.to_datetime(d2) return x-y
error:
<ipython-input-167-b151c936730f> in calc_diff(d1, d2) 39 y = pd.to_datetime(d1) 40 x = pd.to_datetime(d2) ---> 41 return x-y 42 43 #convert data in columns data types pandas/tslib.pyx in pandas.tslib._timestamp.__sub__ (pandas/tslib.c:17620)() typeerror: descriptor '__sub__' requires 'datetime.datetime' object received 'str'
if understood correctly solution problem is:
res = map(calc_diff, date1, date2)
please note date1
, date2
should same length
Comments
Post a Comment