Python 日期的分位数函数?
我有一个捐款金额和日期的数据框。我想知道一定比例的捐款需要多长时间才能到位(在什么时候我们有25%的捐款?75%?)。看起来熊猫分位数函数可以满足我的要求。然而,它似乎只想要数字,而不是日期。是否有一个函数可以对日期执行相同的操作Python 日期的分位数函数?,python,pandas,Python,Pandas,我有一个捐款金额和日期的数据框。我想知道一定比例的捐款需要多长时间才能到位(在什么时候我们有25%的捐款?75%?)。看起来熊猫分位数函数可以满足我的要求。然而,它似乎只想要数字,而不是日期。是否有一个函数可以对日期执行相同的操作 就像Evert所说的,您可以将其临时转换为int 64 compute并转换回datetime YOUR_DATAFRAME.YOUR_DATE.astype('int64').quantile([.25,.5,.75]).astype('datetime64[ns]
就像Evert所说的,您可以将其临时转换为int 64 compute并转换回datetime
YOUR_DATAFRAME.YOUR_DATE.astype('int64').quantile([.25,.5,.75]).astype('datetime64[ns]')
就像Evert所说的,您可以暂时将其转换为int 64 compute,然后再转换回datetime
YOUR_DATAFRAME.YOUR_DATE.astype('int64').quantile([.25,.5,.75]).astype('datetime64[ns]')
我也有同样的问题,在我的例子中,为了一个机器学习问题而拆分一个时间序列 根据和的上述回答,我写了以下内容,并添加了日期可以写成字符串的情况:
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
我也有同样的问题,在我的例子中,为了一个机器学习问题而拆分一个时间序列 根据和的上述回答,我写了以下内容,并添加了日期可以写成字符串的情况:
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
是否无法(临时)将日期转换为(Unix)时间戳,执行分位数操作,然后将结果转换回日期(时间)对象?是否无法(临时)将日期转换为(Unix)时间戳,执行分位数操作,然后将结果转换回日期(时间)对象?