Skip to content

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

Open
natmokval wants to merge 2 commits into
pandas-dev:mainfrom
natmokval:BUG-tz-aware-series-with-NaT-raises-exception-on-to_numpy-for-datetime64
Open

BUG: tz-aware series with NaT raises exception on .to_numpy("datetime64")#66529
natmokval wants to merge 2 commits into
pandas-dev:mainfrom
natmokval:BUG-tz-aware-series-with-NaT-raises-exception-on-to_numpy-for-datetime64

Conversation

@natmokval

Copy link
Copy Markdown
Contributor

if dtype is not None:
dtype = np.dtype(dtype)
if lib.is_np_dtype(dtype, "M") and is_unitless(dtype):
dtype = self._ndarray.dtype

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

wouldn't we usually raise here and ask the user to be more specific?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@jbrockmendel, thank you for reviewing my PR.

Currently, on main, we raiseTypeError: 'float' object cannot be interpreted as an integer

Do you mean we should instead raise:

TypeError(
    "Casting to unit-less dtype 'datetime64' is not supported. "
    "Pass e.g. 'datetime64[ns]' instead."
)

?
As I understand it, we want pd.Series(pd.NaT).dt.tz_localize("UTC").to_numpy("datetime64") to give the same result as pd.Series(pd.NaT).to_numpy("datetime64") rather than raising an error.

I followed the comment in the issue and assumed that pd.Series(pd.NaT).dt.tz_localize("UTC").to_numpy("datetime64[ns]") should work both with and without [ns]. Maybe I am wrong and the comment is no longer relevant.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

id rather this raise and tell users to be more specific, but if it is allowed for some cases at the moment that would need a deprecation. if you feel strongly it should be allowed, im not inclined to argue the point

@natmokval natmokval Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Consistency in behavior is important. I think both pd.Series(pd.NaT).dt.tz_localize("UTC").to_numpy("datetime64") and pd.Series(pd.NaT).to_numpy("datetime64") should either raise or give ['NaT'].

Because we already disallow the unit-less "datetime64" dtype in .astype(), it seems reasonable to do the same here. Since the unit-less dtype is currently allowed for pd.Series(pd.NaT).to_numpy("datetime64"), this would be a deprecation.


def test_to_numpy_tzaware_nat_unitless_dtype():
# GH#59772
result = Series(NaT).dt.tz_localize("UTC").to_numpy("datetime64")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we dont allow .astype("datetime64[ns]"); is it obvious we should be allowing to_numpy like this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I see now that this is inconsistent with astype("datetime64[ns]") behaviour. I will correct to_numpy to raise here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not saying its obvious this should raise, just that its something to discuss. What is your opinion?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Personally, I prefer requiring users to specify the unit explicitly rather than setting it by default in the unit-less case. This can help avoid misunderstandings and unexpected results. Therefore, I am in favor of deprecating unit-less "datetime64" in to_numpy.

Comment thread pandas/core/arrays/datetimes.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Timezones Timezone data dtype

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