mirror of https://github.com/tqdm/tqdm.git
tidy tests, docs
This commit is contained in:
parent
cbc69d04f3
commit
65bc76eca0
|
@ -447,7 +447,7 @@ Pandas Integration
|
||||||
~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
Due to popular demand we've added support for ``pandas`` -- here's an example
|
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
|
.. code:: python
|
||||||
|
|
||||||
|
@ -455,6 +455,8 @@ for ``DataFrameGroupBy.progress_apply``:
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from tqdm import tqdm, tqdm_pandas
|
from tqdm import tqdm, tqdm_pandas
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
|
df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
|
||||||
|
|
||||||
# Create and register a new `tqdm` instance with `pandas`
|
# Create and register a new `tqdm` instance with `pandas`
|
||||||
|
@ -462,7 +464,9 @@ for ``DataFrameGroupBy.progress_apply``:
|
||||||
tqdm_pandas(tqdm())
|
tqdm_pandas(tqdm())
|
||||||
|
|
||||||
# Now you can use `progress_apply` instead of `apply`
|
# 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
|
In case you're interested in how this works (and how to modify it for your
|
||||||
own callbacks), see the
|
own callbacks), see the
|
||||||
|
|
|
@ -9,18 +9,20 @@ df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
|
||||||
tqdm_pandas(tqdm())
|
tqdm_pandas(tqdm())
|
||||||
|
|
||||||
# Now you can use `progress_apply` instead of `apply`
|
# 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!) """
|
""" Source code for `tqdm_pandas` (really simple!) """
|
||||||
# def tqdm_pandas(t):
|
# def tqdm_pandas(t):
|
||||||
# from pandas.core.groupby import DataFrameGroupBy
|
# from pandas.core.frame import DataFrame
|
||||||
# def inner(groups, func, *args, **kwargs):
|
# def inner(df, func, *args, **kwargs):
|
||||||
# t.total = len(groups) + 1
|
# t.total = groups.size // len(groups)
|
||||||
# def wrapper(*args, **kwargs):
|
# def wrapper(*args, **kwargs):
|
||||||
# t.update(1)
|
# t.update(1)
|
||||||
# return func(*args, **kwargs)
|
# return func(*args, **kwargs)
|
||||||
# result = groups.apply(wrapper, *args, **kwargs)
|
# result = df.apply(wrapper, *args, **kwargs)
|
||||||
# t.close()
|
# t.close()
|
||||||
# return result
|
# return result
|
||||||
# DataFrameGroupBy.progress_apply = inner
|
# DataFrame.progress_apply = inner
|
||||||
|
|
|
@ -32,20 +32,20 @@ def tqdm_pandas(t): # pragma: no cover
|
||||||
from pandas.core.frame import DataFrame
|
from pandas.core.frame import DataFrame
|
||||||
from pandas.core.groupby import DataFrameGroupBy
|
from pandas.core.groupby import DataFrameGroupBy
|
||||||
|
|
||||||
def inner(groups, func, *args, **kwargs):
|
def inner(df, func, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
groups : DataFrame[GroupBy]
|
df : DataFrame[GroupBy]
|
||||||
(Grouped) data.
|
Data (may be grouped).
|
||||||
func : function
|
func : function
|
||||||
To be applied on the (grouped) data.
|
To be applied on the (grouped) data.
|
||||||
|
|
||||||
*args and *kwargs are transmitted to DataFrameGroupBy.apply()
|
*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
|
if t.total is None: # not grouped
|
||||||
t.total = groups.size // len(groups)
|
t.total = df.size // len(df)
|
||||||
else:
|
else:
|
||||||
t.total += 1 # pandas calls update once too many
|
t.total += 1 # pandas calls update once too many
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ def tqdm_pandas(t): # pragma: no cover
|
||||||
t.update()
|
t.update()
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
result = groups.apply(wrapper, *args, **kwargs)
|
result = df.apply(wrapper, *args, **kwargs)
|
||||||
|
|
||||||
t.close()
|
t.close()
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ from tests_tqdm import with_setup, pretest, posttest, StringIO, closing
|
||||||
|
|
||||||
|
|
||||||
@with_setup(pretest, posttest)
|
@with_setup(pretest, posttest)
|
||||||
def test_pandas():
|
def test_pandas_groupby_apply():
|
||||||
""" Test pandas.DataFrame.groupby(0).progress_apply """
|
""" Test pandas.DataFrame.groupby(0).progress_apply """
|
||||||
try:
|
try:
|
||||||
from numpy.random import randint
|
from numpy.random import randint
|
||||||
|
@ -31,7 +31,7 @@ def test_pandas():
|
||||||
|
|
||||||
|
|
||||||
@with_setup(pretest, posttest)
|
@with_setup(pretest, posttest)
|
||||||
def test_pandas():
|
def test_pandas_apply():
|
||||||
""" Test pandas.DataFrame.progress_apply """
|
""" Test pandas.DataFrame.progress_apply """
|
||||||
try:
|
try:
|
||||||
from numpy.random import randint
|
from numpy.random import randint
|
||||||
|
@ -39,7 +39,7 @@ def test_pandas():
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
except:
|
except:
|
||||||
raise SkipTest
|
raise SkipTest
|
||||||
|
|
||||||
with closing(StringIO()) as our_file:
|
with closing(StringIO()) as our_file:
|
||||||
df = pd.DataFrame(randint(0, 100, (1000, 6)))
|
df = pd.DataFrame(randint(0, 100, (1000, 6)))
|
||||||
tqdm_pandas(tqdm(file=our_file, leave=True, ascii=True))
|
tqdm_pandas(tqdm(file=our_file, leave=True, ascii=True))
|
||||||
|
|
Loading…
Reference in New Issue