groupby datediff在熊猫
问题描述:
我试图得到最小日期和最大日期之间的区别,按月在新的专栏中出售产品。但是,在groupby中应用函数时,我有一个不寻常的回报。groupby datediff在熊猫
任何帮助,非常感谢。
所以我的步骤是:
数据:
pch_date day product qty unit_price total_price year_month
421 2013-01-07 tuesday p3 13 4.58 59.54 1
141 2015-09-13 monday p8 3 3.77 11.31 9
249 2015-02-02 monday p5 3 1.80 5.40 2
826 2015-10-09 tuesday p5 6 1.80 10.80 10
427 2014-04-18 friday p7 6 4.21 25.26 4
函数定义:
def diff_date(x):
max_date = x.max()
min_date = x.min()
diff_month = (max_date.year - min_date.year)*12 + max_date.month +1
return diff_month
当试图测试:
print diff_date(prod_df['pch_date'])
49这是正确的
但问题:
print prod_df[['product','pch_date']].groupby(['product']).agg({'pch_date': diff_date}).reset_index()[:5]
结果与一个额外的日期即将到来:
product pch_date
0 p1 1970-01-01 00:00:00.000000049
1 p10 1970-01-01 00:00:00.000000048
2 p11 1970-01-01 00:00:00.000000045
3 p12 1970-01-01 00:00:00.000000049
4 p13 1970-01-01 00:00:00.000000045
如何获得在整数区别?
答
您可以使用Groupby.apply
,而不是返回整数而不是日期时间对象。
df.groupby(['product'])['pch_date'].apply(diff_date).reset_index()
至于不让整数值解决方法得到转化为自己的DatetimeIndex值,你可以在你的函数的最后一行更改为str(diff_month)
,如图所示,你可以继续使用Groupby.agg
:
df.groupby(['product'])['pch_date'].agg({'pch_date': diff_date}).reset_index()
谢谢尼基尔 - 你让我的一天 –