有沒有辦法計算和返回日期時間格式的日期時間列的中位數? 我想計算python中datetime64 [ns]格式的列的中位數。下面是一個示例,以列:熊貓datetime64列的中位數
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
名稱:新近度,D型細胞: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的,然後計算中位數和嘗試返回格式的日期時間不工作
df['date'].astype('int64').median().astype('datetime64[ns]')
謝謝@kabanus。這很好。我沒有想到要對列的長度進行排序和使用。 –
@ T-Jay樂於助人。不要忘記接受讓我感覺良好並且爲了他人的利益。 – kabanus