首页 > 解决方案 > How to resample a pandas data frame every half second with data every second?

问题描述

I have a pandas DataFrame like this with time in the index and column A:

                         A
 2018-01-24 00:06:00    3.0
 2018-01-24 00:06:01    4.0
 2018-01-24 00:06:02    8.0
 2018-01-24 00:06:03    4.0
 2018-01-24 00:06:04    2.0

I want the data frame to have half second intervals where the half seconds are the averages between the two data points such that I get something like this:

                             A
 2018-01-24 00:06:00        3.0
 2018-01-24 00:06:00.500    3.5
 2018-01-24 00:06:01        4.0
 2018-01-24 00:06:01.500    6.0
 2018-01-24 00:06:02        8.0
 2018-01-24 00:06:02.500    6.0
 2018-01-24 00:06:03        4.0
 2018-01-24 00:06:03.500    3.0
 2018-01-24 00:06:04        2.0

标签: pythonpython-3.xpandas

解决方案


让我们试试resample interpolate

df=df.resample('500ms').interpolate()
df
                           A
2018-01-24 00:06:00.000  3.0
2018-01-24 00:06:00.500  3.5
2018-01-24 00:06:01.000  4.0
2018-01-24 00:06:01.500  6.0
2018-01-24 00:06:02.000  8.0
2018-01-24 00:06:02.500  6.0
2018-01-24 00:06:03.000  4.0
2018-01-24 00:06:03.500  3.0
2018-01-24 00:06:04.000  2.0

推荐阅读