Skip to content

Commit ee6abc9

Browse files
committed
Fix tqdm_pandas(tqdm_notebook) by delaying instanciation in tqdm_pandas
Signed-off-by: Stephen L. <[email protected]>
1 parent bb53160 commit ee6abc9

3 files changed

Lines changed: 27 additions & 14 deletions

File tree

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,8 @@ for ``DataFrame.progress_apply`` and ``DataFrameGroupBy.progress_apply``:
479479
df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
480480
481481
# Create and register a new `tqdm` instance with `pandas`
482-
# (can use tqdm_gui, optional kwargs, etc.)
483-
tqdm_pandas(tqdm())
482+
# (can use tqdm_gui, tqdm_notebook, optional kwargs, etc.)
483+
tqdm_pandas(tqdm, leave=True)
484484
485485
# Now you can use `progress_apply` instead of `apply`
486486
df.progress_apply(lambda x: x**2)

tqdm/_tqdm_pandas.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@
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

tqdm/tests/tests_pandas.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ def test_pandas_groupby_apply():
1818
df = pd.DataFrame(randint(0, 50, (500, 3)))
1919
dfs = pd.DataFrame(randint(0, 50, (500, 3)),
2020
columns=list('abc'))
21-
tqdm_pandas(tqdm(file=our_file, leave=False, ascii=True))
21+
tqdm_pandas(tqdm, file=our_file, leave=False, ascii=True)
2222
df.groupby(0).progress_apply(lambda x: None)
23-
tqdm_pandas(tqdm(file=our_file, leave=False, ascii=True))
23+
tqdm_pandas(tqdm, file=our_file, leave=False, ascii=True)
2424
dfs.groupby(['a']).progress_apply(lambda x: None)
2525

2626
our_file.seek(0)
@@ -48,9 +48,9 @@ def test_pandas_apply():
4848
df = pd.DataFrame(randint(0, 50, (500, 3)))
4949
dfs = pd.DataFrame(randint(0, 50, (500, 3)),
5050
columns=list('abc'))
51-
tqdm_pandas(tqdm(file=our_file, leave=True, ascii=True))
51+
tqdm_pandas(tqdm, file=our_file, leave=True, ascii=True)
5252
df.progress_apply(lambda x: None)
53-
tqdm_pandas(tqdm(file=our_file, leave=True, ascii=True))
53+
tqdm_pandas(tqdm, file=our_file, leave=True, ascii=True)
5454
dfs.a.progress_apply(lambda x: None)
5555

5656
our_file.seek(0)
@@ -73,7 +73,7 @@ def test_pandas_leave():
7373

7474
with closing(StringIO()) as our_file:
7575
df = pd.DataFrame(randint(0, 100, (1000, 6)))
76-
tqdm_pandas(tqdm(file=our_file, leave=True, ascii=True))
76+
tqdm_pandas(tqdm, file=our_file, leave=True, ascii=True)
7777
df.groupby(0).progress_apply(lambda x: None)
7878

7979
our_file.seek(0)

0 commit comments

Comments
 (0)