为什么是pd.qcut公司()产生巨大的边界?

2024-10-03 17:19:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个事件数据的数据帧,其中一列是事件发生的时间间隔。我想使用pd.qcut()使每个区间的百分位数给定其中的事件,并为每个事件指定其各自的百分位数。你知道吗

def event_quartiler(event_row):
    in_interval = paired_events.loc[events['TimeInterval'] == event_row['TimeInterval']]
    quartiles = pd.qcut(in_interval['DateTime'], 100)
    counter = 1
    for quartile in quartiles.unique():
        if(event_row['DateTime'] in quartile):
            return counter
        counter = counter+1
        if(counter > 100): break
    return -1

events['Quartile'] = events.apply(event_quartiler, axis=1)

我原以为这会简单地将四分位列设置为每个事件各自的百分位,但相反,代码需要花费很长时间才能运行,并通过输出以下内容有效地结束:

ValueError: ("Bin edges must be unique: array([1.55016605e+18, 1.55016616e+18, 1.55016627e+18, 1.55016632e+18,\n       1.55016632e+18, 1.55016636e+18,
... (I put the ellipsis here because there are 100 data points) 
1.55017534e+18, 1.55017545e+18,\n       1.55017555e+18]).\nYou can drop duplicate edges by setting the 'duplicates' kwarg", 'occurred at index 6539')

6539处的数据或其间隔内的任何事件都没有什么不同,但我也找不到代码哪里出错了。你知道吗


Tags: 数据ineventdatetime间隔counter事件events
1条回答
网友
1楼 · 发布于 2024-10-03 17:19:18

我发现了问题所在:qcut试图将所有的数据点自己拟合成四分位数,而cut则取最小值和最大值,并将其切割成n个箱子。因为在这个例子中,我试图生成的四分位数比实际的数据点多,所以qcut失败了。你知道吗

只需使用分为100箱解决了我的问题,我能够使百分位数。你知道吗

相关问题 更多 >