使用 groupby 和 resample 对 Pandas 进行上采样 [英] Pandas upsampling using groupby and resample

查看:115
本文介绍了使用 groupby 和 resample 对 Pandas 进行上采样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将时间序列与间隙分组.我不想填补空白,尊重分组.

I have grouped timeseries with gaps. I wan't to fill the gaps, respecting the groupings.

date 在每个 id 中都是唯一的.

date is unique within each id.

以下有效,但在我不需要 NaN 的地方给了我零

The following works but gives me zero's where I wan't NaN's

data.groupby('id').resample('D', on='date').sum()\
    .drop('id', axis=1).reset_index()

以下由于某种原因不起作用

The following do not work for some reason

data.groupby('id').resample('D', on='date').asfreq()\
    .drop('id', axis=1).reset_index()

data.groupby('id').resample('D', on='date').fillna('pad')\
    .drop('id', axis=1).reset_index()

我收到以下错误:不支持从 level= 或 on= 选择上采样,使用 .set_index(...) 将索引显式设置为类似日期时间

我尝试将 pandas.Grouperset_index 多级索引或单级索引一起使用,但它似乎没有对我的日期列进行上采样,所以我得到了连续的日期或它不尊重 id 列.

I've tried to use the pandas.Grouper with set_index multilevel index or single but it do not seems to upsample my date column so i get continous dates or it do not respect the id column.

Pandas 是 0.23 版

Pandas is version 0.23

自己试试:

data = pd.DataFrame({
'id': [1,1,1,2,2,2],
'date': [
    datetime(2018, 1, 1),
    datetime(2018, 1, 5),
    datetime(2018, 1, 10),
    datetime(2018, 1, 1),
    datetime(2018, 1, 5),
    datetime(2018, 1, 10)],
'value': [100, 110, 90, 50, 40, 60]})

# Works but gives zeros
data.groupby('id').resample('D', on='date').sum()
# Fails
data.groupby('id').resample('D', on='date').asfreq()
data.groupby('id').resample('D', on='date').fillna('pad')

推荐答案

创建 DatetimeIndex 并从 resample 中删除参数 on:

Create DatetimeIndex and remove parameter on from resample:

print (data.set_index('date').groupby('id').resample('D').asfreq())
                id
id date           
1  2018-01-01  1.0
   2018-01-02  NaN
   2018-01-03  NaN
   2018-01-04  NaN
   2018-01-05  1.0
   2018-01-06  NaN
   2018-01-07  NaN
   2018-01-08  NaN
   2018-01-09  NaN
   2018-01-10  1.0
2  2018-01-01  2.0
   2018-01-02  NaN
   2018-01-03  NaN
   2018-01-04  NaN
   2018-01-05  2.0
   2018-01-06  NaN
   2018-01-07  NaN
   2018-01-08  NaN
   2018-01-09  NaN
   2018-01-10  2.0

<小时>

print (data.set_index('date').groupby('id').resample('D').fillna('pad'))
#alternatives
#print (data.set_index('date').groupby('id').resample('D').ffill())
#print (data.set_index('date').groupby('id').resample('D').pad())
               id
id date          
1  2018-01-01   1
   2018-01-02   1
   2018-01-03   1
   2018-01-04   1
   2018-01-05   1
   2018-01-06   1
   2018-01-07   1
   2018-01-08   1
   2018-01-09   1
   2018-01-10   1
2  2018-01-01   2
   2018-01-02   2
   2018-01-03   2
   2018-01-04   2
   2018-01-05   2
   2018-01-06   2
   2018-01-07   2
   2018-01-08   2
   2018-01-09   2
   2018-01-10   2

如果要使用 sum 缺失值需要 min_count=1 参数 - sum:

If want use sum with missing values need min_count=1 parameter - sum:

min_count :整数,默认 0执行操作所需的有效值数.如果存在少于 min_count 的非 NA 值,则结果将为 NA.

min_count : int, default 0 The required number of valid values to perform the operation. If fewer than min_count non-NA values are present the result will be NA.

0.22.0 新版:新增,默认为 0.这意味着全 NA 或空系列的总和为 0,全 NA 或空系列的乘积为 1.

New in version 0.22.0: Added with the default being 0. This means the sum of an all-NA or empty Series is 0, and the product of an all-NA or empty Series is 1.

print (data.groupby('id').resample('D', on='date').sum(min_count=1))

这篇关于使用 groupby 和 resample 对 Pandas 进行上采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆