From bfa5bba4700df0c1489c508ec1e5769c320e996f Mon Sep 17 00:00:00 2001 From: Guangshuo CHEN Date: Fri, 13 Apr 2018 08:53:42 +0200 Subject: [PATCH] add test --- tqdm/_tqdm.py | 2 +- tqdm/tests/tests_pandas.py | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/tqdm/_tqdm.py b/tqdm/_tqdm.py index ac9e5c44..383313af 100755 --- a/tqdm/_tqdm.py +++ b/tqdm/_tqdm.py @@ -549,7 +549,7 @@ class tqdm(object): try: from pandas.core.window import _Rolling_and_Expanding skip_rolling_and_expanding = False - except ImportError: + except ImportError: # pragma: no cover skip_rolling_and_expanding = True deprecated_t = [tkwargs.pop('deprecated_t', None)] diff --git a/tqdm/tests/tests_pandas.py b/tqdm/tests/tests_pandas.py index 45124c84..706b7f6d 100644 --- a/tqdm/tests/tests_pandas.py +++ b/tqdm/tests/tests_pandas.py @@ -20,6 +20,81 @@ 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_data_frame(): + """Test pandas.DataFrame.progress_apply and .progress_applymap""" + 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) + df = pd.DataFrame(randint(0, 50, (100, 200))) + + def task_func(x): + return x + 1 + + # applymap + res1 = df.progress_applymap(task_func) + res2 = df.applymap(task_func) + assert res1.equals(res2) + + # apply + for axis in [0, 1]: + res3 = df.progress_apply(task_func, axis=axis) + res4 = df.apply(task_func, axis=axis) + assert res3.equals(res4) + + our_file.seek(0) + if our_file.read().count('100%') < 3: + our_file.seek(0) + raise AssertionError("\nExpected:\n{0}\nIn:\n{1}\n".format( + '100% at least three times', our_file.read())) + + # apply_map, apply axis=0, apply axis=1 + expects = ['20000/20000', '200/200', '100/100'] + for exres in expects: + our_file.seek(0) + if our_file.getvalue().count(exres) < 1: + our_file.seek(0) + raise AssertionError( + "\nExpected:\n{0}\nIn:\n {1}\n".format( + exres + " at least once.", our_file.read())) + + @with_setup(pretest, posttest) def test_pandas_series(): """Test pandas.Series.progress_apply and .progress_map"""