Skip to content

Pandas.to_numeric raises ValueError when applied to python Decimal object #14827

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

Closed
RogerThomas opened this issue Dec 8, 2016 · 1 comment
Closed
Labels
Enhancement Numeric Operations Arithmetic, Comparison, and Logical operations
Milestone

Comments

@RogerThomas
Copy link
Contributor

When pandas tries to convert columns to numeric using to_numeric a value error is raised

Example

import decimal
df = pd.DataFrame(dict(col=[decimal.Decimal(9.1)]))
df['col'] = df['col'].apply(pd.to_numeric)

however

import decimal
df = pd.DataFrame(dict(col=[decimal.Decimal(9.1)]))
df['col'] = df['col'].astype(float)

works fine

Problem description

to_numeric should be able to convert decimal objects to float types

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 2.7.12.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-53-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_IE.UTF-8 LOCALE: None.None

pandas: 0.19.1
nose: 1.3.7
pip: 9.0.1
setuptools: 28.3.0
Cython: 0.23.4
numpy: 1.11.2
scipy: 0.16.1
statsmodels: 0.6.1
xarray: None
IPython: None
sphinx: 1.3.1
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: 3.2.0
numexpr: 2.4.6
matplotlib: 1.5.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: 0.9.2
apiclient: None
sqlalchemy: 1.0.9
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.38.0
pandas_datareader: None

@jorisvandenbossche jorisvandenbossche added Enhancement Numeric Operations Arithmetic, Comparison, and Logical operations labels Dec 8, 2016
@jreback
Copy link
Contributor

jreback commented Dec 8, 2016

this could be handled after this https://github.com/pandas-dev/pandas/blob/master/pandas/src/inference.pyx#L678 (IOW a -1 comes back, then could check isinstance of Decimal). In order to avoid any perf penalties.

pull requests welcome.

@jreback jreback added this to the Next Major Release milestone Dec 8, 2016
@jreback jreback modified the milestones: 0.20.0, Next Major Release Dec 19, 2016
ShaharBental pushed a commit to ShaharBental/pandas that referenced this issue Dec 26, 2016
closes pandas-dev#14827

Author: Roger Thomas <[email protected]>
Author: Roger Thomas <[email protected]>

Closes pandas-dev#14842 from RogerThomas/fix_to_numeric_on_decimal_fields and squashes the following commits:

91d989b [Roger Thomas] Merge branch 'master' of github.com:pandas-dev/pandas into fix_to_numeric_on_decimal_fields
d7972d7 [Roger Thomas] Move isdecimal to internal api
1f1c62c [Roger Thomas] Add Test And Refactor is_decimal
f1b69da [Roger Thomas] Merge branch 'master' of github.com:pandas-dev/pandas into fix_to_numeric_on_decimal_fields
2d2488c [Roger Thomas] Fix To Numeric on Decimal Fields
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
3 participants