Skip to content

BUG: Allow tz-aware Datetime SQL columns to be passed to parse_dates kwarg. #49506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 8 additions & 4 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,19 +1121,23 @@ def _harmonize_columns(self, parse_dates=None) -> None:
try:
df_col = self.frame[col_name]

# the type the dataframe column should have
col_type = self._get_dtype(sql_col.type)

# Handle date parsing upfront; don't try to convert columns
# twice
if col_name in parse_dates:
try:
fmt = parse_dates[col_name]
except TypeError:
fmt = None
self.frame[col_name] = _handle_date_column(df_col, format=fmt)
# Convert tz-aware Datetime SQL columns to UTC
utc = col_type is DatetimeTZDtype
self.frame[col_name] = _handle_date_column(
df_col, format=fmt, utc=utc
)
continue

# the type the dataframe column should have
col_type = self._get_dtype(sql_col.type)

if (
col_type is datetime
or col_type is date
Expand Down
8 changes: 3 additions & 5 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,7 @@ def check(col):
)

# GH11216
df = read_sql_query("select * from types", self.conn)
df = read_sql_table("types", self.conn)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm this test has been passing on our CI with postgres, and from #11216 it was an intentional choice to test read_sql_query instead of read_sql_table

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which platforms is it being tested on? I'm working on Windows, and it could be that something about my setup is preventing the timezone information from getting into the pandas DataFrame. I could try switching to Ubuntu.

Copy link
Member

@mroeschke mroeschke Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that could be it. This SQL test runs on Ubuntu + Postgres. (GIthub Actions only supports containers, i.e. postgres image, with Ubuntu)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out that the test failure was due to the postgres database timezone setting, not the platform. To get it to pass on either Windows or Ubuntu, I needed to change the database timezone to UTC. If there is an issue to be addressed when the timezone is not UTC, I'm not sure how best to handle it, so I will stop working on this PR.

if not hasattr(df, "DateColWithTz"):
request.node.add_marker(
pytest.mark.xfail(reason="no column with datetime with time zone")
Expand All @@ -1793,9 +1793,7 @@ def check(col):
col = df.DateColWithTz
assert is_datetime64tz_dtype(col.dtype)

df = read_sql_query(
"select * from types", self.conn, parse_dates=["DateColWithTz"]
)
df = read_sql_table("types", self.conn, parse_dates=["DateColWithTz"])
if not hasattr(df, "DateColWithTz"):
request.node.add_marker(
pytest.mark.xfail(reason="no column with datetime with time zone")
Expand All @@ -1806,7 +1804,7 @@ def check(col):
check(df.DateColWithTz)

df = concat(
list(read_sql_query("select * from types", self.conn, chunksize=1)),
list(read_sql_table("types", self.conn, chunksize=1)),
ignore_index=True,
)
col = df.DateColWithTz
Expand Down