2016-06-07 109 views
-2

我想用熊猫做一些与ffill一些常规时间系列,但我得到一个非唯一索引错误。熊猫.asfreq给出了一个重复的索引错误

这里是我的代码:

for d in data_types: 
    series = df[df['datatype'] == d]['measurementvalue'].values 
    times = df[df['datatype'] == d]['displaydate'].values 
    data_series = pd.Series(series, index = times) 
    data_series.drop_duplicates(inplace = True) 
    data_series.asfreq('30Min', method = 'ffill') 
    all_series.append(data_series) 

我收到以下错误作为asfreq调用一个特定data_type结果:

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

这是一组数据,其中drop_duplicates导致长度从2119降至1299,表明它是最密集的(时间明智的)值。

==========

编辑

我做了一些闲逛,并花时间滞后于指数最近的第二收窄的问题,我可以看到当两行落入同一秒时创建的'重复'索引。我的猜测是,这些是违规的行...

2016-03-02 04:03:29.693 8.250347 
2016-03-02 04:03:29.693 7.478983 
2016-03-06 00:19:30.183 45.97248 
2016-03-06 00:19:30.183 24.06088 
2016-03-14 02:44:58.783 9.169300 
2016-03-14 02:44:58.783 4.221998 
2016-03-18 21:54:20.097 73.80586 
2016-03-24 16:41:19.825 3.608202 
2016-03-24 16:41:19.825 3.887996 
2016-03-25 03:35:57.197 4.974968 
2016-03-25 03:35:57.197 5.638140 
2016-04-02 11:18:27.290 7.923712 
2016-04-02 11:18:27.290 6.143240 
2016-04-10 19:59:54.677  3.143636 
2016-04-10 19:59:54.686 14.222390 

什么是最好的方式来删除值?假设我想编写一个自定义方法,将给定索引值的所有重复值发送给我,并发回应该用于该索引值的单个值。我怎样才能做到这一点?

+0

这意味着你已经在你的数据帧复制指标。要看到它:'df [df.index.duplicated()]'也看看 http://stackoverflow.com/questions/27236275/what-does-valueerror-cannot-reindex-from-a-复制轴平均 和 http://stackoverflow.com/questions/27711623/valueerror-cannot-reindex-from-a-duplicate-axis –

+0

还,每当发布,其最好的,包括样本数据,使得您的问题“[Minimum,Complete,and Reproducible](http://stackoverflow.com/help/mcve)” –

+0

@michael_j_ward感谢您的建议。我不知道那个方法调用。知道未来很方便。不幸的是,它不能帮助我,因为原始指标不会重复。当我施加频率时,它们只会被复制。我现在添加更多数据。 – helloB

回答

0

尝试这样的事情,但既然你没有包含任何数据,这只是一个起动器。

for d in data_types: 
     rawDf  = df[df['datatype'] == d] 
     data_series = rawDf[['measurementvalue','displaydate']] 
     data_series.set_index('displaydate',drop=False, inplace = True) 
     data_series.drop_duplicates(inplace = True) 
     data_series.asfreq('30Min', method = 'ffill') 
     all_series.append(data_series) 
+1

感谢您的建议,但仍然产生错误。我想我现在知道错误的来源,尽管我不知道修正。我现在正在编辑我的问题以包含有关数据的更多信息。 – helloB

+0

正确,那么如何编写逻辑删除值以及如果我想结合它们的值呢? – helloB

+0

让我们说我想保持最大。 – helloB

0

如果您想保留每个日期时间的最大值。首先让DATE_TIME列,并使用

df.groupby('date_time').max() 

如果你想始终保持第一或最后一个条目,look at this answer