Skip to content

Commit 01b42ce

Browse files
author
Artemy Kolchinsky
committed
BUG: Should allow numeric mysql table/column names
Doc fix
1 parent f4da4b9 commit 01b42ce

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

doc/source/whatsnew/v0.16.2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,5 @@ Bug Fixes
8888
- Bug where infer_freq infers timerule (WOM-5XXX) unsupported by to_offset (:issue:`9425`)
8989

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

pandas/io/sql.py

-2
Original file line numberDiff line numberDiff line change
@@ -1283,8 +1283,6 @@ def _get_valid_mysql_name(name):
12831283
if not re.match(basere, c):
12841284
if not (0x80 < ord(c) < 0xFFFF):
12851285
raise ValueError("Invalid MySQL identifier '%s'" % uname)
1286-
if not re.match(r'[^0-9]', uname):
1287-
raise ValueError('MySQL identifier cannot be entirely numeric')
12881286

12891287
return '`' + uname + '`'
12901288

pandas/io/tests/test_sql.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,8 @@ def test_illegal_names(self):
17381738

17391739
for ndx, weird_name in enumerate(['test_weird_name]','test_weird_name[',
17401740
'test_weird_name`','test_weird_name"', 'test_weird_name\'',
1741-
'_b.test_weird_name_01-30', '"_b.test_weird_name_01-30"']):
1741+
'_b.test_weird_name_01-30', '"_b.test_weird_name_01-30"',
1742+
'12345','12345blah']):
17421743
df.to_sql(weird_name, self.conn, flavor=self.flavor)
17431744
sql.table_exists(weird_name, self.conn)
17441745

@@ -1839,16 +1840,30 @@ def test_to_sql_save_index(self):
18391840
self._to_sql_save_index()
18401841

18411842
def test_illegal_names(self):
1843+
df = DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])
1844+
1845+
# These tables and columns should be ok
1846+
for ndx, ok_name in enumerate(['99beginswithnumber','12345']):
1847+
df.to_sql(ok_name, self.conn, flavor=self.flavor, index=False,
1848+
if_exists='replace')
1849+
self.conn.cursor().execute("DROP TABLE `%s`" % ok_name)
1850+
self.conn.commit()
1851+
df2 = DataFrame([[1, 2], [3, 4]], columns=['a', ok_name])
1852+
c_tbl = 'test_ok_col_name%d'%ndx
1853+
df2.to_sql(c_tbl, self.conn, flavor=self.flavor, index=False,
1854+
if_exists='replace')
1855+
self.conn.cursor().execute("DROP TABLE `%s`" % c_tbl)
1856+
self.conn.commit()
1857+
18421858
# For MySQL, these should raise ValueError
18431859
for ndx, illegal_name in enumerate(['test_illegal_name]','test_illegal_name[',
18441860
'test_illegal_name`','test_illegal_name"', 'test_illegal_name\'', '']):
1845-
df = DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])
18461861
self.assertRaises(ValueError, df.to_sql, illegal_name, self.conn,
18471862
flavor=self.flavor, index=False)
18481863

18491864
df2 = DataFrame([[1, 2], [3, 4]], columns=['a', illegal_name])
18501865
c_tbl = 'test_illegal_col_name%d'%ndx
1851-
self.assertRaises(ValueError, df2.to_sql, 'test_illegal_col_name',
1866+
self.assertRaises(ValueError, df2.to_sql, c_tbl,
18521867
self.conn, flavor=self.flavor, index=False)
18531868

18541869

0 commit comments

Comments
 (0)