Skip to content

Commit 291acfb

Browse files
Backport PR #53118 on branch 2.0.x (REGR: read_sql dropping duplicated columns) (#53136)
Backport PR #53118: REGR: read_sql dropping duplicated columns Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 44f0a9b commit 291acfb

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v2.0.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ including other versions of pandas.
1313

1414
Fixed regressions
1515
~~~~~~~~~~~~~~~~~
16+
- Fixed regression in :func:`read_sql` dropping columns with duplicated column names (:issue:`53117`)
1617
- Fixed regression in :meth:`DataFrame.loc` losing :class:`MultiIndex` name when enlarging object (:issue:`53053`)
1718
- Fixed regression in :meth:`DataFrame.to_string` printing a backslash at the end of the first row of data, instead of headers, when the DataFrame doesn't fit the line width (:issue:`53054`)
1819
- Fixed regression in :meth:`MultiIndex.join` returning levels in wrong order (:issue:`53093`)

pandas/io/sql.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ def _convert_arrays_to_dataframe(
158158
ArrowExtensionArray(pa.array(arr, from_pandas=True)) for arr in arrays
159159
]
160160
if arrays:
161-
return DataFrame(dict(zip(columns, arrays)))
161+
df = DataFrame(dict(zip(list(range(len(columns))), arrays)))
162+
df.columns = columns
163+
return df
162164
else:
163165
return DataFrame(columns=columns)
164166

pandas/tests/io/test_sql.py

+12
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,18 @@ def test_escaped_table_name(self):
14961496

14971497
tm.assert_frame_equal(res, df)
14981498

1499+
def test_read_sql_duplicate_columns(self):
1500+
# GH#53117
1501+
df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3], "c": 1})
1502+
df.to_sql("test_table", self.conn, index=False)
1503+
1504+
result = pd.read_sql("SELECT a, b, a +1 as a, c FROM test_table;", self.conn)
1505+
expected = DataFrame(
1506+
[[1, 0.1, 2, 1], [2, 0.2, 3, 1], [3, 0.3, 4, 1]],
1507+
columns=["a", "b", "a", "c"],
1508+
)
1509+
tm.assert_frame_equal(result, expected)
1510+
14991511

15001512
@pytest.mark.skipif(not SQLALCHEMY_INSTALLED, reason="SQLAlchemy not installed")
15011513
class TestSQLApi(SQLAlchemyMixIn, _TestSQLApi):

0 commit comments

Comments
 (0)