【问题标题】:Pandas Interpolate 'time' vs 'linear'熊猫插值“时间”与“线性”
【发布时间】:2019-07-25 22:07:57
【问题描述】:

Pandas interpolate function中,当时间索引等间距时,method='time'是否等同于method='linear'

一个基本的例子表明情况就是这样:

even_index = pd.date_range('2019-02-20 10:00 am', 
                           '2019-02-20 2:00 pm', freq='1 h')
values = [10, np.nan, 30, np.nan, 50]

pd.DataFrame(values, index=even_index).interpolate(method='time')

                        0
2019-02-20 10:00:00  10.0
2019-02-20 11:00:00  20.0
2019-02-20 12:00:00  30.0
2019-02-20 13:00:00  40.0
2019-02-20 14:00:00  50.0


pd.DataFrame(values, index=even_index).interpolate(method='linear')

                        0
2019-02-20 10:00:00  10.0
2019-02-20 11:00:00  20.0
2019-02-20 12:00:00  30.0
2019-02-20 13:00:00  40.0
2019-02-20 14:00:00  50.0

“时间”和“线性”之间的差异似乎仅在时间索引不等间距时才会出现:

uneven_index = pd.to_datetime(['2019-02-20 10:00 am', 
               '2019-02-20 10:30 am', '2019-02-20 12:30 pm', 
               '2019-02-20 1:30 pm', '2019-02-20 2:00 pm'])


pd.DataFrame(values, index=uneven_index).interpolate(method='time')

                             0
2019-02-20 10:00:00  10.000000
2019-02-20 10:30:00  14.000000
2019-02-20 12:30:00  30.000000
2019-02-20 13:30:00  43.333333
2019-02-20 14:00:00  50.000000

pd.DataFrame(values, index=uneven_index).interpolate(method='linear')

                        0
2019-02-20 10:00:00  10.0
2019-02-20 10:30:00  20.0
2019-02-20 12:30:00  30.0
2019-02-20 13:30:00  40.0
2019-02-20 14:00:00  50.0

我的问题是这是否总是成立。是否可以假设在等间隔的时间索引下,method='time' 总是会进行线性插值?

【问题讨论】:

    标签: python pandas dataframe interpolation


    【解决方案1】:

    是的

    来自文档:

    ‘linear’:忽略索引,将值视为等距

    因此,如果您的索引是等间距的并且您使用了正确的方法(在您的示例中,时间索引的“时间”方法,但它也可以是具有等间距值的数字索引的“索引”方法),你确实会得到同样的结果。

    【讨论】:

    • 谢谢,我已经在文档中阅读了这一点,这就是我的示例所显示的。如果time 等间隔的间隔完全等同于linear,我只是想与了解库内部的人确认。这并不能肯定地回答这个问题。
    猜你喜欢
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 2019-05-02
    • 2016-08-31
    相关资源
    最近更新 更多