tidy tests, docs

This commit is contained in:
Casper da Costa-Luis 2016-05-21 17:56:28 +01:00
parent cbc69d04f3
commit 65bc76eca0
4 changed files with 23 additions and 17 deletions

View File

@ -447,7 +447,7 @@ Pandas Integration
~~~~~~~~~~~~~~~~~~
Due to popular demand we've added support for ``pandas`` -- here's an example
for ``DataFrameGroupBy.progress_apply``:
for ``DataFrame.progress_apply`` and ``DataFrameGroupBy.progress_apply``:
.. code:: python
@ -455,6 +455,8 @@ for ``DataFrameGroupBy.progress_apply``:
import numpy as np
from tqdm import tqdm, tqdm_pandas
...
df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
# Create and register a new `tqdm` instance with `pandas`
@ -462,7 +464,9 @@ for ``DataFrameGroupBy.progress_apply``:
tqdm_pandas(tqdm())
# Now you can use `progress_apply` instead of `apply`
df.groupby(0).progress_apply(lambda x: x**2)
df.progress_apply(lambda x: x**2)
# can also groupby:
# df.groupby(0).progress_apply(lambda x: x**2)
In case you're interested in how this works (and how to modify it for your
own callbacks), see the

View File

@ -9,18 +9,20 @@ df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
tqdm_pandas(tqdm())
# Now you can use `progress_apply` instead of `apply`
df.groupby(0).progress_apply(lambda x: x**2)
df.progress_apply(lambda x: x**2)
# can also groupby:
# df.groupby(0).progress_apply(lambda x: x**2)
""" Source code for `tqdm_pandas` (really simple!) """
# def tqdm_pandas(t):
# from pandas.core.groupby import DataFrameGroupBy
# def inner(groups, func, *args, **kwargs):
# t.total = len(groups) + 1
# from pandas.core.frame import DataFrame
# def inner(df, func, *args, **kwargs):
# t.total = groups.size // len(groups)
# def wrapper(*args, **kwargs):
# t.update(1)
# return func(*args, **kwargs)
# result = groups.apply(wrapper, *args, **kwargs)
# result = df.apply(wrapper, *args, **kwargs)
# t.close()
# return result
# DataFrameGroupBy.progress_apply = inner
# DataFrame.progress_apply = inner

View File

@ -32,20 +32,20 @@ def tqdm_pandas(t): # pragma: no cover
from pandas.core.frame import DataFrame
from pandas.core.groupby import DataFrameGroupBy
def inner(groups, func, *args, **kwargs):
def inner(df, func, *args, **kwargs):
"""
Parameters
----------
groups : DataFrame[GroupBy]
(Grouped) data.
df : DataFrame[GroupBy]
Data (may be grouped).
func : function
To be applied on the (grouped) data.
*args and *kwargs are transmitted to DataFrameGroupBy.apply()
"""
t.total = getattr(groups, 'ngroups', None)
t.total = getattr(df, 'ngroups', None)
if t.total is None: # not grouped
t.total = groups.size // len(groups)
t.total = df.size // len(df)
else:
t.total += 1 # pandas calls update once too many
@ -53,7 +53,7 @@ def tqdm_pandas(t): # pragma: no cover
t.update()
return func(*args, **kwargs)
result = groups.apply(wrapper, *args, **kwargs)
result = df.apply(wrapper, *args, **kwargs)
t.close()

View File

@ -5,7 +5,7 @@ from tests_tqdm import with_setup, pretest, posttest, StringIO, closing
@with_setup(pretest, posttest)
def test_pandas():
def test_pandas_groupby_apply():
""" Test pandas.DataFrame.groupby(0).progress_apply """
try:
from numpy.random import randint
@ -31,7 +31,7 @@ def test_pandas():
@with_setup(pretest, posttest)
def test_pandas():
def test_pandas_apply():
""" Test pandas.DataFrame.progress_apply """
try:
from numpy.random import randint
@ -39,7 +39,7 @@ def test_pandas():
import pandas as pd
except:
raise SkipTest
with closing(StringIO()) as our_file:
df = pd.DataFrame(randint(0, 100, (1000, 6)))
tqdm_pandas(tqdm(file=our_file, leave=True, ascii=True))