熊猫:合并两个时间序列,并在这两个时间重叠的时间段内获得平均值

发布于 2025-01-25 05:11:15 字数 1722 浏览 2 评论 0原文

我得到了两个pandas dataframes:

ts1
Out[50]: 
                     soil_moisture_ids41  
date_time                                 
2007-01-07 05:00:00               0.1830  
2007-01-07 06:00:00               0.1825  
2007-01-07 07:00:00               0.1825  
2007-01-07 08:00:00               0.1825  
2007-01-07 09:00:00               0.1825  
...                                 ...  
2017-10-10 20:00:00               0.0650  
2017-10-10 21:00:00               0.0650  
2017-10-10 22:00:00               0.0650  
2017-10-10 23:00:00               0.0650  
2017-10-11 00:00:00               0.0650  

[94316 rows x 3 columns]

另一个是

ts2
Out[51]: 
                     soil_moisture_ids42  
date_time                                                        
2016-07-20 00:00:00                0.147  
2016-07-20 01:00:00                0.148  
2016-07-20 02:00:00                0.149  
2016-07-20 03:00:00                0.150  
2016-07-20 04:00:00                0.152  
...                                 ...  
2019-12-31 19:00:00                0.216 
2019-12-31 20:00:00                0.216 
2019-12-31 21:00:00                0.215 
2019-12-31 22:00:00                0.215 
2019-12-31 23:00:00                0.215 

[30240 rows x 3 columns]

您可以看到,从2007-01-072016-07-19,只有ts1具有数据点。从2016-07-202017-10-1 1 1有一些重叠的时间序列。现在,我想结合这两个数据帧。在重叠期间,我想通过ts1ts2获得平均值。在非拼写期间(2007-01-07 to 2016-07-192017-10-12 to 2019-12-31),每个时间邮票的值都设置为ts1ts2的值。那我该怎么做呢?

谢谢!

I got two pandas dataframes as following:

ts1
Out[50]: 
                     soil_moisture_ids41  
date_time                                 
2007-01-07 05:00:00               0.1830  
2007-01-07 06:00:00               0.1825  
2007-01-07 07:00:00               0.1825  
2007-01-07 08:00:00               0.1825  
2007-01-07 09:00:00               0.1825  
...                                 ...  
2017-10-10 20:00:00               0.0650  
2017-10-10 21:00:00               0.0650  
2017-10-10 22:00:00               0.0650  
2017-10-10 23:00:00               0.0650  
2017-10-11 00:00:00               0.0650  

[94316 rows x 3 columns]

and the other one is

ts2
Out[51]: 
                     soil_moisture_ids42  
date_time                                                        
2016-07-20 00:00:00                0.147  
2016-07-20 01:00:00                0.148  
2016-07-20 02:00:00                0.149  
2016-07-20 03:00:00                0.150  
2016-07-20 04:00:00                0.152  
...                                 ...  
2019-12-31 19:00:00                0.216 
2019-12-31 20:00:00                0.216 
2019-12-31 21:00:00                0.215 
2019-12-31 22:00:00                0.215 
2019-12-31 23:00:00                0.215 

[30240 rows x 3 columns]

You could see that, from 2007-01-07 to 2016-07-19, only ts1 has the data points. And from 2016-07-20 to 2017-10-11 there are some overlapped time series. Now I want to combine these two data frames. During the overlapped period, I want to get the mean values over ts1 and ts2. During the non-overlapped period, (2007-01-07 to 2016-07-19 and 2017-10-12 to 2019-12-31), the values at each time stamp is set as the value from ts1 or ts2. So how can I do it?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失与倦" 2025-02-01 05:11:15

Use concat with aggregate mean, if only one value get same ouput, if multiple get mean. Also finally DatatimeIndex is sorted:

s = pd.concat([ts1, ts2]).groupby(level=0).mean()
俯瞰星空 2025-02-01 05:11:15

只需先存储串联系列,然后应用平均值即可。即MERGED_TS = PD.CONCAT([[TS1,TS2]),然后mean_ts = merged_ts.group_by(level = 0).mean(Mean()>

Just store the concatenated series first and then apply the mean. i.e. merged_ts = pd.concat([ts1, ts2]) and then mean_ts = merged_ts.group_by(level=0).mean()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文