重采样数据帧的2D插值

时间:2018-07-19 01:29:29

标签: python-3.x pandas interpolation

我想在对数据帧重新采样后对其进行2D插值。重采样部分可以按天,月或分钟。下面的示例仅用于说明该过程。与其从数据框中逐列删除数据,不如在数据帧本身中执行重采样和内插。

示例:

import pandas as pd
import scipy.interpolate

df = pd.DataFrame.from_dict({'time': {0: pd.Timestamp('2015-01-01 00:30:00'),
  1: pd.Timestamp('2015-01-01 00:30:00'),
  2: pd.Timestamp('2015-01-01 00:30:00'),
  3: pd.Timestamp('2015-01-01 00:30:00'),
  4: pd.Timestamp('2015-02-01 00:30:00'),
  5: pd.Timestamp('2015-02-01 00:30:00'),
  6: pd.Timestamp('2015-02-01 00:30:00'),
  7: pd.Timestamp('2015-02-01 00:30:00')},
 'x': {0: 25.0,
  1: 25.0,
  2: 25.5,
  3: 25.5,
  4: 25.0,
  5: 25.0,
  6: 25.5,
  7: 25.5},
 'y': {0: 51.25,
  1: 51.875,
  2: 51.25,
  3: 51.875,
  4: 51.25,
  5: 51.875,
  6: 51.25,
  7: 51.875},
 'z': {0: 1.9466316037407283e-13,
  1: 1.4071156620529962e-13,
  2: 2.1328328914192035e-13,
  3: 1.5983803094022503e-13,
  4: 1.8673746631035076e-13,
  5: 1.1844342238769012e-13,
  6: 2.0592551372867335e-13,
  7: 1.341905238186683e-13}})
df.set_index("time", inplace=True)

然后插值:

interp_x = df["x"].values
interp_y = df["y"].values
interp_z = df["z"].values
interp_f = scipy.interpolate.interp2d(x=interp_x, y=interp_y, z=interp_z)

最后,以下

df.resample("1M").apply(lambda r: interp_f(r["x"], r["y"]))

无法正常工作,因为我得到了

time
2015-01-31    [[1.9070031334221175e-13, 1.9070031334221175e-...
2015-02-28    [[1.9070031334221175e-13, 1.9070031334221175e-...
Freq: M, dtype: object

0 个答案:

没有答案