假設我有一個溫度傳感器資料的時間序列資料幀,其間隔為 30 分鐘。我如何基本上將每個 30 分鐘的間隔細分為更小的 5 分鐘間隔,同時考慮到每個間隔之間的溫降差異?
我想象做這樣的事情可以作業:
30 分鐘間隔:
interval 1: temp = 30 interval 2: temp = 255分鐘間隔:
interval 1: temp = 30 interval 2: temp = 29 interval 3: temp = 28 interval 4: temp = 27 interval 5: temp = 26 interval 6: temp = 25
uj5u.com熱心網友回復:
我會將資料幀重新采樣到較低的時間解析度(在這種情況下為“6T”,T 表示分鐘),這將為缺失的時間步長創建新行,其中包含 nan 值,然后您可以以某種方式填充這些 nan,對于你所描述的,我認為線性插值就足夠了。
這里有一個簡單的例子,我認為它可以與您描述的資料相匹配。
import pandas as pd
df = pd.DataFrame({"temp":[30, 25, 20, 18]}, index = pd.date_range("2021-12-01 12:00:00", "2021-12-01 13:59:00", freq = "30T"))
#This resample will preserve your values at their original time indexes, and will create new rows for the intermediate
#datetime full of nans
#the .last() is just used to select the value for each time-step, you could also use mean o max o min or mean as there is just one value for each time step so it would get you the same.
df = df.resample("6T").last()
#it really depends on how you want to implement the change over time of the data, but as you described a linear
#variation, what you can use is a simple linear interpolation between values with the method interpolate
df.interpolate()
uj5u.com熱心網友回復:
如果您可以包含以下兩件事的示例,將會很有幫助:
- 您的資料目前是什么樣的?
- 完成后你也希望它看起來像什么?
否則不清楚你在問什么或問題是什么,所以很難提供幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/384377.html
