Skip to content

DOC: Document datetime and timedelta to python's object type conversion #29557

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 13 commits into
base: main
Choose a base branch
from

Conversation

devmt04
Copy link

@devmt04 devmt04 commented Aug 13, 2025

The need to document datetime and timedelta to Python object type conversions arose during a discussion in issue #29516

NumPy follows a strict type conversion protocol when converting datetime64 or timedelta64 to standard Python objects. The rules are as follows:

Conversion of datetime64 to Python objects

  • Not-a-time is returned as None.
  • For days or coarser, returns a datetime.date.
  • For microseconds or coarser, returns a datetime.datetime.
  • For units finer than microseconds, returns an integer.

Conversion of timedelta64 to Python objects

  • Not-a-time is returned as None.
  • For microseconds or coarser, returns a datetime.timedelta.
  • For Y/M/B (nonlinear units), generic units and units finer than microseconds, returns an integer.

Currently, this behavior is not clearly documented, which can lead to ambiguity for end users, forcing them to infer the conversion rules through experimentation.

@InessaPawson InessaPawson added the triage review Issue/PR to be discussed at the next triage meeting label Aug 20, 2025
@InessaPawson InessaPawson moved this to Awaiting a code review in NumPy first-time contributor PRs Aug 20, 2025
@InessaPawson InessaPawson removed the triage review Issue/PR to be discussed at the next triage meeting label Aug 20, 2025
@InessaPawson InessaPawson moved this from Awaiting a code review to Pending authors' response in NumPy first-time contributor PRs Aug 20, 2025
Copy link
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

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

While you're at it, I still think adding a new section that describes what NaT is is worth doing. But this is fine to merge without that. It looks much better with the table and code examples!

Maybe @rossbar is interested in looking this over from a documentation editing point of view?

@devmt04 devmt04 force-pushed the docs-datetime-pyobject-conversion branch from 0098596 to cb016cd Compare August 21, 2025 20:02
Copy link
Contributor

@tylerjereddy tylerjereddy left a comment

Choose a reason for hiding this comment

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

Seems useful to me as well, I learned some stuff--I think I found one small typo when reading it over.

Converting datetime and timedelta to Python Object
==================================================

NumPy follows a strict protocol when converting `datetime64` and/or `timedelta64` to Python Objects (e.g., ``tuple``, ``list``, `datetime.datetime`).
Copy link
Member

Choose a reason for hiding this comment

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

The cases where .item() returns a tuple or list are pretty rare in practice, so if you need examples, then I'd go with more common python types like float or str.

Copy link
Author

Choose a reason for hiding this comment

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

I just get to know that for python types like str, int, float it returns np.str_, np.int64 and np.float64. I guess we should stick with examples like tuple, list, and datetime.datetime and explicitly mention the behavior with types like str, int, and float.

Copy link
Author

Choose a reason for hiding this comment

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

for python types like str, int, float it returns np.str_, np.int64 and np.float64.

Can this be seen as a bug? Since it should be expected to convert them to Python objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Pending authors' response
Development

Successfully merging this pull request may close these issues.

5 participants