我希望将连续的NaN
值合并成切片。是否有一种使用numpy或pandas简单的方法来实现这一点?
l = [
(996, np.nan), (997, np.nan), (998, np.nan),
(999, -47.3), (1000, -72.5), (1100, -97.7),
(1200, np.nan), (1201, np.nan), (1205, -97.8),
(1300, np.nan), (1302, np.nan), (1305, -97.9),
(1400, np.nan), (1405, -97.10), (1408, np.nan)
]
l = pd.Series(dict(l))
期望结果:
[
(slice(996, 999, None), array([nan, nan, nan])),
(999, -47.3),
(1000, -72.5),
(1100, -97.7),
(slice(1200, 1202, None), array([nan, nan])),
(1205, -97.8),
(slice(1300, 1301, None), array([nan])),
(slice(1302, 1303, None), array([nan])),
(1305, -97.9),
(slice(1400, 1401, None), array([nan])),
(1405, -97.1),
(slice(1408, 1409, None), array([nan]))
]
一个有两个维度的numpy数组也可以,而不是一组元组的列表。
更新于2019/05/31:我刚刚意识到,如果我使用字典代替Pandas系列,算法会更加高效。