有没有一种方法可以计算并以datetime格式返回datetime列的中位数? 我想在Python中计算一个datetime64[ns]格式的列的中位数。以下是该列的样本:
df['date'].head()
0 2017-05-08 13:25:13.342
1 2017-05-08 16:37:45.545
2 2017-01-12 11:08:04.021
3 2016-12-01 09:06:29.912
4 2016-06-08 03:16:40.422
名称:recency,数据类型:datetime64[ns]
我的目标是使中位数与上面的日期列具有相同的日期时间格式:
尝试转换为np.array:
median_ = np.median(np.array(df['date']))
但是那会抛出错误:
TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('<M8[ns]')
将数据类型转换为int64,然后计算中位数并尝试将返回格式转换为datetime是无效的。
df['date'].astype('int64').median().astype('datetime64[ns]')