From f478f98e7160418ed38a05ddb287495bcf464db4 Mon Sep 17 00:00:00 2001 From: Mohit Deoli Date: Wed, 13 Aug 2025 22:34:10 +0530 Subject: [PATCH 1/3] DOC: document datetime/timedelta to PyObject conversion --- doc/source/reference/arrays.datetime.rst | 18 ++++++++++++++++++ numpy/_core/src/multiarray/_datetime.h | 1 + 2 files changed, 19 insertions(+) diff --git a/doc/source/reference/arrays.datetime.rst b/doc/source/reference/arrays.datetime.rst index 2095dc3aea49..9c53d1bbdb1e 100644 --- a/doc/source/reference/arrays.datetime.rst +++ b/doc/source/reference/arrays.datetime.rst @@ -309,6 +309,24 @@ 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: + +- For days or coarser, returns a datetime.date. +- 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 the string "NaT". +- For microseconds or coarser, returns a datetime.timedelta. +- For units finer than microseconds, returns an integer. + Business day functionality ========================== diff --git a/numpy/_core/src/multiarray/_datetime.h b/numpy/_core/src/multiarray/_datetime.h index 112c57433094..64284f550af4 100644 --- a/numpy/_core/src/multiarray/_datetime.h +++ b/numpy/_core/src/multiarray/_datetime.h @@ -243,6 +243,7 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj, * Converts a datetime into a PyObject *. * * For days or coarser, returns a datetime.date. + * For days or coarser, returns a datetime.date. * For microseconds or coarser, returns a datetime.datetime. * For units finer than microseconds, returns an integer. */ From 78e201c54a92b45a89b86629983094948095afe4 Mon Sep 17 00:00:00 2001 From: Mohit Deoli Date: Wed, 13 Aug 2025 22:51:35 +0530 Subject: [PATCH 2/3] DOC: document datetime/timedelta to PyObject conversion --- doc/source/reference/arrays.datetime.rst | 2 +- numpy/_core/src/multiarray/_datetime.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/reference/arrays.datetime.rst b/doc/source/reference/arrays.datetime.rst index 9c53d1bbdb1e..d14d99c39b44 100644 --- a/doc/source/reference/arrays.datetime.rst +++ b/doc/source/reference/arrays.datetime.rst @@ -316,7 +316,7 @@ NumPy follows a strict protocol when converting datetime and timedelta to Python For conversion of datetime to a Python Object: -- For days or coarser, returns a datetime.date. +- Not-a-time is returned as the string "NaT". - For days or coarser, returns a datetime.date. - For microseconds or coarser, returns a datetime.datetime. - For units finer than microseconds, returns an integer. diff --git a/numpy/_core/src/multiarray/_datetime.h b/numpy/_core/src/multiarray/_datetime.h index 64284f550af4..49745197dfab 100644 --- a/numpy/_core/src/multiarray/_datetime.h +++ b/numpy/_core/src/multiarray/_datetime.h @@ -242,7 +242,7 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj, /* * Converts a datetime into a PyObject *. * - * For days or coarser, returns a datetime.date. + * Not-a-time is returned as the string "NaT". * For days or coarser, returns a datetime.date. * For microseconds or coarser, returns a datetime.datetime. * For units finer than microseconds, returns an integer. From 21e460eef7c60f92d8726f22a18dae757adf67ba Mon Sep 17 00:00:00 2001 From: Mohit Deoli Date: Thu, 14 Aug 2025 00:53:50 +0530 Subject: [PATCH 3/3] DOC: document datetime/timedelta to PyObject conversion --- doc/source/reference/arrays.datetime.rst | 8 +++++--- numpy/_core/src/multiarray/_datetime.h | 6 +++--- numpy/_core/src/multiarray/datetime.c | 6 +++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/doc/source/reference/arrays.datetime.rst b/doc/source/reference/arrays.datetime.rst index d14d99c39b44..d2d68a26be3c 100644 --- a/doc/source/reference/arrays.datetime.rst +++ b/doc/source/reference/arrays.datetime.rst @@ -309,6 +309,7 @@ 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 ================================================== @@ -316,16 +317,17 @@ NumPy follows a strict protocol when converting datetime and timedelta to Python For conversion of datetime to a Python Object: -- 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. For conversion of timedelta to Python Object -- 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. + Business day functionality ========================== diff --git a/numpy/_core/src/multiarray/_datetime.h b/numpy/_core/src/multiarray/_datetime.h index 49745197dfab..7ef2c39023a1 100644 --- a/numpy/_core/src/multiarray/_datetime.h +++ b/numpy/_core/src/multiarray/_datetime.h @@ -242,7 +242,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. @@ -253,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); diff --git a/numpy/_core/src/multiarray/datetime.c b/numpy/_core/src/multiarray/datetime.c index 0dac36a0903b..f2f9e6405f9a 100644 --- a/numpy/_core/src/multiarray/datetime.c +++ b/numpy/_core/src/multiarray/datetime.c @@ -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. @@ -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)