Python panda datetime64列的中位数
是否有方法计算并以datetime格式返回datetime列的中位数? 我想计算python中datetime64[ns]格式的列的中间值。以下是该列的示例:Python panda datetime64列的中位数,python,python-datetime,datetime64,Python,Python Datetime,Datetime64,是否有方法计算并以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 名称:最近,数据类型
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
名称:最近,数据类型: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]')
只取中间值怎么样
dates = list(df.sort('date')['date'])
print dates[len(dates)//2]
如果表格已排序,您甚至可以跳过一行。您已接近,则
中值()
返回一个浮点值
,因此首先将其转换为int
:
import math
median = math.floor(df['date'].astype('int64').median())
然后将表示日期的int
转换为datetime64
:
result = np.datetime64(median, "ns") #unit: nanosecond
您也可以尝试:
df['date'].astype('datetime64[ns]')。分位数(0.5,interpolation=“middpoint”)
谢谢@kabanus。这很有效。我没有想到排序和使用列的长度。@T-Jay很乐意帮忙。别忘了接受,让我感觉良好,为他人着想。
result = np.datetime64(median, "ns") #unit: nanosecond