From 8b77070f12cad36fff325d36f1506e880fe5f1be Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Mon, 30 Nov 2015 23:25:05 +0000 Subject: [PATCH] pandas examples --- README.rst | 21 ++++----------------- examples/pandas_progress_apply.py | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 examples/pandas_progress_apply.py diff --git a/README.rst b/README.rst index 05a02c34..285a3519 100644 --- a/README.rst +++ b/README.rst @@ -183,7 +183,7 @@ Returns Examples and Advanced Usage --------------------------- -See the ``examples`` folder. +See the ``examples`` folder or import the module and run ``help()``. Hooks and callbacks ~~~~~~~~~~~~~~~~~~~ @@ -269,22 +269,9 @@ for ``DataFrameGroupBy.progress_apply``: # Now you can use `progress_apply` instead of `apply` df.groupby(0).progress_apply(lambda x: x**2) -In case you're interested in the internals, the ``tqdm_pandas`` function is -defined as follows: - -.. code:: python - - def tqdm_pandas(t): - from pandas.core.groupby import DataFrameGroupBy - def inner(groups, func, *args, **kwargs): - t.total = len(groups) + 1 - def wrapper(*args, **kwargs): - t.update(1) - return func(*args, **kwargs) - result = groups.apply(wrapper, *args, **kwargs) - t.close() - return result - DataFrameGroupBy.progress_apply = inner +In case you're interested in how this works (and how to modify it for your +own callbacks), see the ``examples`` folder or import the midule and run +``help()``. Contributions ------------- diff --git a/examples/pandas_progress_apply.py b/examples/pandas_progress_apply.py new file mode 100644 index 00000000..0658a05a --- /dev/null +++ b/examples/pandas_progress_apply.py @@ -0,0 +1,26 @@ +import pandas as pd +import numpy as np +from tqdm import tqdm, tqdm_pandas + +df = pd.DataFrame(np.random.randint(0, 100, (100000, 6))) + +# Create and register a new `tqdm` instance with `pandas` +# (can use tqdm_gui, optional kwargs, etc.) +tqdm_pandas(tqdm()) + +# Now you can use `progress_apply` instead of `apply` +df.groupby(0).progress_apply(lambda x: x**2) + + +""" Source code for `tqdm_pandas` (really simple!) """ +# def tqdm_pandas(t): +# from pandas.core.groupby import DataFrameGroupBy +# def inner(groups, func, *args, **kwargs): +# t.total = len(groups) + 1 +# def wrapper(*args, **kwargs): +# t.update(1) +# return func(*args, **kwargs) +# result = groups.apply(wrapper, *args, **kwargs) +# t.close() +# return result +# DataFrameGroupBy.progress_apply = inner