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

Idea/Feature Request: Allow callable for floatfmt. #297

Open
jagerber48 opened this issue Oct 21, 2023 · 3 comments
Open

Idea/Feature Request: Allow callable for floatfmt. #297

jagerber48 opened this issue Oct 21, 2023 · 3 comments

Comments

@jagerber48
Copy link

I have a custom formatter for floats. One example feature is that I can format floats to be in exponential notation but to always have an exponent that is a multiple of 3 (engineering notation). Right now if I apply this formatter to a table (e.g. list of lists) and then run it through tabulate, all of the floats (now strings) will be re-cast as floats and reformatted using the default float format. This is not what I want. In fact, because "float castable stings" get converted back to floats it's actually impossible for me to get what I want. What would be better is if I can pass in a callable function to be used to format floats instead of just a python built-in format specification mini-language specifier

@jagerber48 jagerber48 changed the title Allow callable for floatfmt. Idea/Feature Request: Allow callable for floatfmt. Oct 21, 2023
@Shreyansh-Agrawal
Copy link

I am facing a similar issue while printing data using tabulate-

File "c:<FILEPATH>\VSCode\QuizApp\controllers\handlers\quiz_handler.py", line 59, in display_questions_by_category
pretty_print(
File "c:<FILEPATH>\VSCode\QuizApp\utils\pretty_print.py", line 12, in pretty_print
print(tabulate(data, headers=headers, tablefmt='rounded_grid', showindex=row_id))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:<FILEPATH>\Lib\site-packages\tabulate_init_.py", line 2153, in tabulate
cols = [
^
File "C:<FILEPATH>\Lib\site-packages\tabulate_init_.py", line 2154, in <listcomp>
[format(v, ct, fl_fmt, int_fmt, miss_v, has_invisible) for v in c]
File "C:<FILEPATH>\Lib\site-packages\tabulate_init
.py", line 2154, in <listcomp>
[format(v, ct, fl_fmt, int_fmt, miss_v, has_invisible) for v in c]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:<FILEPATH>\Lib\site-packages\tabulate_init
.py", line 1232, in _format
return format(float(val), floatfmt)
^^^^^^^^^^
ValueError: could not convert string to float: 'True'

Type of data I am printing -

<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
<class 'str'> <class 'str'> <class 'str'> <class 'str'>

@nunamia
Copy link

nunamia commented Feb 21, 2024

fix
image

@jagerber48
Copy link
Author

I was able to work around this issue fine by setting disable_numparse=True. I was pretty surprised to see this sort of auto-parsing done by default with an option to suppress this behavior slightly buried in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants