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

BUG: pandas.to_sql raises unexpected column error if data contains -numpy.inf #34431

Closed
2 of 3 tasks
PiotrBenedysiuk opened this issue May 28, 2020 · 3 comments · Fixed by #34493
Closed
2 of 3 tasks
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions

Comments

@PiotrBenedysiuk
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import pandas
import numpy
pandas.DataFrame({'foo': [numpy.inf]}).to_sql('foobar1', engine) #ProgrammingError: inf can not be used with MySQL
pandas.DataFrame({'foo': [-numpy.inf]}).to_sql('foobar2', engine) # OperationalError: (1054, "Unknown column 'infe0' in 'field list'")
pandas.DataFrame({'foo': [-numpy.inf], 'infe0':['bar']}).to_sql('foobar3', engine) # inserts (foo:Null, infe0: 'bar') into db

Problem description

I'd expect consistent behaviour between numpy.inf and -numpy.inf. Raising ProgrammingError in both cases is clear for the user. Furthermore, the query send should not interpret -inf as field infe0.

Expected Output

ProgrammingError: inf can not be used with MySQL in all 3 cases.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.19.76-linuxkit
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: C.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.4
pytest: None
pip: 20.0.2
setuptools: 40.6.2
Cython: None
numpy: 1.15.4
scipy: None
pyarrow: 0.11.1
xarray: None
IPython: 7.13.0
sphinx: None
patsy: None
dateutil: 2.8.1
pytz: 2019.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: 4.4.1
bs4: None
html5lib: None
sqlalchemy: 1.3.11
pymysql: None
psycopg2: None
jinja2: 2.11.2
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@PiotrBenedysiuk PiotrBenedysiuk added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 28, 2020
@arw2019
Copy link
Member

arw2019 commented May 29, 2020

This runs fine on the 1.0. master.

The complete snippet I ran is:

import pandas
import numpy

from sqlalchemy import create_engine

engine = create_engine('sqlite://', echo=False)

pandas.DataFrame({'foo': [numpy.inf]}).to_sql('foobar1', engine) #ProgrammingError: inf can not be used with MySQL
pandas.DataFrame({'foo': [-numpy.inf]}).to_sql('foobar2', engine) # OperationalError: (1054, "Unknown column 'infe0' in 'field list'")
pandas.DataFrame({'foo': [-numpy.inf], 'infe0':['bar']}).to_sql('foobar3', engine) # inserts (foo:Null, infe0: 'bar') into db
Output of pd.show_versions()

INSTALLED VERSIONS

commit : 62c7dd3
python : 3.8.2.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-101-generic
Version : #102-Ubuntu SMP Mon May 11 10:07:26 UTC 2020
machine : x86_64
processor :
byteorder : little
LC_ALL : C.UTF-8
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.0.dev0+1681.g62c7dd3e7.dirty
numpy : 1.17.5
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 46.4.0.post20200518
Cython : 0.29.19
pytest : 5.4.2
hypothesis : 5.15.1
sphinx : 3.0.4
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.1
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.14.0
pandas_datareader: None
bs4 : 4.9.1
bottleneck : 1.3.2
fastparquet : 0.4.0
gcsfs : None
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : 0.17.1
pytables : None
pyxlsb : None
s3fs : 0.4.2
scipy : 1.4.1
sqlalchemy : 1.3.17
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.15.1
xlrd : 1.2.0
xlwt : 1.3.0

@WillAyd
Copy link
Member

WillAyd commented May 29, 2020

Would take a test for this if anyone would like to add

@WillAyd WillAyd added Needs Tests Unit test(s) needed to prevent regressions good first issue and removed Needs Triage Issue that has not been reviewed by a pandas team member Bug labels May 29, 2020
arw2019 added a commit to arw2019/pandas that referenced this issue May 31, 2020
arw2019 added a commit to arw2019/pandas that referenced this issue May 31, 2020
@jorisvandenbossche
Copy link
Member

This might depend on the database you are using (and the database driver).

For example, the example using sqlite also works with pandas 0.23.

(now, it's still good to actually test this for sqlite)

arw2019 added a commit to arw2019/pandas that referenced this issue Jun 1, 2020
arw2019 added a commit to arw2019/pandas that referenced this issue Jun 23, 2020
arw2019 added a commit to arw2019/pandas that referenced this issue Jun 23, 2020
arw2019 added a commit to arw2019/pandas that referenced this issue Jun 23, 2020
mroeschke pushed a commit that referenced this issue Jul 1, 2020
* TST: pd.to_sql for dataframes with -np.inf (#34431)

* DOC: updated what's new (#34431)

* DOC: improved entry (#34431)

* TST: moved to _TestSQLAlchemy + added round trips

* TST: rename + add comment with GH issue #

* TST: rewrote using pytest.mark.parametrize for arg to DataFrame

* TST: removed underscore from _input

* DOC: added double backtick to np.inf & removed extraneous space

* TST: pd.to_sql for dataframes with -np.inf (#34431)

* DOC: updated what's new (#34431)

* DOC: improved entry (#34431)

* TST: moved to _TestSQLAlchemy + added round trips

* TST: rename + add comment with GH issue #

* TST: rewrote using pytest.mark.parametrize for arg to DataFrame

* TST: removed underscore from _input

* DOC: added double backtick to np.inf & removed extraneous space

* BUG: add catch for MySQL error with np.inf

* use regex for string match + add runtime import

* clean up regex

* TST: update to catch error for -np.inf with MySQL

* DOC: resolved conflict in whatsnew

* TST: update test_to_sql_with_neg_npinf

* fixed error handler syntax in SQLDatabase.to_sql

* fixed error handler syntax in SQLDatabase.to_sql

* TST: added an xfail test for npinf entries with mysql

* fixed imports

* added reference to GH issue

* fixed test_to_sql_with_npinf error catch

* fixed spelling error in message (can not -> cannot)

* DOC: added info re MySQL ValueError to whatsnew

* fixed variable name in to_sql

* replaced sqlalchemy's dialect.name with flavor

* fixed typo in test_to_sql-with-npinf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants