median of panda datetime64 column

浪子不回头ぞ 提交于 2019-12-01 07:38:58

How about just taking the middle value?

dates = list(df.sort('date')['date'])
print dates[len(dates)//2]

If the table is sorted you can even skip a line.

You can also try quantile(0.5) with some conversions, which is not quite the same as the median if the length of the data frame is even, but this might suffice:

df['date'].astype('datetime64[ns]').quantile(.5)

You are close, the median() return a float so convert it to be an int first:

import math

median = math.floor(df['date'].astype('int64').median())

Then convert the int represent the date into datetime64:

result = np.datetime64(median, "ns") #unit: nanosecond
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!