采用to_period就可以简单快速的实现,查了很多站内的方案,都需要一长串的函数定义,所以我来发布一个简单的方法实现吧。
df['months'] = dfenddate.dt.to_period('M') - df.begindate.dt.to_period('M')
如此,也不会因为要四舍五入导致的月份差数据错误。
采用to_period就可以简单快速的实现,查了很多站内的方案,都需要一长串的函数定义,所以我来发布一个简单的方法实现吧。
df['months'] = dfenddate.dt.to_period('M') - df.begindate.dt.to_period('M')
如此,也不会因为要四舍五入导致的月份差数据错误。