Skip to content

Commit 05ee112

Browse files
authored
BUG: read_sql reading duplicate tz aware columns (#53311)
* BUG: read_sql reading duplicate tz aware columns * Use isetitem * Move to postgres class
1 parent 43f1bc8 commit 05ee112

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

doc/source/whatsnew/v2.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ I/O
403403
- Bug in :func:`read_hdf` not properly closing store after a ``IndexError`` is raised (:issue:`52781`)
404404
- Bug in :func:`read_html`, style elements were read into DataFrames (:issue:`52197`)
405405
- Bug in :func:`read_html`, tail texts were removed together with elements containing ``display:none`` style (:issue:`51629`)
406+
- Bug in :func:`read_sql` when reading multiple timezone aware columns with the same column name (:issue:`44421`)
406407
- Bug when writing and reading empty Stata dta files where dtype information was lost (:issue:`46240`)
407408

408409
Period

pandas/io/sql.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ def _parse_date_columns(data_frame, parse_dates):
131131
# we want to coerce datetime64_tz dtypes for now to UTC
132132
# we could in theory do a 'nice' conversion from a FixedOffset tz
133133
# GH11216
134-
for col_name, df_col in data_frame.items():
134+
for i, (col_name, df_col) in enumerate(data_frame.items()):
135135
if isinstance(df_col.dtype, DatetimeTZDtype) or col_name in parse_dates:
136136
try:
137137
fmt = parse_dates[col_name]
138138
except TypeError:
139139
fmt = None
140-
data_frame[col_name] = _handle_date_column(df_col, format=fmt)
140+
data_frame.isetitem(i, _handle_date_column(df_col, format=fmt))
141141

142142
return data_frame
143143

pandas/tests/io/test_sql.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,6 +2890,44 @@ def test_schema_support(self):
28902890
res2 = pdsql.read_table("test_schema_other2")
28912891
tm.assert_frame_equal(res1, res2)
28922892

2893+
def test_self_join_date_columns(self):
2894+
# GH 44421
2895+
from sqlalchemy.engine import Engine
2896+
from sqlalchemy.sql import text
2897+
2898+
create_table = text(
2899+
"""
2900+
CREATE TABLE person
2901+
(
2902+
id serial constraint person_pkey primary key,
2903+
created_dt timestamp with time zone
2904+
);
2905+
2906+
INSERT INTO person
2907+
VALUES (1, '2021-01-01T00:00:00Z');
2908+
"""
2909+
)
2910+
if isinstance(self.conn, Engine):
2911+
with self.conn.connect() as con:
2912+
with con.begin():
2913+
con.execute(create_table)
2914+
else:
2915+
with self.conn.begin():
2916+
self.conn.execute(create_table)
2917+
2918+
sql_query = (
2919+
'SELECT * FROM "person" AS p1 INNER JOIN "person" AS p2 ON p1.id = p2.id;'
2920+
)
2921+
result = pd.read_sql(sql_query, self.conn)
2922+
expected = DataFrame(
2923+
[[1, Timestamp("2021", tz="UTC")] * 2], columns=["id", "created_dt"] * 2
2924+
)
2925+
tm.assert_frame_equal(result, expected)
2926+
2927+
# Cleanup
2928+
with sql.SQLDatabase(self.conn, need_transaction=True) as pandasSQL:
2929+
pandasSQL.drop_table("person")
2930+
28932931

28942932
# -----------------------------------------------------------------------------
28952933
# -- Test Sqlite / MySQL fallback

0 commit comments

Comments
 (0)