python - 重采样错误 : cannot reindex a non-unique index with a method or limit

标签 python python-2.7 pandas group-by resampling

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

我这里有一个以日期作为索引、ID 和比特率的 DataFrame。 我想按 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

有什么想法吗?谢谢!

最佳答案

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

一种可能的解决方案是添加 drop_duplicates并使用旧的 way对于使用 groupbyresample:

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  

关于python - 重采样错误 : cannot reindex a non-unique index with a method or limit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39792933/

相关文章:

Python - 对 Pandas 数据框进行两次排序

python - 在 Pandas DF 中使用删除重复项,但根据首选项列表选择保留列

Python:变量的使用及其区别 ("a, b = 0, 1"VS "a = 0", "b = 1")

python-2.7 - Flask 管理员显示枚举值而不是名称

python - 使用 str 中的常量值在 pandas df 中添加日期列

python-2.7 - 如何使用xlwings在Excel中获取公式结果

python - 使用 Pandas 将多个数据帧合并到单个列上,同时保留 "on"列

Python 缩进错误 : too many levels of indentation

python - 如何使用 ADC 在 Raspberry Pi 中获得最高采样率?

python - Scikit-learn微调: Postprocess predicted labels before evaluation