Skip to content

REGR: DatetimeTZDtype __from_arrow__ interprets UTC values as wall time #56922

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

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ def __from_arrow__(self, array: pa.Array | pa.ChunkedArray) -> DatetimeArray:
else:
np_arr = array.to_numpy()

return DatetimeArray._from_sequence(np_arr, dtype=self, copy=False)
return DatetimeArray._simple_new(np_arr, dtype=self)

def __setstate__(self, state) -> None:
# for pickle compat. __get_state__ is defined in the
Expand Down
14 changes: 13 additions & 1 deletion pandas/tests/arrays/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pandas.core.dtypes.dtypes import DatetimeTZDtype

import pandas as pd
from pandas import Series
import pandas._testing as tm
from pandas.core.arrays import DatetimeArray

Expand Down Expand Up @@ -232,7 +233,7 @@ def test_from_arrowtest_from_arrow_with_different_units_and_timezones_with_(
dtype = DatetimeTZDtype(unit=pd_unit, tz=pd_tz)

result = dtype.__from_arrow__(arr)
expected = DatetimeArray._from_sequence(
expected = DatetimeArray._simple_new(
Copy link
Member Author

@lithomas1 lithomas1 Jan 17, 2024

Choose a reason for hiding this comment

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

I think this was changed from the regular DatetimeArray constructor to DatetimeArray._from_sequence when the deprecation was done.

If this patch is correct, than that would've been wrong and _simple_new should also be the correct replacement here.

Copy link
Member

Choose a reason for hiding this comment

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

Ideally we would use a different constructor, I think (maybe just pd.array(..., dtype=dtype)?), because otherwise we are testing that __from_arrow__ which uses _simple_new gives the same result as _simple_new ... (which was now happening with _from_sequence)

Copy link
Member

Choose a reason for hiding this comment

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

Good point. Could do something like DatetimeArray._from_sequence(int64_data, dtype=f"M8[{unit}]").tz_localize("UTC").astype(self, copy=False)

Once pyarrow is required we can also make DTA._from_sequence Just Work with the pyarrow object as input

np.array(data, dtype=f"datetime64[{pa_unit}]").astype(f"datetime64[{pd_unit}]"),
dtype=dtype,
)
Expand All @@ -242,6 +243,17 @@ def test_from_arrowtest_from_arrow_with_different_units_and_timezones_with_(
tm.assert_extension_array_equal(result, expected)


def test_datetimetz_from_arrow_roundtrip():
# GH 56775
pa = pytest.importorskip("pyarrow")

ser = Series(["2012-01-01", "2012-01-02"], dtype="datetime64[ns, Europe/Brussels]")

result = ser.dtype.__from_arrow__(pa.array(ser))
expected = DatetimeArray._from_sequence(ser)
tm.assert_extension_array_equal(result, expected)


@pytest.mark.parametrize(
("unit", "tz"),
[
Expand Down