【问题标题】:how can I resample pandas dataframe by day on period time?如何在期间按天对 pandas 数据帧进行重新采样?
【发布时间】:2020-11-07 19:20:18
【问题描述】:

我有一个这样的数据框:

df.head()
Out[2]: 
         price   sale_date 
0  477,000,000  1396/10/30 
1  608,700,000  1396/10/30 
2  580,000,000  1396/10/03 
3  350,000,000  1396/10/03 
4  328,000,000  1396/03/18

日期时间超出范围
那么我按照下面将它们作为周期时间

df['sale_date']=df['sale_date'].str.replace('/','').astype(int)

def conv(x):
    return pd.Period(year=x // 10000,
                     month=x // 100 % 100,
                     day=x % 100, freq='D')
 
df['sale_date'] = df['sale_date'].str.replace('/','').astype(int).apply(conv)

现在我想按天对它们进行重新采样,如下所示:

df.resample(freq='d', on='sale_date').sum()

但它给了我这个错误:

resample() got an unexpected keyword argument 'freq'

【问题讨论】:

  • pandas.DataFrame.resample 没有参数freq,而是rule。另外,如果您没有任何可用时间,您想如何按天汇总?我的意思是,从逻辑上讲,结果将是它们自己的值(每行)。
  • 感谢您的回答。我编辑我的数据框。现在我有一些具有同一天的行。所以现在重新采样是正确的..@CainãMaxCouto-Silva
  • 它现在给了我这个错误:当前不支持从 level= 或 on= 选择使用 PeriodIndex 重新采样,使用 .set_index(...) 显式设置索引 @CainãMaxCouto-Silva
  • @saeedzali - 试试df.set_index('sale_date').resample('D')['price'].sum(),但对我来说不起作用。
  • @saeedzali,是的,它似乎还没有实现,我们需要set_index。但我现在也试过了,它对我也不起作用。可能使用period[D] 和resample 也没有实现。

标签: python pandas python-datetime


【解决方案1】:

在 pandas 1.1.3 中,resample 和 Grouper 对我来说似乎不起作用(我猜是 bug):

df['sale_date']=df['sale_date'].str.replace('/','').astype(int)
df['price'] = df['price'].str.replace(',','').astype(int)

def conv(x):
    return pd.Period(year=x // 10000,
                     month=x // 100 % 100,
                     day=x % 100, freq='D')
 
df['sale_date'] = df['sale_date'].apply(conv)

# df = df.set_index('sale_date').resample('D')['price'].sum()
#OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1396-03-18 00:00:00

# df = df.set_index('sale_date').groupby(pd.Grouper(freq='D'))['price'].sum()
#OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1396-03-18 00:00:00

可能的解决方案由sum 聚合,因此如果重复sale_date 则将price 值相加:

df = df.groupby('sale_date')['price'].sum().reset_index()
print (df)
    sale_date      price
0  1396-03-18  328000000
1  1396-10-03  580000000
2  1396-10-30  477000000
3  1396-11-25  608700000
4  1396-12-05  350000000

编辑:Series.reindex 和period_range 是可能的:

s = df.groupby('sale_date')['price'].sum()
rng = pd.period_range(s.index.min(), s.index.max(), name='sale_date')
df = s.reindex(rng, fill_value=0).reset_index()
print (df)
      sale_date      price
0    1396-03-18  328000000
1    1396-03-19          0
2    1396-03-20          0
3    1396-03-21          0
4    1396-03-22          0
..          ...        ...
258  1396-12-01          0
259  1396-12-02          0
260  1396-12-03          0
261  1396-12-04          0
262  1396-12-05  350000000

[263 rows x 2 columns]

【讨论】:

  • 很抱歉占用您的时间。如何在我的数据框中填补空白日期时间
  • 是否可以用第二天和前一天的平均值填充值?感谢您的帮助@jezrael
  • @saeedzali - 你认为df = s.reindex(rng, fill_value=0).reset_index()之后的每3行吗?
  • @saeedzali - 我认为最好的应该是新问题。
  • 不,我的数据框中没有零值。当那天是假期时(例如),我有一些空白,我想通过第二天和前一天来填补它们@jezrael
猜你喜欢
  • 1970-01-01
  • 2017-09-19
  • 2013-11-22
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 2013-12-05
  • 2017-12-12
相关资源
最近更新 更多