[英]Join a time-series dataframe with an "interval" dataframe
我正在努力將區間 dataframe 的數據加入到時間序列 dataframe 中。 對於我的時間序列的每一行,我想查看它包含在哪個區間中,並從區間 dataframe 返回一個特定值。
我受到了這個解決方案的啟發: 如何加入兩個數據框,其列值在一定范圍內?
但據我所知,由於過於復雜的原因,它不起作用。
這是我的錯誤信息:
KeyError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13072/1034504056.py in <module>
1 #df_test.index = pd.IntervalIndex.from_arrays(df_test['Start'],df_test['End'],closed='both')
----> 2 data_test['Product'] = data_test.index.to_series().apply(lambda x : df_test.iloc[df_test.index.get_loc(x)]['Product'])
~\Anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwargs)
4355 dtype: float64
4356 """
-> 4357 return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
4358
4359 def _reduce(
~\Anaconda3\lib\site-packages\pandas\core\apply.py in apply(self)
1041 return self.apply_str()
1042
-> 1043 return self.apply_standard()
1044
1045 def agg(self):
~\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
1097 # List[Union[Callable[..., Any], str]]]]]"; expected
1098 # "Callable[[Any], Any]"
-> 1099 mapped = lib.map_infer(
1100 values,
1101 f, # type: ignore[arg-type]
~\Anaconda3\lib\site-packages\pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()
~\AppData\Local\Temp/ipykernel_13072/1034504056.py in <lambda>(x)
1 #df_test.index = pd.IntervalIndex.from_arrays(df_test['Heure début réelle'],df_test['Hre fin réelle'],closed='both')
----> 2 data_test['Designation'] = data_test.index.to_series().apply(lambda x : df_test.iloc[df_test.index.get_loc(x)]['Désignation article'])
~\Anaconda3\lib\site-packages\pandas\core\indexes\interval.py in get_loc(self, key, method, tolerance)
631 matches = mask.sum()
632 if matches == 0:
--> 633 raise KeyError(key)
634 elif matches == 1:
635 return mask.argmax()
KeyError: Timestamp('2021-10-23 23:59:29')
function 我想成功。
df_test.index = pd.IntervalIndex.from_arrays(df_test['Start'],df_test['End'],closed='both')
data_test['Product'] = data_test.index.to_series().apply(lambda x : df_test.iloc[df_test.index.get_loc(x)]['Product'])
data_test 的樣本值
{'Ordre': {92: 3149484,
93: 3149484,
94: 3149484,
95: 3149610,
96: 3149610,
97: 3149610,
98: 3149610,
99: 3149610,
100: 3149610,
101: 3149610,
102: 3149611},
'Start': {92: Timestamp('2021-10-26 06:55:00'),
93: Timestamp('2021-10-26 06:55:00'),
94: Timestamp('2021-10-26 06:55:00'),
95: Timestamp('2021-10-26 07:25:00'),
96: Timestamp('2021-10-26 07:25:00'),
97: Timestamp('2021-10-26 07:25:00'),
98: Timestamp('2021-10-26 08:30:00'),
99: Timestamp('2021-10-26 08:30:00'),
100: Timestamp('2021-10-26 08:30:00'),
101: Timestamp('2021-10-26 08:30:00'),
102: Timestamp('2021-10-26 11:37:00')},
'End': {92: Timestamp('2021-10-26 07:25:00'),
93: Timestamp('2021-10-26 07:25:00'),
94: Timestamp('2021-10-26 07:25:00'),
95: Timestamp('2021-10-26 08:30:00'),
96: Timestamp('2021-10-26 08:30:00'),
97: Timestamp('2021-10-26 08:30:00'),
98: Timestamp('2021-10-26 11:37:00'),
99: Timestamp('2021-10-26 11:37:00'),
100: Timestamp('2021-10-26 11:37:00'),
101: Timestamp('2021-10-26 11:37:00'),
102: Timestamp('2021-10-26 12:30:00')},
'Product': {92: 'Product_1',
93: 'Product_1',
94: 'Product_1',
95: 'Product_2',
96: 'Product_2',
97: 'Product_2',
98: 'Product_2',
99: 'Product_2',
100: 'Product_2',
101: 'Product_2',
102: 'Product_2'}}
df_test 的樣本值
{'Temperature_1': {Timestamp('2021-10-26 06:55:29'): 62.9905242919922,
Timestamp('2021-10-26 06:56:29'): 62.9905242919922,
Timestamp('2021-10-26 06:57:29'): 62.9905242919922,
Timestamp('2021-10-26 06:58:29'): 62.9905242919922,
Timestamp('2021-10-26 06:59:29'): 62.9905242919922,
Timestamp('2021-10-26 08:25:29'): 65.0611953735352,
Timestamp('2021-10-26 08:26:29'): 65.0611953735352,
Timestamp('2021-10-26 08:27:29'): 65.0611953735352,
Timestamp('2021-10-26 08:28:29'): 65.0611953735352,
Timestamp('2021-10-26 08:29:29'): 65.0611953735352},
'Temperature_2': {Timestamp('2021-10-26 06:55:29'): 66.8290863037109,
Timestamp('2021-10-26 06:56:29'): 66.8290863037109,
Timestamp('2021-10-26 06:57:29'): 66.8290863037109,
Timestamp('2021-10-26 06:58:29'): 66.8290863037109,
Timestamp('2021-10-26 06:59:29'): 66.8290863037109,
Timestamp('2021-10-26 08:25:29'): 67.0449523925781,
Timestamp('2021-10-26 08:26:29'): 67.0449523925781,
Timestamp('2021-10-26 08:27:29'): 67.0449523925781,
Timestamp('2021-10-26 08:28:29'): 66.0404281616211,
Timestamp('2021-10-26 08:29:29'): 66.0404281616211}}
output 將是一個新列,指示哪個產品與時間間隔中是否包含時間戳有關:
{'Temperature_1': {Timestamp('2021-10-26 06:55:29'): 62.9905242919922,
Timestamp('2021-10-26 06:56:29'): 62.9905242919922,
Timestamp('2021-10-26 06:57:29'): 62.9905242919922,
Timestamp('2021-10-26 06:58:29'): 62.9905242919922,
Timestamp('2021-10-26 06:59:29'): 62.9905242919922,
Timestamp('2021-10-26 08:25:29'): 65.0611953735352,
Timestamp('2021-10-26 08:26:29'): 65.0611953735352,
Timestamp('2021-10-26 08:27:29'): 65.0611953735352,
Timestamp('2021-10-26 08:28:29'): 65.0611953735352,
Timestamp('2021-10-26 08:29:29'): 65.0611953735352},
'Temperature_2': {Timestamp('2021-10-26 06:55:29'): 66.8290863037109,
Timestamp('2021-10-26 06:56:29'): 66.8290863037109,
Timestamp('2021-10-26 06:57:29'): 66.8290863037109,
Timestamp('2021-10-26 06:58:29'): 66.8290863037109,
Timestamp('2021-10-26 06:59:29'): 66.8290863037109,
Timestamp('2021-10-26 08:25:29'): 67.0449523925781,
Timestamp('2021-10-26 08:26:29'): 67.0449523925781,
Timestamp('2021-10-26 08:27:29'): 67.0449523925781,
Timestamp('2021-10-26 08:28:29'): 66.0404281616211,
Timestamp('2021-10-26 08:29:29'): 66.0404281616211},
'Product': {Timestamp('2021-10-26 06:55:29'): 'Product_1',
Timestamp('2021-10-26 06:56:29'): 'Product_1',
Timestamp('2021-10-26 06:57:29'): 'Product_1',
Timestamp('2021-10-26 06:58:29'): 'Product_1',
Timestamp('2021-10-26 06:59:29'): 'Product_1',
Timestamp('2021-10-26 08:25:29'): 'Product_2',
Timestamp('2021-10-26 08:26:29'): 'Product_2',
Timestamp('2021-10-26 08:27:29'): 'Product_2',
Timestamp('2021-10-26 08:28:29'): 'Product_2',
Timestamp('2021-10-26 08:29:29'): 'Product_2'}}
感謝您的幫助和建議
間隔應該在 data_test 上創建,而不是 df_test。 此外,您的 data_test 有重復項:
data_test = data_test.drop_duplicates()
data_test.index = pd.IntervalIndex.from_arrays(data_test['Start'],
data_test['End'],
closed='both')
product = (df_test
.index
.to_series()
.apply(lambda df: data_test.iloc[data_test.index.get_loc(df),
data_test.columns.get_loc('Product')])
)
df_test.assign(Product = product)
Temperature_1 Temperature_2 Product
2021-10-26 06:55:29 62.990524 66.829086 Product_1
2021-10-26 06:56:29 62.990524 66.829086 Product_1
2021-10-26 06:57:29 62.990524 66.829086 Product_1
2021-10-26 06:58:29 62.990524 66.829086 Product_1
2021-10-26 06:59:29 62.990524 66.829086 Product_1
2021-10-26 08:25:29 65.061195 67.044952 Product_2
2021-10-26 08:26:29 65.061195 67.044952 Product_2
2021-10-26 08:27:29 65.061195 67.044952 Product_2
2021-10-26 08:28:29 65.061195 66.040428 Product_2
2021-10-26 08:29:29 65.061195 66.040428 Product_2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.