Skip to content

Commit 60c1a1d

Browse files
Merge pull request #10803 from IamGianluca/fix_issue_10750
BUG: Allow read_sql_table to read from views
2 parents 25be2f2 + 113b1ad commit 60c1a1d

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v0.17.0.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ Other enhancements
142142
- ``pd.merge`` will now allow duplicate column names if they are not merged upon (:issue:`10639`).
143143

144144
- ``pd.pivot`` will now allow passing index as ``None`` (:issue:`3962`).
145+
146+
- ``read_sql_table`` will now allow reading from views (:issue:`10750`).
147+
145148
- ``drop_duplicates`` and ``duplicated`` now accept ``keep`` keyword to target first, last, and all duplicates. ``take_last`` keyword is deprecated, see :ref:`deprecations <whatsnew_0170.deprecations>` (:issue:`6511`, :issue:`8505`)
146149

147150
.. ipython :: python

pandas/io/sql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def read_sql_table(table_name, con, schema=None, index_col=None,
337337
from sqlalchemy.schema import MetaData
338338
meta = MetaData(con, schema=schema)
339339
try:
340-
meta.reflect(only=[table_name])
340+
meta.reflect(only=[table_name], views=True)
341341
except sqlalchemy.exc.InvalidRequestError:
342342
raise ValueError("Table %s not found" % table_name)
343343

pandas/io/tests/test_sql.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@
161161
SELECT * FROM iris WHERE
162162
"Name"=%(name)s AND "SepalLength"=%(length)s
163163
"""
164+
},
165+
'create_view': {
166+
'sqlite': """
167+
CREATE VIEW iris_view AS
168+
SELECT * FROM iris
169+
"""
164170
}
165171
}
166172

@@ -244,6 +250,10 @@ def _load_iris_data(self):
244250
for row in r:
245251
self._get_exec().execute(ins, row)
246252

253+
def _load_iris_view(self):
254+
self.drop_table('iris_view')
255+
self._get_exec().execute(SQL_STRINGS['create_view'][self.flavor])
256+
247257
def _check_iris_loaded_frame(self, iris_frame):
248258
pytype = iris_frame.dtypes[0].type
249259
row = iris_frame.iloc[0]
@@ -482,6 +492,7 @@ class _TestSQLApi(PandasSQLTest):
482492
def setUp(self):
483493
self.conn = self.connect()
484494
self._load_iris_data()
495+
self._load_iris_view()
485496
self._load_test1_data()
486497
self._load_test2_data()
487498
self._load_test3_data()
@@ -492,6 +503,11 @@ def test_read_sql_iris(self):
492503
"SELECT * FROM iris", self.conn)
493504
self._check_iris_loaded_frame(iris_frame)
494505

506+
def test_read_sql_view(self):
507+
iris_frame = sql.read_sql_query(
508+
"SELECT * FROM iris_view", self.conn)
509+
self._check_iris_loaded_frame(iris_frame)
510+
495511
def test_legacy_read_frame(self):
496512
with tm.assert_produces_warning(FutureWarning):
497513
iris_frame = sql.read_frame(

0 commit comments

Comments
 (0)