Skip to content

Commit 2e74274

Browse files
committed
tqdm_pandas: backwards compatibility + deprecation message and unit test
Signed-off-by: Stephen L. <[email protected]>
1 parent ee6abc9 commit 2e74274

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

tqdm/_tqdm_pandas.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ def inner(df, func, *args, **kwargs):
5858
total += 1 # pandas calls update once too many
5959

6060
# Init bar
61-
t = tclass(*targs, total=total, **tkwargs)
61+
if isinstance(tclass, type) or \
62+
(hasattr(tclass, '__name__') and
63+
tclass.__name__ == 'tqdm_notebook'): # delayed adapter case
64+
t = tclass(*targs, total=total, **tkwargs)
65+
else:
66+
t = tclass
67+
t.total = total
68+
t.write("Warning: tqdm_pandas: using a bar instance is deprecated,"
69+
" please provide a bar class instead.", file=t.fp)
6270

6371
# Define bar updating wrapper
6472
def wrapper(*args, **kwargs):
@@ -69,7 +77,7 @@ def wrapper(*args, **kwargs):
6977
# on the df using our wrapper (which provides bar updating)
7078
result = df.apply(wrapper, *args, **kwargs)
7179

72-
# Close bar and return result
80+
# Close bar and return pandas calculation result
7381
t.close()
7482
return result
7583

tqdm/tests/tests_pandas.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,24 @@ def test_pandas_leave():
8383
our_file.seek(0)
8484
raise AssertionError("\nExpected:\n{0}\nIn:{1}\n".format(
8585
exres, our_file.read()))
86+
87+
88+
@with_setup(pretest, posttest)
89+
def test_pandas_deprecation():
90+
""" Test bar object instance as argument deprecation """
91+
try:
92+
from numpy.random import randint
93+
from tqdm import tqdm_pandas
94+
import pandas as pd
95+
except:
96+
raise SkipTest
97+
98+
# Trigger a warning.
99+
with closing(StringIO()) as our_file:
100+
df = pd.DataFrame(randint(0, 50, (500, 3)))
101+
tqdm_pandas(tqdm(file=our_file, leave=False, ascii=True, ncols=20))
102+
df.groupby(0).progress_apply(lambda x: None)
103+
# Check deprecation message
104+
our_file.seek(0)
105+
out = our_file.read()
106+
assert "deprecated" in out

0 commit comments

Comments
 (0)