重新采样错误:无法使用方法或limi重新索引非唯一索引

2024-05-15 05:34:11 发布

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

我用熊猫来构造和处理数据。

这里有一个数据框,其中日期是索引、Id和比特率。 我想将我的数据按Id和重采样进行分组,同时,将与每个Id相关的时间日期进行分组,最后保留比特率分数。

例如,给定:

df = pd.DataFrame(
{'Id' : ['CODI126640013.ts', 'CODI126622312.ts'],
'beginning_time':['2016-07-08 02:17:42', '2016-07-08 02:05:35'], 
'end_time' :['2016-07-08 02:17:55', '2016-07-08 02:26:11'],
'bitrate': ['3750000', '3750000'],
'type' : ['vod', 'catchup'],
'unique_id' : ['f2514f6b-ce7e-4e1a-8f6a-3ac5d524be30', 'f2514f6b-ce7e-4e1a-8f6a-3ac5d524bb22']})

它给出:

enter image description here

这是我的代码,用于获取每次Id和比特率为的日期的唯一列:

df = df.drop(['type', 'unique_id'], axis=1)
df.beginning_time = pd.to_datetime(df.beginning_time)
df.end_time = pd.to_datetime(df.end_time)
df = pd.melt(df, id_vars=['Id','bitrate'], value_name='dates').drop('variable', axis=1)
df.set_index('dates', inplace=True)

它给出:

enter image description here

现在,是重新取样的时候了! 这是我的代码:

print (df.groupby('Id').resample('1S').ffill())

结果是:

enter image description here

这正是我想做的! 我有38279个具有相同列的日志,当我执行相同操作时,我会收到一条错误消息。第一部分工作得很好,并且给出了:

enter image description here

部分(df.groupby('Id').resample('1S').ffill())给出以下错误消息:

ValueError: cannot reindex a non-unique index with a method or limit

有什么想法吗?谢谢!


Tags: 数据iddftimetypeendpdunique
1条回答
网友
1楼 · 发布于 2024-05-15 05:34:11

似乎在列beginning_timeend_time中存在重复项问题,我尝试模拟它:

df = pd.DataFrame(
{'Id' : ['CODI126640013.ts', 'CODI126622312.ts', 'a'],
'beginning_time':['2016-07-08 02:17:42', '2016-07-08 02:17:42', '2016-07-08 02:17:45'], 
'end_time' :['2016-07-08 02:17:42', '2016-07-08 02:17:42', '2016-07-08 02:17:42'],
'bitrate': ['3750000', '3750000', '444'],
'type' : ['vod', 'catchup', 's'],
'unique_id':['f2514f6b-ce7e-4e1a-8f6a-3ac5d524be30', 'f2514f6b-ce7e-4e1a-8f6a-3ac5d524bb22','w']})

print (df)  
                 Id       beginning_time  bitrate             end_time  \
0  CODI126640013.ts  2016-07-08 02:17:42  3750000  2016-07-08 02:17:42   
1  CODI126622312.ts  2016-07-08 02:17:42  3750000  2016-07-08 02:17:42   
2                 a  2016-07-08 02:17:45      444  2016-07-08 02:17:42   

      type                             unique_id  
0      vod  f2514f6b-ce7e-4e1a-8f6a-3ac5d524be30  
1  catchup  f2514f6b-ce7e-4e1a-8f6a-3ac5d524bb22  
2        s                                     w  
df = df.drop(['type', 'unique_id'], axis=1)
df.beginning_time = pd.to_datetime(df.beginning_time)
df.end_time = pd.to_datetime(df.end_time)
df = pd.melt(df, id_vars=['Id','bitrate'], value_name='dates').drop('variable', axis=1)
df.set_index('dates', inplace=True)


print (df)  
                                   Id  bitrate
dates                                         
2016-07-08 02:17:42  CODI126640013.ts  3750000
2016-07-08 02:17:42  CODI126622312.ts  3750000
2016-07-08 02:17:45                 a      444
2016-07-08 02:17:42  CODI126640013.ts  3750000
2016-07-08 02:17:42  CODI126622312.ts  3750000
2016-07-08 02:17:42                 a      444

print (df.groupby('Id').resample('1S').ffill())

ValueError: cannot reindex a non-unique index with a method or limit

一种可能的解决方案是添加^{},并使用旧的way作为resamplegroupby

df = df.drop(['type', 'unique_id'], axis=1)
df.beginning_time = pd.to_datetime(df.beginning_time)
df.end_time = pd.to_datetime(df.end_time)
df = pd.melt(df, id_vars=['Id','bitrate'], value_name='dates').drop('variable', axis=1)

print (df.groupby('Id').apply(lambda x : x.drop_duplicates('dates')
                                          .set_index('dates')
                                          .resample('1S')
                                          .ffill()))

                                                    Id  bitrate
Id               dates                                         
CODI126622312.ts 2016-07-08 02:17:42  CODI126622312.ts  3750000
CODI126640013.ts 2016-07-08 02:17:42  CODI126640013.ts  3750000
a                2016-07-08 02:17:41                 a      444
                 2016-07-08 02:17:42                 a      444
                 2016-07-08 02:17:43                 a      444
                 2016-07-08 02:17:44                 a      444
                 2016-07-08 02:17:45                 a      444

您还可以通过^{}检查重复项:

print (df[df.beginning_time == df.end_time])
2        s                                     w  
                 Id       beginning_time  bitrate             end_time  \
0  CODI126640013.ts  2016-07-08 02:17:42  3750000  2016-07-08 02:17:42   
1  CODI126622312.ts  2016-07-08 02:17:42  3750000  2016-07-08 02:17:42   

      type                             unique_id  
0      vod  f2514f6b-ce7e-4e1a-8f6a-3ac5d524be30  
1  catchup  f2514f6b-ce7e-4e1a-8f6a-3ac5d524bb22  

相关问题 更多 >

    热门问题