Skip to content

np.array constructor can convert PyDateTime to np.datetime64 #111

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

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 12 additions & 0 deletions numpy/core/src/multiarray/_datetime.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#ifndef _NPY_PRIVATE__DATETIME_H_
#define _NPY_PRIVATE__DATETIME_H_

/*
* Set to non-zero to make np.array() automatically convert python datetime.date
* and datetime.datetime to np.datetime64.
*/
#define NPY_ARRAY_AUTOCONVERT_PYDATETIME 0

NPY_NO_EXPORT char *_datetime_strings[NPY_DATETIME_NUMUNITS];

NPY_NO_EXPORT int _days_per_month_table[2][12];
Expand All @@ -20,6 +26,12 @@ is_leapyear(npy_int64 year);
NPY_NO_EXPORT npy_int64
get_datetimestruct_days(const npy_datetimestruct *dts);

/*
* Gets a descr for a Date or Datetime
*/
NPY_NO_EXPORT PyArray_Descr *
get_datetime_dtype_for_obj(PyObject *obj);

/*
* Creates a datetime or timedelta dtype using a copy of the provided metadata.
*/
Expand Down
12 changes: 12 additions & 0 deletions numpy/core/src/multiarray/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
#include "common.h"
#include "buffer.h"

#include "_datetime.h"

NPY_NO_EXPORT PyArray_Descr *
_array_find_python_scalar_type(PyObject *op)
{
#if NPY_ARRAY_AUTOCONVERT_PYDATETIME
PyArray_Descr *res;
#endif
if (PyFloat_Check(op)) {
return PyArray_DescrFromType(PyArray_DOUBLE);
}
Expand All @@ -41,6 +45,14 @@ _array_find_python_scalar_type(PyObject *op)
}
return PyArray_DescrFromType(PyArray_LONGLONG);
}
#if NPY_ARRAY_AUTOCONVERT_PYDATETIME
else {
res = get_datetime_dtype_for_obj(op);
if (res != NULL) {
return res;
}
}
#endif
return NULL;
}

Expand Down
30 changes: 30 additions & 0 deletions numpy/core/src/multiarray/datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,27 @@
#include "_datetime.h"
#include "datetime_strings.h"

static PyObject *dtype_us;
static PyObject *dtype_D;

/*
* Imports the PyDateTime functions so we can create these objects.
* This is called during module initialization
*/
NPY_NO_EXPORT void
numpy_pydatetime_import()
{
PyArray_DatetimeMetaData meta;

PyDateTime_IMPORT;

meta.num = 1;

meta.base = NPY_FR_us;
dtype_us = create_datetime_dtype(PyArray_DATETIME, &meta);

meta.base = NPY_FR_D;
dtype_D = create_datetime_dtype(PyArray_DATETIME, &meta);
}

/* Exported as DATETIMEUNITS in multiarraymodule.c */
Expand Down Expand Up @@ -683,6 +696,23 @@ PyArray_TimedeltaToTimedeltaStruct(npy_timedelta val, NPY_DATETIMEUNIT fr,
memset(result, -1, sizeof(npy_timedeltastruct));
}

/*
* Gets a descr for a Date or Datetime
*/
NPY_NO_EXPORT PyArray_Descr *
get_datetime_dtype_for_obj(PyObject *datetime)
{
if (PyDateTime_Check(datetime)) {
Py_INCREF(dtype_us);
return dtype_us;
} else if (PyDate_Check(datetime)) {
Py_INCREF(dtype_D);
return dtype_D;
} else {
return NULL;
}
}

/*
* Creates a datetime or timedelta dtype using a copy of the provided metadata.
*/
Expand Down