Skip to content

Conversation

tomasmacieira
Copy link

Fix error when converting tz-aware Series with NaT to NumPy array

Previously, converting a Series of timezone-aware pd.NaT to a NumPy array using
.to_numpy("datetime64[ns]") would raise an exception.
It would happen because it could not be converted to datetime64 as a tz-aware value.
This is now fixed by removing the timezone localization from NaT values before the conversion.

Pandas series with NaT (not a time) localized, raised an exception when
converting to a numpy array
Fixed by removing localization from the NaT's
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.

@mroeschke
Copy link
Member

Thanks for the pull request, but it appears to have gone stale. If interested in continuing, please merge in the main branch, address any review comments and/or failing tests, and we can reopen.

@mroeschke mroeschke closed this May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: tz-aware series with NaT raises exception on .to_numpy("datetime64")

2 participants