理解pct_change()&shift() in Python

  • Overview

    理解offset in Python

  • pct_change(periods=1, fill_method='pad', limit=None, freq=None, **kwargs)

    Percentage change between the current and a prior element.

    fill_method mean how to handle NAs before computing percent changes;

    limit also about the NA, mean the number of consecutive NAs to fill before stopping;

  • shift(periods=1, freq=None, axis=0, fill_value=<object object>)

    Shift index by desired number of periods with an optional time freq.

    When freq is not passed, shift the index without realigning the data.

    If freq is passed (in this case, the index must be date or datetime, or it will raise a NotImplementedError), the index will be increased using the periods and the freq.

    fill_value is the scalar value to use newly introduced missing values. The default depends on the dtype of self(np.nan for numeric data, NaT for datetime, timedelta, period data)

    shift() 是针对index进行操作

  • How to use freq in shift()?

    shift() shifting values with periods, and shifting time-series data with freq.

    df.shift(freq='10D')
    # is equal to 
    df.shift(period=10, freq='D')
    

    在这里插入图片描述

  • Summary

    针对shift(),不要用数据上下移动的思考角度,而是将shift()参数period或freq用于index相加。

    shift()是针对index的值进行增减操作的函数,index分为valuetime dtype,才会有periodfreq的区别,freq的使用要求indexdtype必须为time类型

  • References

  1. All the Pandas shift() you should know for data analysis
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值