You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Python] Incorrect conversion from datetime.datetime and datetime.time objects with non-UTC timezones #40122
Describe the bug, including details regarding any error messages, version, and platform.
I may be missing something fundamental, but Pyarrow seems to improperly handle the conversion from datetime.datetime and datetime.time objects with non-UTC timezones.
Datetimes
This Python datetime is the turn of the millenium in UTC+1:
I think everything is behaving as expected, but the repr is confusing.
Datetimes
...
Converting to pyarrow makes it 11 PM in UTC+1, which is a different time (not just the same time in a different coordinate system):
So it actually did convert it to 11 PM in UTC (the UTC value is what is stored under the hood), and thus still representing midnight in UTC+1.
One might expect wall-time relative to the type in the repr, but so the repr is showing the UTC values, which is causing the confusion.
In the latest pyarrow 15.0, we improved the repr a little bit by adding "Z" as an indicator that what you see are UTC values:
In [4]: a
Out[4]:
<pyarrow.lib.TimestampArray object at 0x7f2fe814ebc0>
[
1999-12-31 23:00:00.000000Z
]
In [5]: a.type
Out[5]: TimestampType(timestamp[us, tz=+01:00])
Times
Creating a pyarrow array from a datetime.time strips timezone info:
The Time type in Arrow format does not have a notion of timezones (see fatbuffer spec), and pyarrow's conversion code for python->arrow currently just ignores the timezone.
This is not ideal, and maybe it would be better if pyarrow just raised an error instead? (indicating it doesn't support datetime.time objects with a timezone?)
Localized timestamps will currently be returned as UTC (pandas’s native representation). Timezone-naive data will be implicitly interpreted as UTC.
Hmm, that note seems quite outdated (also, it is about timestamps, not about times .. although it appears in the Time64Array doc page, but that's because this from_pandas method is implemented on the base class and thus has the same docstring for all concrete Array subclasses).
changed the title [-]Incorrect conversion from datetime.datetime and datetime.time objects with non-UTC timezones[/-][+][Python] Incorrect conversion from datetime.datetime and datetime.time objects with non-UTC timezones[/+]on Feb 20, 2024
That note seems to have been added almost at the beginning of the project (#287), and since then quite some changes were made to timezone handling.
(localized timestamps are not exactly returned as UTC, but the timezone information is preserved; and for timezone-naive data there is an equivalent timestamp without timezone type in Arrow ..)
Makes sense! You're right that the datetime.datetimes are all the same between base Python, pandas, Arrow and base R, they're just displayed differently. I definitely prefer base Python and pandas's display to Arrow's though, maybe version 16 could switch? ;)
For the times, I would definitely raise an error on datetime.time objects with a non-default timezone, it seems quite dangerous to truncate the timezone silently.
Activity
jorisvandenbossche commentedon Feb 20, 2024
I think everything is behaving as expected, but the repr is confusing.
So it actually did convert it to 11 PM in UTC (the UTC value is what is stored under the hood), and thus still representing midnight in UTC+1.
One might expect wall-time relative to the type in the repr, but so the repr is showing the UTC values, which is causing the confusion.
In the latest pyarrow 15.0, we improved the repr a little bit by adding "Z" as an indicator that what you see are UTC values:
The Time type in Arrow format does not have a notion of timezones (see fatbuffer spec), and pyarrow's conversion code for python->arrow currently just ignores the timezone.
This is not ideal, and maybe it would be better if pyarrow just raised an error instead? (indicating it doesn't support
datetime.timeobjects with a timezone?)Hmm, that note seems quite outdated (also, it is about timestamps, not about times .. although it appears in the Time64Array doc page, but that's because this
from_pandasmethod is implemented on the base class and thus has the same docstring for all concrete Array subclasses).[-]Incorrect conversion from datetime.datetime and datetime.time objects with non-UTC timezones[/-][+][Python] Incorrect conversion from datetime.datetime and datetime.time objects with non-UTC timezones[/+]jorisvandenbossche commentedon Feb 20, 2024
That note seems to have been added almost at the beginning of the project (#287), and since then quite some changes were made to timezone handling.
(localized timestamps are not exactly returned as UTC, but the timezone information is preserved; and for timezone-naive data there is an equivalent timestamp without timezone type in Arrow ..)
Wainberg commentedon Feb 22, 2024
Makes sense! You're right that the datetime.datetimes are all the same between base Python, pandas, Arrow and base R, they're just displayed differently. I definitely prefer base Python and pandas's display to Arrow's though, maybe version 16 could switch? ;)
For the times, I would definitely raise an error on datetime.time objects with a non-default timezone, it seems quite dangerous to truncate the timezone silently.