Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 658
1
import sys
2
3
__author__ = "github.com/casperdcl"
4
__all__ = ['tqdm_pandas']
5
6
7
def tqdm_pandas(tclass, *targs, **tkwargs):
8
"""
9
Registers the given `tqdm` instance with
10
`pandas.core.groupby.DataFrameGroupBy.progress_apply`.
11
It will even close() the `tqdm` instance upon completion.
12
13
Parameters
14
----------
15
tclass : tqdm class you want to use (eg, tqdm, tqdm_notebook, etc)
16
targs and tkwargs : arguments for the tqdm instance
17
18
Examples
19
--------
20
>>> import pandas as pd
21
>>> import numpy as np
22
>>> from tqdm import tqdm, tqdm_pandas
23
>>>
24
>>> df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
25
>>> tqdm_pandas(tqdm, leave=True) # can use tqdm_gui, optional kwargs, etc
26
>>> # Now you can use `progress_apply` instead of `apply`
27
>>> df.groupby(0).progress_apply(lambda x: x**2)
28
29
References
30
----------
31
https://stackoverflow.com/questions/18603270/
32
progress-indicator-during-pandas-operations-python
33
"""
34
from tqdm import TqdmDeprecationWarning
35
36
if isinstance(tclass, type) or (getattr(tclass, '__name__', '').startswith(
37
'tqdm_')): # delayed adapter case
38
TqdmDeprecationWarning("""\
39
Please use `tqdm.pandas(...)` instead of `tqdm_pandas(tqdm, ...)`.
40
""", fp_write=getattr(tkwargs.get('file', None), 'write', sys.stderr.write))
41
tclass.pandas(*targs, **tkwargs)
42
else:
43
TqdmDeprecationWarning("""\
44
Please use `tqdm.pandas(...)` instead of `tqdm_pandas(tqdm(...))`.
45
""", fp_write=getattr(tclass.fp, 'write', sys.stderr.write))
46
type(tclass).pandas(deprecated_t=tclass)
47
48