【问题标题】:Resampling Error : cannot reindex a non-unique index with a method or limit重采样错误:无法使用方法或限制重新索引非唯一索引
【发布时间】:2017-02-09 02:46:06
【问题描述】:

我正在使用 Pandas 来构建和处理数据。

我这里有一个 DataFrame,其中日期为索引、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']})

给出:

这是我的代码,用于获取每次 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)

给出:

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

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

这就是结果:

这正是我想做的! 我有 38279 个具有相同列的日志,并且在执行相同操作时出现错误消息。第一部分完美运行,并给出了这个:

(df.groupby('Id').resample('1S').ffill())部分给出了这个错误信息:

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

有什么想法吗?谢谢!

【问题讨论】:

    标签: python python-2.7 pandas group-by resampling


    【解决方案1】:

    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: 无法使用方法或限制重新索引非唯一索引

    一种可能的解决方案是添加drop_duplicates 并将旧的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
    

    您也可以通过boolean indexing检查重复:

    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  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多