Skip to content

Bug when using SQLAlchemy and masters' to_sql for integer-named columns in df #6925

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
JohnSmizz opened this issue Apr 22, 2014 · 2 comments
Closed
Labels
IO SQL to_sql, read_sql, read_sql_query

Comments

@JohnSmizz
Copy link

Hi-
when trying to write a dataframe with non-string named columns via current to_sql the function fails because function _safe_col_name attempts to call the strip method.
NB I do not have master installed I just copied the SQL module and call the v14 functions from it.

df = pd.DataFrame(randn(2,4))
con = sqlalchemy.create_engine("mssql+pyodbc://server/db?driver=SQL Server Native Client 11.0")
import sql
sql.to_sql(df, "TBL_TEMP", con, if_exists="replace")

yields the following error:

Traceback (most recent call last):

  File "<ipython-input-40-36a592728500>", line 4, in <module>
    sql.to_sql(df, "TBL_TEMP", con, if_exists="replace")

  File "N:\Python\sql.py", line 399, in to_sql
    index_label=index_label)

  File "N:\Python\sql.py", line 773, in to_sql
    index_label=index_label)

  File "N:\Python\sql.py", line 479, in __init__
    self.table = self._create_table_statement()

  File "N:\Python\sql.py", line 601, in _create_table_statement
    for name, typ in zip(safe_columns, column_types)]

  File "N:\Python\sql.py", line 600, in <listcomp>
    columns = [Column(name, typ)

  File "N:\Python\sql.py", line 50, in _safe_col_name
    return col_name.strip().replace(' ', '_')

AttributeError: 'numpy.int64' object has no attribute 'strip'

However if i replace the first line with

df = pd.DataFrame(randn(2,4), columns=list("abcd"))

there is no error.

showversions info below

INSTALLED VERSIONS

commit: None
python: 3.3.3.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 44 Stepping 2, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en_GB

pandas: 0.13.1
Cython: None
numpy: 1.8.0
scipy: 0.13.2
statsmodels: 0.5.0
IPython: 1.1.0
sphinx: 1.2
patsy: 0.2.1
scikits.timeseries: None
dateutil: 2.2
pytz: 2013.8
bottleneck: None
tables: 3.0.0
numexpr: 2.2.2
matplotlib: 1.3.1
openpyxl: 1.6.1
xlrd: 0.9.2
xlwt: None
xlsxwriter: 0.5.2
sqlalchemy: 0.9.0
lxml: None
bs4: None
html5lib: None
bq: None

@jorisvandenbossche
Copy link
Member

This should be fixed in PR #6902, which was merged some hours ago. Can you confirm this?

Note, the whole 'safe columns' is removed, they are now just escaped.

Good that you try out the master version of the sql module! If you find other things or inconveniences, certainly let know!

@JohnSmizz
Copy link
Author

sure will do - i guess i need to post new bug/issue reports for each one?

i just updated the master sql module, looks like this issue has been resolved. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO SQL to_sql, read_sql, read_sql_query
Projects
None yet
Development

No branches or pull requests

2 participants