Search Missing Timestamp and display in python?

后端 未结 2 1482
醉话见心
醉话见心 2021-01-20 15:56

Here is my some Dataset which having Time,Temperature1,Temperature2

Timestamp.             Temperature1.        Temperature2
09/01/2016 00:00:08          53.         


        
2条回答
  •  执念已碎
    2021-01-20 16:10

    Assuming that the timestamps have been converted to datetime, if you set the index to the timestamp column and then reindex with a date range then the missing values will show up:

    In [94]:
    df['Timestamp'] = pd.to_datetime(df['Timestamp'])
    df = df.set_index('Timestamp')
    df
    
    Out[94]:
                         Temperature1  Temperature2
    Timestamp                                      
    2016-09-01 00:00:08          53.4          45.5
    2016-09-01 00:00:38          53.5          45.2
    2016-09-01 00:01:08          54.6          43.2
    2016-09-01 00:01:38          55.2          46.3
    2016-09-01 00:02:08          54.5          45.5
    2016-09-01 00:04:08          54.2          35.5
    2016-09-01 00:05:08          52.4          45.7
    2016-09-01 00:05:38          53.4          45.2
    
    In [96]:    
    df.reindex(pd.date_range(start=df.index[0], end=df.index[-1], freq='30s'))
    
    Out[96]:
                         Temperature1  Temperature2
    2016-09-01 00:00:08          53.4          45.5
    2016-09-01 00:00:38          53.5          45.2
    2016-09-01 00:01:08          54.6          43.2
    2016-09-01 00:01:38          55.2          46.3
    2016-09-01 00:02:08          54.5          45.5
    2016-09-01 00:02:38           NaN           NaN
    2016-09-01 00:03:08           NaN           NaN
    2016-09-01 00:03:38           NaN           NaN
    2016-09-01 00:04:08          54.2          35.5
    2016-09-01 00:04:38           NaN           NaN
    2016-09-01 00:05:08          52.4          45.7
    2016-09-01 00:05:38          53.4          45.2
    

    This assumes that the timestamps are regular, here we construct a date range using the timestamp first and last values with a frequency of 30 seconds:

    In [97]:
    pd.date_range(start=df.index[0], end=df.index[-1], freq='30s')
    
    Out[97]:
    DatetimeIndex(['2016-09-01 00:00:08', '2016-09-01 00:00:38',
                   '2016-09-01 00:01:08', '2016-09-01 00:01:38',
                   '2016-09-01 00:02:08', '2016-09-01 00:02:38',
                   '2016-09-01 00:03:08', '2016-09-01 00:03:38',
                   '2016-09-01 00:04:08', '2016-09-01 00:04:38',
                   '2016-09-01 00:05:08', '2016-09-01 00:05:38'],
                  dtype='datetime64[ns]', freq='30S')
    

    When you reindex with this, any missing index labels become NaN values

提交回复
热议问题