pandas datetime64列的中位数 [英] median of panda datetime64 column
问题描述
是否有一种方法可以计算并以datetime格式返回datetime列的中位数? 我想计算datetime64 [ns]格式的python列的中位数.下面是该列的示例:
Is there a way to compute and return in datetime format the median of a datetime column? I want to calculate the median of a column in python which is in datetime64[ns] format. Below is a sample to the column:
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
名称:新近度,dtype:datetime64 [ns]
Name: recency, dtype: datetime64[ns]
我的目标是使中位数与上面的日期列具有相同的日期时间格式:
My aim is to have the median in same datetime format as the date column above:
尝试转换为np.array:
Tried converting to 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无效
Converting to int64 and then calculating the median and attempt to the return format to datetime does not work
df['date'].astype('int64').median().astype('datetime64[ns]')
推荐答案
仅取中间值怎么样?
dates = list(df.sort('date')['date'])
print dates[len(dates)//2]
如果表格已排序,您甚至可以跳过一行.
If the table is sorted you can even skip a line.
这篇关于 pandas datetime64列的中位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!