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

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

wants to merge 2 commits into from

Conversation

cdcadman
Copy link
Contributor

@cdcadman cdcadman commented Nov 3, 2022

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

This PR is related to existing tests which fail for me on the main branch, but they pass after making these changes.

FAILED pandas/tests/io/test_sql.py::TestPostgreSQLAlchemy::test_datetime_with_timezone - AssertionError: assert False
FAILED pandas/tests/io/test_sql.py::TestPostgreSQLAlchemyConn::test_datetime_with_timezone - AssertionError: assert False

Both tests fail here:

        # this is parsed on Travis (linux), but not on macosx for some reason
        # even with the same versions of psycopg2 & sqlalchemy, possibly a
        # Postgresql server version difference
        col = df.DateColWithTz
>       assert is_datetime64tz_dtype(col.dtype)
E       AssertionError: assert False
E        +  where False = is_datetime64tz_dtype(dtype('O'))
E        +    where dtype('O') = 0    2000-01-01 00:00:00-08:00\n1    2000-06-01 00:00:00-07:00\nName: DateColWithTz, dtype: object.dtype

pandas\tests\io\test_sql.py:1794: AssertionError

@@ -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.

@mroeschke mroeschke added the IO SQL to_sql, read_sql, read_sql_query label Nov 3, 2022
@cdcadman
Copy link
Contributor Author

cdcadman commented Nov 3, 2022

I will set the Postresql timezone to UTC so that these tests can pass.

@cdcadman cdcadman closed this Nov 3, 2022
@cdcadman cdcadman deleted the timezone_aware_field branch November 15, 2022 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO SQL to_sql, read_sql, read_sql_query
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants