'SQL datetime CET to Python becomes UTC

In my SQL database I have a date column with the type `datetimeoffset(0)', where the entries looks like this:

2022-01-14 23:00:00 +01:00

Which is in CET time.

Then when I try to load this into a Pandas data frame with:

    year = 2022
    month = 1

    columns = "date"
    table = "[dbo].[MyTable]"
    conditions = f"YEAR(date) = '{year}' AND MONTH(date) = '{month}'"

    sql_query = f"SELECT {columns} FROM {table} WHERE {conditions}"

    df = pd.read_sql(sql_query, connection)

So I try to load all datetimes within a year and month so I get all the hours within this month.

The resulting dtype of the data frame, and in particular the column with this date is:

date     datetime64[ns, UTC]

Hence, it is now UTC. And I can see that it has been transformed to +00:00. I could probably convert it back to CET when I have it in my data frame, but it's always nice to have everything as clean as possible when you load it from SQL.

So is this a Pandas problem, or an SQL problem, and can it even be solved without having to convert to CET again when I have it Pandas data frame form ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source