question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Int64 numbers from Pandas DataFrames interpreted as float and incorrectly printed due to overflow

See original GitHub issue

Issue

When Tabulate prints a Pandas DataFrame with an int64 field the resulting value is incorrectly shown due to overflow. It appears that Tabulate is interpreting Pandas int64 fields as float and then performing a format() call which fails natively in Python:

format(503498111827123021, '.0f')
'503498111827123008'

Expected Behavior

This error does not happen when passing a 64 bit int as a list directly into Tabulate because it is treating the int64 as an int. I believe the fix here is that Tabulate should recognize that a DataFrame int64 field should also be treated as an integer and not attempt to perform a floating format operation.

Reproduction

Test 64bit int with Pandas DataFrame head() -> Correct

import pandas as pd

df = pd.DataFrame({'colA': [503498111827123021]})
df.dtypes

colA    int64
dtype: object

df.head()
                 colA
0  503498111827123021

Test 64bit int withtabulate() on list data-> Correct

from tabulate import tabulate

table = [[503498111827123021]]
print(tabulate(table))
------------------
503498111827123021
------------------

print(tabulate(table, floatfmt='.0f'))
------------------
503498111827123021
------------------

Test 64 bit float with with tabulate() on float data -> Incorrect

from tabulate import tabulate

table = [[503498111827123021.0]]
print(tabulate(table, floatfmt='.0f'))
------------------
503498111827123008
------------------

Test 64 bit int DataFrame field with various combinations -> Incorrect

from tabulate import tabulate
import pandas as pd

df = pd.DataFrame({'colA': [503498111827123021]})
print(tabulate(df, floatfmt='.0f'))
-  ------------------
0  503498111827123008
-  ------------------

print(tabulate(df))
# Without arguments this is being seen as float
-  -----------
0  5.03498e+17
-  -----------

print(df.to_markdown(floatfmt='.0f'))
|    |               colA |
|---:|-------------------:|
|  0 | 503498111827123008 |

print(df.to_markdown())
|    |        colA |
|---:|------------:|
|  0 | 5.03498e+17 |

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jbencinacommented, Nov 3, 2022

@astanin I think we’re saying the same thing. I did have the issue in 0.9.0 but not in the latest source from GitHub. I downloaded the latter after filing the issue because I was going to debug it and realized it was already fixed 🙂

0reactions
astanincommented, Nov 8, 2022

@jbencina No specific date for the next release yet. There are quite some bugs to fix https://github.com/astanin/python-tabulate/milestones/v0.9.1 I’ll try to remember to update this thread when it’s done.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stop Pandas from converting int to float due to an insertion in ...
As of pandas 1.0.0 I believe you have another option, which is to first use convert_dtypes. This converts the dataframe columns to dtypes...
Read more >
display np.int64 numbers as integers, do not apply 'floatfmt' #18
When the df is converted to a numpy array, numpy will upcast all int values to floats. ... Is there a safe way...
Read more >
pandas.to_numeric — pandas 1.5.2 documentation
Convert argument to a numeric type. The default return dtype is float64 or int64 depending on the data supplied. Use the downcast parameter...
Read more >
How to Fix: ValueError: cannot convert float NaN to integer
This error occurs when you attempt to convert a column in a pandas DataFrame from a float to an integer, yet the column...
Read more >
Python Data Analysis with Pandas and Matplotlib - Coding Club
DataFrames are organised into colums (each of which is a Series), and each column can store a single data-type, such as floating point...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found