Skip to content Skip to sidebar Skip to footer

Median Of Panda Datetime64 Column

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. Bel

Solution 1:

You can also try quantile(0.5):

df['date'].astype('datetime64[ns]').quantile(0.5, interpolation="midpoint")

Solution 2:

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.


Solution 3:

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

Post a Comment for "Median Of Panda Datetime64 Column"