Pandas Rolling Window - datetime64[ns] are not implemented

后端 未结 1 1839
陌清茗
陌清茗 2021-02-07 18:50

I\'m attempting to use Python/Pandas to build some charts. I have data that is sampled every second. Here is a sample:

Index, Time, Value

31362, 1975-05-07 07         


        
相关标签:
1条回答
  • 2021-02-07 19:11

    Setup

    from StringIO import StringIO
    import pandas as pd
    
    text = """Index,Time,Value
    31362,1975-05-07 07:59:18,36.151612
    31363,1975-05-07 07:59:19,36.181368
    31364,1975-05-07 07:59:20,36.197195
    31365,1975-05-07 07:59:21,36.151413
    31366,1975-05-07 07:59:22,36.138009
    31367,1975-05-07 07:59:23,36.142962
    31368,1975-05-07 07:59:24,36.122680"""
    
    df = pd.read_csv(StringIO(text), index_col=0, parse_dates=[1])
    
    df.rolling(2).mean()
    
    NotImplementedError: ops for Rolling for this dtype datetime64[ns] are not implemented
    

    First off, this is confirmation of @BrenBarn's comment and he should get the credit if he decides to post an answer. BrenBarn, if you decide to answer, I'll delete this post.

    Explanation

    Pandas has no idea what a rolling mean of date values ought to be. df.rolling(2).mean() is attempting to roll and average over both the Time and Value columns. The error is politely (or impolitely, depending on your perspective) telling you that you're trying something non-sensical.

    Solution

    Move the Time column to the index and then... well that's it.

    df.set_index('Time').rolling(2).mean()
    

    0 讨论(0)
提交回复
热议问题