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


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