Skip to content

Fix #59772: tz_aware NaT raises exception on to_numpy #61302

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ ExtensionArray
- Bug in :class:`Categorical` when constructing with an :class:`Index` with :class:`ArrowDtype` (:issue:`60563`)
- Bug in :meth:`.arrays.ArrowExtensionArray.__setitem__` which caused wrong behavior when using an integer array with repeated values as a key (:issue:`58530`)
- Bug in :meth:`ArrowExtensionArray.factorize` where NA values were dropped when input was dictionary-encoded even when dropna was set to False(:issue:`60567`)
- Bug in :meth:`ExtensionArray.to_numpy` raising ``TypeError`` on a tz-aware series with ``NaT`` (:issue:`59772`)
- Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`)
- Bug in comparison between object with :class:`ArrowDtype` and incompatible-dtyped (e.g. string vs bool) incorrectly raising instead of returning all-``False`` (for ``==``) or all-``True`` (for ``!=``) (:issue:`59505`)
- Bug in constructing pandas data structures when passing into ``dtype`` a string of the type followed by ``[pyarrow]`` while PyArrow is not installed would raise ``NameError`` rather than ``ImportError`` (:issue:`57928`)
Expand Down
12 changes: 11 additions & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,17 @@ def to_numpy(
-------
numpy.ndarray
"""
result = np.asarray(self, dtype=dtype)
if dtype == "datetime64" and self.dtype.kind == "M":
Copy link
Member

Choose a reason for hiding this comment

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

This fix is too specific. I think the TypeError raised in the original issue should be root caused first.

# Make sure NaT is not tz_aware
result = np.array(
[
np.datetime64("NaT", "s") if isna(x) else x.tz_localize(None).asm8
for x in self
],
dtype="datetime64[s]",
)
else:
result = np.asarray(self, dtype=dtype)
if copy or na_value is not lib.no_default:
result = result.copy()
if na_value is not lib.no_default:
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,3 +1356,10 @@ def test_from_pandas_array(dtype):
result = idx_cls(arr)
expected = idx_cls(data)
tm.assert_index_equal(result, expected)


def test_to_numpy_with_NaT_tz_aware():
# GH#59772
result = pd.Series(NaT).dt.tz_localize("UTC").to_numpy("datetime64")
expected = pd.Series(NaT).to_numpy("datetime64")
tm.assert_equal(result, expected)
Loading