77__all__ = ['tqdm_pandas' ]
88
99
10- def tqdm_pandas (t ):
10+ def tqdm_pandas (tclass , * targs , ** tkwargs ):
1111 """
1212 Registers the given `tqdm` instance with
1313 `pandas.core.groupby.DataFrameGroupBy.progress_apply`.
1414 It will even close() the `tqdm` instance upon completion.
1515
16+ Parameters
17+ ----------
18+ tclass : tqdm class you want to use (eg, tqdm, tqdm_notebook, etc)
19+ targs and tkwargs : arguments for the tqdm instance
20+
1621 Examples
1722 --------
1823 >>> import pandas as pd
1924 >>> import numpy as np
2025 >>> from tqdm import tqdm, tqdm_pandas
2126 >>>
2227 >>> df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
23- >>> tqdm_pandas(tqdm() ) # can use tqdm_gui, optional kwargs, etc
28+ >>> tqdm_pandas(tqdm, leave=True ) # can use tqdm_gui, optional kwargs, etc
2429 >>> # Now you can use `progress_apply` instead of `apply`
2530 >>> df.groupby(0).progress_apply(lambda x: x**2)
2631
@@ -44,23 +49,31 @@ def inner(df, func, *args, **kwargs):
4449
4550 *args and *kwargs are transmitted to DataFrameGroupBy.apply()
4651 """
47- t .total = getattr (df , 'ngroups' , None )
48- if t .total is None : # not grouped
49- t .total = len (df ) if isinstance (df , Series ) \
52+ # Precompute total iterations
53+ total = getattr (df , 'ngroups' , None )
54+ if total is None : # not grouped
55+ total = len (df ) if isinstance (df , Series ) \
5056 else df .size // len (df )
5157 else :
52- t . total += 1 # pandas calls update once too many
58+ total += 1 # pandas calls update once too many
5359
60+ # Init bar
61+ t = tclass (* targs , total = total , ** tkwargs )
62+
63+ # Define bar updating wrapper
5464 def wrapper (* args , ** kwargs ):
5565 t .update ()
5666 return func (* args , ** kwargs )
5767
68+ # Apply the provided function (in *args and **kwargs)
69+ # on the df using our wrapper (which provides bar updating)
5870 result = df .apply (wrapper , * args , ** kwargs )
5971
72+ # Close bar and return result
6073 t .close ()
61-
6274 return result
6375
76+ # Monkeypatch pandas to provide easy methods
6477 # Enable custom tqdm progress in pandas!
6578 DataFrame .progress_apply = inner
6679 DataFrameGroupBy .progress_apply = inner
0 commit comments