簡體   English   中英

熊貓分位數功能的日期?

[英]Pandas quantile function for dates?

我有一個捐贈金額和日期的數據框。 我想看看捐贈的一定比例需要多長時間(在什么時候我們有25%的捐贈?,75%的捐贈)。 看起來Pandas分位數功能可以滿足我的要求。 但是,它似乎只需要數字,而不是日期。 是否存在與日期相同的功能?

http://pandas.pydata.org/pandas-docs/dev/generated/pandas.core.groupby.DataFrameGroupBy.quantile.html#pandas.core.groupby.DataFrameGroupBy.quantile

就像Evert所說的那樣,您可以將其臨時轉換為int 64計算,然后轉換回datetime

YOUR_DATAFRAME.YOUR_DATE.astype('int64').quantile([.25,.5,.75]).astype('datetime64[ns]')

我遇到了同樣的問題,在我的情況下,為機器學習問題拆分了一個時間序列。

我寫了基於上述回答以下埃弗特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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM