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

Dataframe.to_excel treats decimal.Decimal as string instead of numeric type, the data in the Excel cell is formatted as a string, not a number #26277

Closed
3f0c4 opened this issue May 3, 2019 · 2 comments
Labels
Bug IO Excel read_excel, to_excel Numeric Operations Arithmetic, Comparison, and Logical operations

Comments

@3f0c4
Copy link

3f0c4 commented May 3, 2019

Code Sample

(float output is prepared for the sake of comparison to Decimal)

# ----------------------------------
### imports

import pandas as pd
import decimal as dc

# ----------------------------------
### definitions

value_str = '2.445'

# ----------------------------------
### instantiations

#df_float = pd.DataFrame(data=[float(value_str)])
df_decimal = pd.DataFrame(data=[dc.Decimal(value_str)])

# ----------------------------------
### exports

# excel
#df_float.to_excel('float' + '.xlsx')
df_decimal.to_excel('decimal' + '.xlsx')

Observed Output

(settings : German locale, decimal sign is ',')

	0
0	2.445

result and Excel warning : number formatted as string

Expected Output

(settings : German locale, decimal sign is ',')

	0
0	2,445

Problem description

Since Decimal represents a numeric type, one would expect that it gets treated as such, just like floats.

The exported value in the Excel cell should be a number, not a string.

Presumably, the openpyxl Cell type should get set to 'n'.

Seemingly, openpyxl is able to handle decimals and knows a numeric Cell type 'n' but pandas deliberately sets string/non-numeric type as the openpyxl Cell type when a Decimal value is set.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Windows
OS-release: 2012ServerR2
machine: AMD64
processor: Intel64 Family 6 Model 79 Stepping 0, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.24.2
pytest: None
pip: 10.0.1
setuptools: 39.1.0
Cython: None
numpy: 1.16.2
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: 2.6.2
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
None

@gfyoung gfyoung added IO Excel read_excel, to_excel Numeric Operations Arithmetic, Comparison, and Logical operations Bug labels May 6, 2019
@gfyoung
Copy link
Member

gfyoung commented May 6, 2019

@3f0c4 : Thanks for opening this! Investigation and PR are welcome!

@3f0c4 3f0c4 closed this as completed May 6, 2019
@3f0c4
Copy link
Author

3f0c4 commented May 6, 2019

I think I was wrong and openpyxl effectively ignores the DECIMAL type. I have to check again later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Excel read_excel, to_excel Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

No branches or pull requests

2 participants