我有捐赠金额和日期的数据框。我想知道一定比例的捐款需要花多长时间(在什么时候我们有25%的捐款?75%?)。看起来像Pandas分位数功能会做我想要的。然而,它似乎只想要数字,而不是日期。是否有一个与日期相同的函数?
答案 0 :(得分:3)
与Evert一样,您可以暂时将其转换为int 64计算并转换回日期时间
YOUR_DATAFRAME.YOUR_DATE.astype('int64').quantile([.25,.5,.75]).astype('datetime64[ns]')
答案 1 :(得分:1)
我有同样的问题,在我的情况下,分割机器学习问题的时间序列。
我根据evert和steboc的上述答案撰写了以下内容,并添加了日期可能写为字符串的情况:
def get_split_date(df, date_column, quantile):
""" Get the date on which to split a dataframe for timeseries splitting """
# 1. convert date_column to datetime (useful in case it is a string)
# 2. convert into int (for sorting)
# 3. get the quantile
# 4. get the corresponding date
# 5. return, pray that it works
quantile_date = pd.to_datetime(df[date_column], coerce = True).astype('int64').quantile(q=quantile).astype('datetime64[ns]')
return quantile_date