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.

BUG: Small numbers in scientific notation do not show the right significant figures in DataFrame.

See original GitHub issue

Research

  • I have searched the [pandas] tag on StackOverflow for similar questions.

  • I have asked my usage related question on StackOverflow.

Link to question on StackOverflow

https://stackoverflow.com/questions/71359050/pandas-series-not-showing-small-numbers-in-scientific-notation-depending-on-firs/71359216#71359216

Question about pandas

I am using pandas dataframes to store astronomical data (this is my library) and the default unit system has many variables in scientific notation. I would like to use the to_string() function to save the data as a text file. This is an example of the workflow:

import numpy as np
import pandas as pd

myDF = pd.DataFrame(columns=['c0', 'c1', 'c2', 'c3'])

myDF.loc['r0', 'c0'] = 'abc'
myDF.loc['r0', 'c1'] = np.nan
myDF.loc['r0', 'c2'] = 1234.0
myDF.loc['r0', 'c3'] = 1.234e-18

# Save the table as a dataframe.
with open(f'output_DF.txt', 'wb') as output_file:
    string_DF = myDF.to_string()
    output_file.write(string_DF.encode('UTF-8'))

print('Output DF\n', myDF)

Calling the cell data from the dataframe returns the right value (in this case 1.234e-18) but the dataframe is stored in a text file as:

      c0   c1     c2   c3
r0  abc  NaN  1234.0  0.0

This is also how it is displayed on the terminal. I think that the right behaviour should preserve the significant figures. I am not sure if this is an issue with the dtypes in the dataframe creation or while writting the output file.

I am using pandas 1.4.1 in windows 10. I wonder if anyone could please offer me some advice on the right way to do this operation.

Thank you for your work.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mroeschkecommented, Mar 30, 2022

@ArLough I would recommend not tackling an issue with any of the Needs ... tags as further discussion might be needed to determine the action item. Instead I would suggest looking for issues with good first issue

0reactions
ArLoughcommented, Mar 31, 2022

Ok, thank you! That’s helpful

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas series not showing small numbers in scientific ...
I would shape my df first, with proper dtypes, then add the data: import pandas as pd df = pd.DataFrame( {'ion': pd.Series(dtype='str') ...
Read more >
How to suppress scientific notation in Pandas - Re-thought
Four easy ways to suppress scientific notation in Python Pandas with detailed instructions and two ways of reverting back your global ...
Read more >
Floating-point arithmetic may give inaccurate result in Excel
Microsoft Excel was designed around the IEEE 754 specification to ... In this case, the normalized number has eight significant digits ...
Read more >
v0.17.0.rst.txt - Pandas
A ``Series`` or a ``DataFrame`` column previously *could* be assigned a ... precision as "significant figures" this did work for scientific notation but ......
Read more >
Pandas series not showing small numbers in scientific ...
I have managed to find the error source: In the current version of pandas, dataframes with the column 'object' dtype no longer use...
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