How can I do an interpolating reindex in pandas using datetime indices?

后端 未结 1 1016
说谎
说谎 2021-01-19 19:49

I have a series with a datetime index, and what I\'d like is to interpolate this data using some other, arbitrary datetime index. Essentially what I want is how to make the

相关标签:
1条回答
  • 2021-01-19 20:00

    Try this:

    from pandas import Series
    import datetime
    
    datetime_index = [datetime.datetime(2010, 1, 5), datetime.datetime(2010, 1, 10)]
    s1 = Series([5, 15], [datetime.datetime(2010, 1, 5), datetime.datetime(2010, 1, 15)])
    s2 = Series(None, datetime_index)
    s3 = s1.combine_first(s2)
    s3.interpolate()
    

    Based on the comments, the result interpolated to the target index would be:

    goal_series  = s3.interpolate().reindex(datetime_index)
    
    assert((goal_series == Series([5, 10], datetime_index)).all())
    
    0 讨论(0)
提交回复
热议问题