masdeseiscaracteres
1/13/2017 - 3:16 PM

Pandas tips & tricks

Pandas tips & tricks

Grouping functions

  • groupby(func) returns a GroupBy object func is called on each element of the object index but first on the whole index to check the returned type:
    • If an output the same length as the index is returned, the output is used for the grouping.
    • If a scalar is returned, the function is called on each element of the index afterwards.

Groupy class methods (see Appendix for the definition of functions types)

  • groupby.agg(reducing function)

  • groupby.transform(transforming function)

  • groupby.apply(potentially-size-changing function)

  • resample()

  • rolling()

  • expanding()

Related objects

  • pandas.Grouper
  • GroupBy object

Appendix

Types of functions:

  • potentially-size-changing function: changes either ndim or the size tuple (or none)
  • reducing function: collapses one dimension, thus reducing ndim by one
  • transforming function: changes neither ndim nor the size tuple

References

https://pandas.pydata.org/pandas-docs/stable/groupby.html

L = []
from copy import deepcopy
def foo(*args, **kwargs):
    L.append({'args': deepcopy([*args]), 'kwargs': deepcopy({**kwargs})})
    return None
Frequencies
-----------
pd.tseries.frequencies

Offset
------
pd.tseries.offsets


Complex patterns
----------------
http://stackoverflow.com/questions/39967460/using-pandas-to-create-dateoffset-of-paydays