【问题标题】:Pandas drop rows in time series with less than x observation熊猫在时间序列中丢弃少于 x 观察的行
【发布时间】:2020-05-31 11:05:44
【问题描述】:

我正在处理 Pandas 中的时间序列数据(时间戳用作索引)。我正在对我的数据集进行一些过滤,最终得到一个主要包含连续观察(一分钟数据)的数据框。然而,也有只有一分钟或几分钟观察的时间间隔。这些我想排除。我怎样才能使用类似的东西来掌握那些短时间间隔:

df = df.drop(df[<some boolean condition>].index)
timestamp               value     
2018-01-08 06:13:00     143
2018-01-08 06:14:00     324
2018-01-08 06:15:00     324
2018-01-08 06:16:00     324
2018-01-08 06:17:00     324
2018-01-08 06:20:00     324(remove)
2018-01-08 06:35:00     324
2018-01-08 06:36:00     324
2018-01-08 06:37:00     324
2018-01-08 06:38:00     324
2018-01-08 06:39:00     324
2018-01-08 06:40:00     324

【问题讨论】:

    标签: python pandas time-series


    【解决方案1】:

    用途:

    #convert index to Series
    s = df.index.to_series()
    #test if 1 Minute difference, then cumulative sum
    a = s.diff().ne(pd.Timedelta(1, unit='Min')).cumsum()
    
    #filter if counts of cumulative value greater like N, e.g. 3
    N = 3
    df = df[a.map(a.value_counts()).gt(N)]
    print (df)
                         value
    timestamp                 
    2018-01-08 06:13:00    143
    2018-01-08 06:14:00    324
    2018-01-08 06:15:00    324
    2018-01-08 06:16:00    324
    2018-01-08 06:17:00    324
    2018-01-08 06:35:00    324
    2018-01-08 06:36:00    324
    2018-01-08 06:37:00    324
    2018-01-08 06:38:00    324
    2018-01-08 06:39:00    324
    2018-01-08 06:40:00    324
    

    【讨论】:

    猜你喜欢
    • 2013-01-10
    • 2017-11-17
    • 1970-01-01
    • 2018-11-14
    • 2020-01-22
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 2022-11-11
    相关资源
    最近更新 更多