Let's say I have a dataframe with two columns that contain dates, and I want to create a new columns whose value is the number of months between those dates.
>df
Index Date1 Date2
1 2012/03/07 2013/03/16
2 2012/12/05 2012/12/25
3 2010/06/30 2013/05/19
4 2002/11/02 2011.06.08
df["Date1"]= pd.to_datetime(df["Date1"])
df["Date2"]= pd.to_datetime(df["Date2"])
Date1 will always be before date2. My current method of doing this requires about 10 steps, and I'm pretty sure there's an easier way to do this. Thoughts?
see this link: http://pandas.pydata.org/pandas-docs/dev/timeseries.html#time-deltas
(df['Date2']-df['Date1']).apply(lambda x: x/np.timedelta64(1,'M'))
for numpy >=1.7 (see the link if you are using 1.6.1)
I am not sure what it will do with the fraction. (usually I would divide by np.timedelta64(1,'D')
then divide by say 30 to make a fractional num of months (as a float)
I'm not sure how to do it in python but the steps I would do:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.