数据帧中从第一行开始的间隔[python 3.6.0]

2024-08-21Python开发问题
6

本文介绍了数据帧中从第一行开始的间隔[python 3.6.0]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

以下数据的时间间隔为5分钟,正在尝试将其分组为10分钟

数据帧名称为df

script_id DATE_TIME 打开 关闭 音量
201 2019-01-01 10:45:00 1492.9 1493.85 1492.15 1492.9 7189
201 2019-01-01 10:50:00 1492.9 1495.95 1492.2 1495.85 15440
201 2019-01-01 10:55:00 1495.85 1495.95 1494 1494.5 8360
201 2019-01-01 11:00:00 1494.5 1494.5 1492 1492.05 9910
201 2019-01-01 11:05:00 1492.05 1493.9 1492 1493.35 14961
201 2019-01-01 11:10:00 1493.4 1493.4 1488 1489.25 16493
201 2019-01-01 11:15:00 1489.25 1492 1489.25 1490.6 14590
201 2019-01-01 11:20:00 1490.6 1491.65 1490 1491.5 3470

执行以下代码时:

df_f = df.groupby(['script_id', pd.Grouper(key='date_time', freq=f'{tf}T')])
                            .agg(open=pd.NamedAgg(column='open', aggfunc='first'),
                                high=pd.NamedAgg(column='high', aggfunc='max'),
                                low=pd.NamedAgg(column='low', aggfunc='min'),
                                close=pd.NamedAgg(column='close', aggfunc='last'),
                                volume=pd.NamedAgg(column='volume', aggfunc='sum'))
                                .reset_index()
                print(df_f)

结果为(已从此处删除不需要的详细信息):

DATE_TIME
2019-01-01 10:40:00
2019-01-01 10:50:00
2019-01-01 11:00:00
2019-01-01 11:10:00

但应该是(已从此处删除不需要的详细信息):-(预期结果)

DATE_TIME
2019-01-01 10:45:00
2019-01-01 10:55:00
2019-01-01 11:05:00
2019-01-01 11:15:00

推荐答案

在调用pd.Grouper(... offset="5T")

时似乎只需要提供偏移量参数
df_f = df.groupby(['script_id', pd.Grouper(key='date_time', freq='10T', offset="5T")])
                            .agg(open=pd.NamedAgg(column='open', aggfunc='first'),
                                high=pd.NamedAgg(column='high', aggfunc='max'),
                                low=pd.NamedAgg(column='low', aggfunc='min'),
                                close=pd.NamedAgg(column='close', aggfunc='last'),
                                volume=pd.NamedAgg(column='volume', aggfunc='sum'))
                                .reset_index()

print(df_f)
   script_id           date_time     open     high      low    close  volume
0        201 2019-01-01 10:45:00  1492.90  1495.95  1492.15  1495.85   22629
1        201 2019-01-01 10:55:00  1495.85  1495.95  1492.00  1492.05   18270
2        201 2019-01-01 11:05:00  1492.05  1493.90  1488.00  1489.25   31454
3        201 2019-01-01 11:15:00  1489.25  1492.00  1489.25  1491.50   18060

旧版本的pandas.Grouper对象使用base而不是offsetpd.Grouper(..., base=5)

>>> df_f = df.groupby(['script_id', pd.Grouper(key='date_time', freq=f'10T', base=5)])
                            .agg(open=pd.NamedAgg(column='open', aggfunc='first'),
                                high=pd.NamedAgg(column='high', aggfunc='max'),
                                low=pd.NamedAgg(column='low', aggfunc='min'),
                                close=pd.NamedAgg(column='close', aggfunc='last'),
                                volume=pd.NamedAgg(column='volume', aggfunc='sum'))
                                .reset_index()

print(df_f)
   script_id           date_time     open     high      low    close  volume
0        201 2019-01-01 10:45:00  1492.90  1495.95  1492.15  1495.85   22629
1        201 2019-01-01 10:55:00  1495.85  1495.95  1492.00  1492.05   18270
2        201 2019-01-01 11:05:00  1492.05  1493.90  1488.00  1489.25   31454
3        201 2019-01-01 11:15:00  1489.25  1492.00  1489.25  1491.50   18060

这篇关于数据帧中从第一行开始的间隔[python 3.6.0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10