Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding custom information on pandas progress_apply #1027

Open
3 of 8 tasks
AliAbdelaal opened this issue Aug 31, 2020 · 5 comments
Open
3 of 8 tasks

Adding custom information on pandas progress_apply #1027

AliAbdelaal opened this issue Aug 31, 2020 · 5 comments
Assignees
Labels
question/docs ‽ Documentation clarification candidate submodule ⊂ Periphery/subclasses

Comments

@AliAbdelaal
Copy link

  • I have marked all applicable categories:
    • exception-raising bug
    • visual output bug
    • documentation request (i.e. "X is missing from the documentation." If instead I want to ask "how to use X?" I understand [StackOverflow#tqdm] is more appropriate)
    • new feature request
  • I have visited the [source website], and in particular
    read the [known issues]
  • I have searched through the [issue tracker] for duplicates
  • I have mentioned version numbers, operating system and
    environment, where applicable:
    import tqdm, sys
    print(tqdm.__version__, sys.version, sys.platform)

4.42.1 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] linux

[source website]: https://github.com/tqdm/tqdm/
[known issues]: https://github.com/tqdm/tqdm/#faq-and-known-issues
[issue tracker]: https://github.com/tqdm/tqdm/issues?q=
[StackOverflow#tqdm]: https://stackoverflow.com/questions/tagged/tqdm

Can't add a custom information message such as `set_postfix` for example when dealing with pandas dataframe `progress_apply` function.
@casperdcl
Copy link
Sponsor Member

casperdcl commented Aug 31, 2020

#from tqdm.auto import tqdm
from tqdm import tqdm
import pandas as pd

df = pd.DataFrame(...)
tqdm.pandas(desc="customised", postfix={"hello": -1})
df.progress_apply(...)

tqdm.pandas(desc="remove postfix", postfix=None)
df.progress_apply(...)

I agree this is clunky but (kw)arguments to df.progress_apply are all passed to df.apply

@casperdcl casperdcl self-assigned this Aug 31, 2020
@casperdcl casperdcl added question/docs ‽ Documentation clarification candidate submodule ⊂ Periphery/subclasses labels Aug 31, 2020
@AliAbdelaal
Copy link
Author

#from tqdm.auto import tqdm
from tqdm import tqdm
import pandas as pd

df = pd.DataFrame(...)
tqdm.pandas(desc="customised", postfix={"hello": -1})
df.progress_apply(...)

tqdm.pandas(desc="remove postfix", postfix=None)
df.progress_apply(...)

I agree this is clunky but (kw)arguments to df.progress_apply are all passed to df.apply

Can I change the postfix string based on the iteration dynamically instead of having some fixed postfix ?

@casperdcl
Copy link
Sponsor Member

That's not exactly currently supported, and it wouldn't be easy to add without breaking backward compatibility.

The easiest approach would be explicitly defining a bar_format, e.g:

tqdm.pandas(
	bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}, iter={n}]")
df.progress_apply(...)

@AliAbdelaal
Copy link
Author

Cool, thank you so much for your help.

@armsp
Copy link

armsp commented May 14, 2024

Currently I use it as follows -

from tqdm import tqdm
pbar = tqdm()

# inside of a class's __init__
        pbar.pandas(bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}]")
# inside the function that progress_apply calls -
        pbar.set_postfix({"Accuracy":self.accuracy})  # Update with dynamic info

# progress_apply
        df.progress_apply(self.prediction, axis=1)) 
# in the end
pbar.close()

The result is usable, but could definitely be made prettier -

0it [00:00, ?it/s]
0it [04:24, ?it/s, Accuracy=0.559]
 16%|███████████████████████████████▋                                                                 | 119/743 [03:34<20:11,  1.94s/it]                                                                                     

Not sure if that's the best approach, how do you use it @casperdcl ? Because the ideal solution would be having that custom postfix displayed on the right and not the top.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question/docs ‽ Documentation clarification candidate submodule ⊂ Periphery/subclasses
Projects
None yet
Development

No branches or pull requests

3 participants