-
Notifications
You must be signed in to change notification settings - Fork 179
Closed
Labels
bugSomething isn't workingSomething isn't working
Milestone
Description
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
ddemidov, MartijnVanbiervliet, rgemulla, s-weigand, esnahn and 2 more
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working