Skip to content

display np.int64 numbers as integers, do not apply 'floatfmt' #18

@jonsel087

Description

@jonsel087

This is more a result of how pandas data frames are stored internally, but the issue comes up when you tabulate any dataframe that contains float type columns. When the df is converted to a numpy array, numpy will upcast all int values to floats.

from tabulate import tabulate
import pandas as pd
df = pd.DataFrame({'A':[1,2,3,4],'B':[.20942, .092384, .019823, .01923]})
print(tabulate(df, floatfmt='.2f', headers=df.columns, tablefmt='pipe'))
|    |    A |    B |
|---:|-----:|-----:|
|  0 | 1.00 | 0.21 |
|  1 | 2.00 | 0.09 |
|  2 | 3.00 | 0.02 |
|  3 | 4.00 | 0.02 |

This can further be shown that it's caused by internal storage of np.int64 dtype:

print(tabulate([['int','float'],[np.int64(1), 3.14159]], headers='firstrow',tablefmt='pipe', floatfmt='.2f'))
|   int |   float |
|------:|--------:|
|  1.00 |    3.14 |

Is there a safe way to convert int64 types to vanilla int when parsing dataframes? Maybe there is a workaround i'm not thinking of.

PS - Note that np.int64(1) == int(1) evaluates to True

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions