Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.16.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@ Bug Fixes
- Bug where infer_freq infers timerule (WOM-5XXX) unsupported by to_offset (:issue:`9425`)

- Bug to handle masking empty ``DataFrame``(:issue:`10126`)
- Bug where MySQL interface could not handle numeric table/column names (:issue:`10255`)

2 changes: 0 additions & 2 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,8 +1283,6 @@ def _get_valid_mysql_name(name):
if not re.match(basere, c):
if not (0x80 < ord(c) < 0xFFFF):
raise ValueError("Invalid MySQL identifier '%s'" % uname)
if not re.match(r'[^0-9]', uname):
raise ValueError('MySQL identifier cannot be entirely numeric')

return '`' + uname + '`'

Expand Down
21 changes: 18 additions & 3 deletions pandas/io/tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1738,7 +1738,8 @@ def test_illegal_names(self):

for ndx, weird_name in enumerate(['test_weird_name]','test_weird_name[',
'test_weird_name`','test_weird_name"', 'test_weird_name\'',
'_b.test_weird_name_01-30', '"_b.test_weird_name_01-30"']):
'_b.test_weird_name_01-30', '"_b.test_weird_name_01-30"',
'12345','12345blah']):
df.to_sql(weird_name, self.conn, flavor=self.flavor)
sql.table_exists(weird_name, self.conn)

Expand Down Expand Up @@ -1839,16 +1840,30 @@ def test_to_sql_save_index(self):
self._to_sql_save_index()

def test_illegal_names(self):
df = DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])

# These tables and columns should be ok
for ndx, ok_name in enumerate(['99beginswithnumber','12345']):
df.to_sql(ok_name, self.conn, flavor=self.flavor, index=False,
if_exists='replace')
self.conn.cursor().execute("DROP TABLE `%s`" % ok_name)
self.conn.commit()
df2 = DataFrame([[1, 2], [3, 4]], columns=['a', ok_name])
c_tbl = 'test_ok_col_name%d'%ndx
df2.to_sql(c_tbl, self.conn, flavor=self.flavor, index=False,
if_exists='replace')
self.conn.cursor().execute("DROP TABLE `%s`" % c_tbl)
self.conn.commit()

# For MySQL, these should raise ValueError
for ndx, illegal_name in enumerate(['test_illegal_name]','test_illegal_name[',
'test_illegal_name`','test_illegal_name"', 'test_illegal_name\'', '']):
df = DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])
self.assertRaises(ValueError, df.to_sql, illegal_name, self.conn,
flavor=self.flavor, index=False)

df2 = DataFrame([[1, 2], [3, 4]], columns=['a', illegal_name])
c_tbl = 'test_illegal_col_name%d'%ndx
self.assertRaises(ValueError, df2.to_sql, 'test_illegal_col_name',
self.assertRaises(ValueError, df2.to_sql, c_tbl,
self.conn, flavor=self.flavor, index=False)


Expand Down