Search code examples
pythonpandastqdm

Is there progress_transform which shows the progress bar like progress_apply?


I am trying to use df["series"].transform instead of df["series"].apply. For .apply we have .progress_apply after initiate

from tqdm import tqdm
tqdm.pandas
df["series"].progress_apply(function)

however, I cant find the version for .progress_transform. I know we can use other tqdm methods like loop it but I just wish to know whether do we have .progress_transform? thank you


Solution

  • Maybe we can look at the actual source code and change apply to transform?

    import pandas as pd
    import numpy as np
    from tqdm.auto import tqdm
    
    def tqdm_pandas(t):
        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 = df.transform(wrapper, *args, **kwargs)
            t.close()
            return result
        DataFrame.progress_transform = inner
        
        
    df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
    
    # Register `pandas.progress_apply` and `pandas.Series.map_apply` with `tqdm`
    # (can use `tqdm.gui.tqdm`, `tqdm.notebook.tqdm`, optional kwargs, etc.)
    tqdm.pandas(desc="my bar!")
    
    
    df.groupby(0)[5].progress_transform(sum)