wrappers
A List of Useful Decorators/Wrappers for pandas Modules
The decorators can be used for profiling/understanding the a function which are developed to handle a DataFrame object.
- pandaswizard.wrappers.recordCounter(func: callable) callable
Verbose the Shape of the DataFrame Pre- & Post- Function Execution
On execution of a function (typically, if the function uses the
drop()ormerge()or etc.) the decorator prints the number of records before and after the execution.import pandas as pd import pandaswizard as pdw # attempt to create an ubiquitous naming frame = pd.DataFrame(data = { "LABEL" : ["A", "A", "B"], "VALUES" : [1, 2, 3] }) @pdw.wrappers.recordCounter def dropvals(frame): return frame[frame["LABEL"] != "B"] filtered = dropvals(frame = frame.copy()) >> Executed with @recordCounter[`dropvals`] >> >> Original Record Count = 3 >> >> Final Record Count = 2 >> >> Dropped/Added Records = -1 (= 33.333%)LIMITATION: The decorator works IFF the dataframe is either the first arguments or is passed as an keyword argument with the argument name as either data or frame which is the general convention that is followed throughout the module. If the code has arguments, then it has preference over the keyword arguments and the dataframe must be the first argument.
USE Cases: Often on function execution, joining/dropping records users need to execute df.shape before and after to check the record count. This is simplified and gives the result, and thus reducing code during such operations.
- pandaswizard.wrappers.timeit(func: callable) callable
A Mimic of the iPython Magic
%timeitfor DataframesThe built-in iPython magic function
%timeitdisplays the executed time of a function, or like the one from command line:python -m timeit "function()"
The function is built specifically to handle functions which returns a
pd.DataFrameobject, thus it prints more information like the number of records fetched, shape and other information w/o explictly needing to call additional pandas function by the end-user.