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

The missingval argument doesn't recognise np.nan values, it only recognises None #316

Open
paulrougieux opened this issue Mar 22, 2024 · 0 comments

Comments

@paulrougieux
Copy link

paulrougieux commented Mar 22, 2024

Related to

Current behaviour

The missingval argument doesn't recognise np.nan values, it only recognises None


import pandas
import numpy as np
from tabulate import tabulate
df = pandas.DataFrame({"x": [1, 2], "y": [0, np.nan]})
print(tabulate(df,floatfmt=".0f", missingval="-",tablefmt="grid"))
--
+---+---+-----+
| 0 | 1 |   0 |
+---+---+-----+
| 1 | 2 | nan |
+---+---+-----+

In [6]: print(tabulate(df.replace(np.nan, None),floatfmt=".0f", missingval="-",tablefmt="grid"))
+---+---+---+
| 0 | 1 | 0 |
+---+---+---+
| 1 | 2 | - |
+---+---+---+

Note: In practice I use the data frame .to_markdown() methods which calls tabulate in the background, as explained in the pandas documentation of DataFrame.to_markdown

In [7]: print(df.to_markdown(floatfmt=".0f", index=False, missingval="-"))
|   x |   y |
|----:|----:|
|   1 |   0 |
|   2 | nan |

In [8]: print(df.replace(np.nan, None).to_markdown(floatfmt=".0f", index=False, missingval="-"))
|   x |   y |
|----:|----:|
|   1 |   0 |
|   2 |   - |

Proposed behaviour

The missingval argument should recognise np.nan as a missing value. This would help avoid having to replace them with
df.replace(np.nan, None).

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

1 participant