[Python] Pandas interpolate 'time' vs 'linear' for equally-spaced times

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    In Pandas interpolate function, is method='time' equivalent to method='linear' when the time index is equally spaced?

    A basic example suggests this is the case:

    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]
    df_even = pd.DataFrame(values, index=even_index)


    >>> df_even.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

    >>> df_even.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


    A difference between 'time' and 'linear' only seems to occur if the time index is not equally spaced:

    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'])
    df_uneven = pd.DataFrame(values, index=uneven_index)


    >>> df_uneven.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

    >>> df_uneven.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


    My question is whether this always holds. Can it be assumed with an equally spaced time index, method='time' will always perform linear interpolation?

    Continue reading...

Compartilhe esta Página