What's the equivalent of cut/qcut for pandas date fields?

后端 未结 4 1769
Happy的楠姐
Happy的楠姐 2020-12-31 20:13

Update: starting with version 0.20.0, pandas cut/qcut DOES handle date fields. See What\'s New for more.

pd.cut and pd.qcut now sup

4条回答
  •  执念已碎
    2020-12-31 20:52

    Just need to set the index of the field you'd like to resample by, here's some examples

    In [36]: df.set_index('recd').resample('1M',how='sum')
    Out[36]: 
                     price  qty
    recd                       
    2012-07-31   64.151194    9
    2012-08-31   93.476665    7
    2012-09-30   94.193027    7
    2012-10-31         NaN  NaN
    2012-11-30         NaN  NaN
    2012-12-31   12.353405    6
    2013-01-31         NaN  NaN
    2013-02-28  129.586697    7
    2013-03-31         NaN  NaN
    2013-04-30         NaN  NaN
    2013-05-31  211.979583   13
    
    In [37]: df.set_index('recd').resample('1M',how='count')
    Out[37]: 
    2012-07-31  price    1
                qty      1
                ship     1
    2012-08-31  price    1
                qty      1
                ship     1
    2012-09-30  price    2
                qty      2
                ship     2
    2012-10-31  price    0
                qty      0
                ship     0
    2012-11-30  price    0
                qty      0
                ship     0
    2012-12-31  price    1
                qty      1
                ship     1
    2013-01-31  price    0
                qty      0
                ship     0
    2013-02-28  price    2
                qty      2
                ship     2
    2013-03-31  price    0
                qty      0
                ship     0
    2013-04-30  price    0
                qty      0
                ship     0
    2013-05-31  price    3
                qty      3
                ship     3
    dtype: int64
    

提交回复
热议问题