Skip to content

Commit

Permalink
bump version, merge branch 'pandas-rolling'
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Apr 15, 2018
2 parents d42a3d7 + b2228e1 commit 10a7ed1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
14 changes: 12 additions & 2 deletions tqdm/_tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,11 @@ def pandas(tclass, *targs, **tkwargs):
from pandas.core.groupby import GroupBy
from pandas.core.groupby import PanelGroupBy
from pandas import Panel
try:
# pandas>=0.18.0
from pandas.core.window import _Rolling_and_Expanding
except ImportError: # pragma: no cover
_Rolling_and_Expanding = None

deprecated_t = [tkwargs.pop('deprecated_t', None)]

Expand All @@ -568,7 +573,9 @@ def inner(df, func, *args, **kwargs):
total = df.size
elif isinstance(df, Series):
total = len(df)
else: # DataFrame or Panel
elif _Rolling_and_Expanding is None or \
not isinstance(df, _Rolling_and_Expanding):
# DataFrame or Panel
axis = kwargs.get('axis', 0)
# when axis=0, total is shape[axis1]
total = df.size // df.shape[axis]
Expand All @@ -595,7 +602,7 @@ def wrapper(*args, **kwargs):
# it seems `pandas apply` calls `func` twice
# on the first column/row to decide whether it can
# take a fast or slow code path; so stop when t.total==t.n
t.update(n=1 if t.total and t.n < t.total else 0)
t.update(n=1 if not t.total or t.n < t.total else 0)
return func(*args, **kwargs)

# Apply the provided function (in **kwargs)
Expand Down Expand Up @@ -626,6 +633,9 @@ def wrapper(*args, **kwargs):
GroupBy.progress_aggregate = inner_generator('aggregate')
GroupBy.progress_transform = inner_generator('transform')

if _Rolling_and_Expanding is not None: # pragma: no cover
_Rolling_and_Expanding.progress_apply = inner_generator()

def __init__(self, iterable=None, desc=None, total=None, leave=True,
file=None, ncols=None, mininterval=0.1, maxinterval=10.0,
miniters=None, ascii=None, disable=False, unit='it',
Expand Down
2 changes: 1 addition & 1 deletion tqdm/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
__all__ = ["__version__"]

# major, minor, patch, -extra
version_info = 4, 22, 0
version_info = 4, 23, 0

# Nice string for the version
__version__ = '.'.join(map(str, version_info))
Expand Down
31 changes: 31 additions & 0 deletions tqdm/tests/tests_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@ def test_pandas_setup():
assert '100/123' in res


@with_setup(pretest, posttest)
def test_pandas_rolling_expanding():
"""Test pandas.(Series|DataFrame).(rolling|expanding)"""
try:
from numpy.random import randint
import pandas as pd
except ImportError:
raise SkipTest

with closing(StringIO()) as our_file:
tqdm.pandas(file=our_file, leave=True, ascii=True)

series = pd.Series(randint(0, 50, (123,)))
res1 = series.rolling(10).progress_apply(lambda x: 1)
res2 = series.rolling(10).apply(lambda x: 1)
assert res1.equals(res2)

res3 = series.expanding(10).progress_apply(lambda x: 2)
res4 = series.expanding(10).apply(lambda x: 2)
assert res3.equals(res4)

expects = ['114it'] # 123-10+1
for exres in expects:
our_file.seek(0)
if our_file.getvalue().count(exres) < 2:
our_file.seek(0)
raise AssertionError(
"\nExpected:\n{0}\nIn:\n{1}\n".format(
exres + " at least twice.", our_file.read()))


@with_setup(pretest, posttest)
def test_pandas_series():
"""Test pandas.Series.progress_apply and .progress_map"""
Expand Down

0 comments on commit 10a7ed1

Please sign in to comment.