如何识别熊猫数据框Python中缺少的时间戳?

时间:2020-08-11 22:40:42

标签: pandas dataframe timestamp

如何在下面的数据框中每1分钟丢失一次时间戳?

                                     Latitudes
Timestamps                                 
2015-12-04 12:14:44.327000-05:00  41.805440
2015-12-04 12:14:44.631000-05:00  41.805440
2015-12-04 12:20:31.180000-05:00  41.804460
2015-12-04 12:20:31.375000-05:00  41.804460
2015-12-04 12:21:16.009000-05:00  41.804933
                                    ...
2015-12-18 08:42:05.020000-05:00  41.805483
2015-12-18 08:52:13.703000-05:00  41.805480
2015-12-18 09:13:08.378000-05:00  41.805616
2015-12-18 09:32:49.127000-05:00  41.805329
2015-12-18 09:43:07.421000-05:00  41.805449

我在做

df.set_index('Timestamps', inplace =True)                        
df.reindex(pd.date_range(start=df.index[0], end=df.index[-1], freq='1Min'))

但是它不起作用,为什么?

所需的输出-

          Timestamps                        latitude
0     2015-12-04 12:14:44.327000-05:00       41.80544
1     2015-12-04 12:15:44.327000-05:00       NaN
2     2015-12-04 12:16:44.327000-05:00       NaN
3     2015-12-04 12:17:44.327000-05:00       NaN
4     2015-12-04 12:18:44.327000-05:00       NaN
..................................................
5     2015-12-04 12:20:31.180000-05:00    41.804460
6     2015-12-04 12:21:16.009000-05:00    41.804933     

                     

Blockquote 我还希望在数据帧中缺少时间戳值的纬度NAN值中填写-1。

1 个答案:

答案 0 :(得分:0)

我修复了它。

df = df.set_index('Timestamps').asfreq('1Min')

输出:

                                  Timestamps  latitude
0     2015-12-04 12:14:44.327000-05:00       41.80544
1     2015-12-04 12:15:44.327000-05:00       NaN
2     2015-12-04 12:16:44.327000-05:00       NaN
3     2015-12-04 12:17:44.327000-05:00       NaN
4     2015-12-04 12:18:44.327000-05:00       NaN
                               ...       ...
20004 2015-12-18 09:38:44.327000-05:00       NaN
20005 2015-12-18 09:39:44.327000-05:00       NaN
20006 2015-12-18 09:40:44.327000-05:00       NaN
20007 2015-12-18 09:41:44.327000-05:00       NaN
20008 2015-12-18 09:42:44.327000-05:00       NaN