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 5 commits 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
20 changes: 20 additions & 0 deletions doc/source/reference/arrays.datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,26 @@ us / μs microsecond +/- 2.9e5 years [290301 BC, 294241 AD]
as attosecond +/- 9.2 seconds [ 1969 AD, 1970 AD]
======== ================ ======================= ==========================


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

NumPy follows a strict protocol when converting datetime and timedelta to Python Objects (e.g., tuple, list, datetime.datetime).

For conversion of datetime to a Python Object:

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

For conversion of timedelta to Python Object

- 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.
Copy link
Member

Choose a reason for hiding this comment

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

It might be easier to read and digest this as a table. I'd also like to see some code examples to explicitly show what happens. The nice part about code examples is they get run as part of our test suite.

Instead of "Not-a-time" I think we usually refer to it as NaT.

Copy link
Member

@ngoldbaum ngoldbaum Aug 20, 2025

Choose a reason for hiding this comment

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

I think also adding some text this page explaining what NaT is would be helpful. We use it in a few places above, but we never explicitly explain it's the datetime equivalent of NaN and that NaT is short for not-a-time.

Copy link
Author

Choose a reason for hiding this comment

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

but we never explicitly explain it's the datetime equivalent of NaN and that NaT is short for not-a-time.

I think we should explain NaT explicitly under title "Datetime units", as we already had given an overview of it under "Basic datetimes".

Copy link
Author

Choose a reason for hiding this comment

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

Also I have noticed a unknown non-linear timedelta type "B" mentioned in C code comments, is that a thing in NumPy? As I found no reference of it in docs.

For conversion of timedelta to Python Object

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

All I am aware of is that "B" is one of the Period aliases in Pandas. Is this a mistake?



Business day functionality
==========================

Expand Down
5 changes: 3 additions & 2 deletions numpy/_core/src/multiarray/_datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj,
/*
* Converts a datetime into a PyObject *.
*
* 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.
Expand All @@ -252,9 +253,9 @@ convert_datetime_to_pyobject(npy_datetime dt, PyArray_DatetimeMetaData *meta);
/*
* Converts a timedelta into a PyObject *.
*
* Not-a-time is returned as the string "NaT".
* Not-a-time is returned as None.
* For microseconds or coarser, returns a datetime.timedelta.
* For units finer than microseconds, returns an integer.
* For Y/M/B (nonlinear units), generic units and units finer than microseconds, returns an integer.
*/
NPY_NO_EXPORT PyObject *
convert_timedelta_to_pyobject(npy_timedelta td, PyArray_DatetimeMetaData *meta);
Expand Down
6 changes: 3 additions & 3 deletions numpy/_core/src/multiarray/datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2761,7 +2761,7 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj,
/*
* Converts a datetime into a PyObject *.
*
* Not-a-time is returned as the string "NaT".
* 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.
Expand Down Expand Up @@ -2945,9 +2945,9 @@ convert_timedelta_to_timedeltastruct(PyArray_DatetimeMetaData *meta,
/*
* Converts a timedelta into a PyObject *.
*
* Not-a-time is returned as the string "NaT".
* Not-a-time is returned as None.
* For microseconds or coarser, returns a datetime.timedelta.
* For units finer than microseconds, returns an integer.
* For Y/M/B (nonlinear units), generic units and units finer than microseconds, returns an integer.
*/
NPY_NO_EXPORT PyObject *
convert_timedelta_to_pyobject(npy_timedelta td, PyArray_DatetimeMetaData *meta)
Expand Down
Loading