熊猫在基于多索引的数据帧上重采样和偏移量的使用

2022-12-01 04:11:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个df,它有一个多索引[(纬度、经度、时间)],行数为148x244x90x24。对于每个纬度和经度,UTC格式的时间为每小时从2014-01-01 00:00:002014:03:31 23:00:00

                                                FFDI
latitude    longitude   time    
-39.20000   140.80000   2014-01-01 00:00:00     6.20000
                        2014-01-01 01:00:00     4.10000
                        2014-01-01 02:00:00     2.40000
                        2014-01-01 03:00:00     1.90000
                        2014-01-01 04:00:00     1.70000
                        2014-01-01 05:00:00     1.50000
                        2014-01-01 06:00:00     1.40000
                        2014-01-01 07:00:00     1.30000
                        2014-01-01 08:00:00     1.20000
                        2014-01-01 09:00:00     1.00000
                        2014-01-01 10:00:00     1.00000
                        2014-01-01 11:00:00     0.90000
                        2014-01-01 12:00:00     0.90000
                        ... ... ... ...
                        2014-03-31 21:00:00     0.30000
                        2014-03-31 22:00:00     0.30000
                        2014-03-31 23:00:00     0.50000
            140.83786   2014-01-01 00:00:00     3.20000
                        2014-01-01 01:00:00     2.90000
                        2014-01-01 02:00:00     2.10000
                        2014-01-01 03:00:00     2.90000
                        2014-01-01 04:00:00     1.20000
                        2014-01-01 05:00:00     0.90000
                        2014-01-01 06:00:00     1.10000
                        2014-01-01 07:00:00     1.60000
                        2014-01-01 08:00:00     1.40000
                        2014-01-01 09:00:00     1.50000
                        2014-01-01 10:00:00     1.20000
                        2014-01-01 11:00:00     0.80000
                        2014-01-01 12:00:00     0.40000
                        ... ... ... ...
                        2014-03-31 21:00:00     0.30000
                        2014-03-31 22:00:00     0.30000
                        2014-03-31 23:00:00     0.50000
            ... ... ... ...
... ... ...
-33.90000   140.80000   2014-01-01 00:00:00     6.20000
                        2014-01-01 01:00:00     4.10000
                        2014-01-01 02:00:00     2.40000
                        2014-01-01 03:00:00     1.90000
                        2014-01-01 04:00:00     1.70000
                        2014-01-01 05:00:00     1.50000
                        2014-01-01 06:00:00     1.40000
                        2014-01-01 07:00:00     1.30000
                        2014-01-01 08:00:00     1.20000
                        2014-01-01 09:00:00     1.00000
                        2014-01-01 10:00:00     1.00000
                        2014-01-01 11:00:00     0.90000
                        2014-01-01 12:00:00     0.90000
                        ... ... ... ...
                        2014-03-31 21:00:00     0.30000
                        2014-03-31 22:00:00     0.30000
                        2014-03-31 23:00:00     0.50000
            140.83786   2014-01-01 00:00:00     3.20000
                        2014-01-01 01:00:00     2.90000
                        2014-01-01 02:00:00     2.10000
                        2014-01-01 03:00:00     2.90000
                        2014-01-01 04:00:00     1.20000
                        2014-01-01 05:00:00     0.90000
                        2014-01-01 06:00:00     1.10000
                        2014-01-01 07:00:00     1.60000
                        2014-01-01 08:00:00     1.40000
                        2014-01-01 09:00:00     1.50000
                        2014-01-01 10:00:00     1.20000
                        2014-01-01 11:00:00     0.80000
                        2014-01-01 12:00:00     0.40000
                        ... ... ... ...
                        2014-03-31 21:00:00     0.30000
                        2014-03-31 22:00:00     0.30000
                        2014-03-31 23:00:00     0.50000

78001920 rows × 1 columns

我需要使用前一天13:00:00到当天12:00:00的小时值计算日期的每日最大FFDI值,以适合我的时区(+11)。例如,如果在+11时区计算2014-01-10的每日最大FFDI,我可以使用从2014-01-09 13:00:00到2014-01-10 12:00:00的每小时FFDI

df_daily_max = df .groupby(['latitude', 'longitude', pd.Grouper(freq='24H',base=13,loffset='11H',level='time')])['FFDI'].max().reset_index(name='Max FFDI')

计算从13:00:00开始,频率为24小时

输出为:

    latitude                    longitude                   time                    Max FFDI
0   -39.20000076293945312500    140.80000305175781250000    2013-12-31 13:00:00     6.19999980926513671875
1   -39.20000076293945312500    140.80000305175781250000    2014-01-01 13:00:00     1.50000000000000000000
2   -39.20000076293945312500    140.80000305175781250000    2014-01-02 13:00:00     1.60000002384185791016

我希望输出为:

    latitude                    longitude                   time            Max FFDI
0   -39.20000076293945312500    140.80000305175781250000    2014-01-01      6.19999980926513671875
1   -39.20000076293945312500    140.80000305175781250000    2014-01-02      1.50000000000000000000
2   -39.20000076293945312500    140.80000305175781250000    2014-01-03      1.60000002384185791016