user9505772
user9505772

Reputation:

Interpolating datetime Index

I have a DataFrame (df) as follow where 'date' is a datetime index (Y-M-D):

df :
                        values
date
2010-01-01        10
2010-01-02        20
2010-01-03      - 30

I want to create a new df with interpolated datetime index as follow:

                                    values
date
2010-01-01 12:00:00     10
2010-01-01 17:00:00     15    # mean value betw. 2010-01-01 and 2010-01-02
2010-01-02 12:00:00     20
2010-01-02 17:00:00     - 5    # mean value betw. 2010-01-02 and 2010-01-03
2010-01-03 12:00:00    -30

Can anyone help me on this?

Upvotes: 1

Views: 536

Answers (1)

jezrael
jezrael

Reputation: 863731

I believe need add 12 hours to index first, then reindex by union new indices with 17 and last interpolate:

df1 = df.set_index(df.index + pd.Timedelta(12, unit='h'))
idx = (df.index + pd.Timedelta(17, unit='h')).union(df1.index)

df2 = df1.reindex(idx).interpolate()
print (df2)
                     values
date                       
2010-01-01 12:00:00    10.0
2010-01-01 17:00:00    15.0
2010-01-02 12:00:00    20.0
2010-01-02 17:00:00    -5.0
2010-01-03 12:00:00   -30.0
2010-01-03 17:00:00   -30.0

Upvotes: 1

Related Questions