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

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
@@ -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.

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