@@ -24,6 +24,9 @@ template<> struct NPY_TYPE<int64_t> { static const int value = NPY_LONGLONG; };
2424template <> struct NPY_TYPE <uint64_t > { static const int value = NPY_ULONGLONG ; };
2525template <> struct NPY_TYPE <double > { static const int value = NPY_DOUBLE ; };
2626template <> struct NPY_TYPE <std::string> { static const int value = NPY_UNICODE ; };
27+ template <> struct NPY_TYPE <DateTime> { static const int value = NPY_DATETIME ; };
28+ template <> struct NPY_TYPE <TimeDelta> { static const int value = NPY_TIMEDELTA ; };
29+ template <> struct NPY_TYPE <Date> { static const int value = NPY_DATETIME ; };
2730
2831template <typename T> struct is_native { static constexpr bool value = false ; };
2932template <> struct is_native <bool > { static constexpr bool value = true ; };
@@ -93,7 +96,7 @@ inline PyObject * as_nparray( const csp::TimeSeriesProvider * ts, const TickBuff
9396{
9497 int32_t len = startIndex - endIndex + 1 ;
9598 if ( len <= 0 || !ts -> valid () || ( !tickBuffer && endIndex != 0 ) )
96- return empty_array ( NPY_TYPE <DateTime>::value );
99+ return empty_array ( NPY_OBJECT );
97100
98101 DateTime * values = getValues ( tickBuffer, lastValue, startIndex, endIndex, &len, tailPadding );
99102 npy_intp dims[]{ ( npy_intp ) len };
@@ -104,6 +107,7 @@ inline PyObject * as_nparray( const csp::TimeSeriesProvider * ts, const TickBuff
104107 auto date_type = PyPtr<PyObject>::own ( PyUnicode_FromString ( " <M8[ns]" ) );
105108 PyArray_DescrConverter ( date_type.get (), &datetime_descr );
106109 }
110+
107111 // PyArray_NewFromDescr steals a reference
108112 Py_INCREF ( datetime_descr );
109113 auto * array = PyArray_NewFromDescr ( &PyArray_Type, datetime_descr, 1 , dims, nullptr , values, 0 , nullptr );
@@ -116,7 +120,7 @@ inline PyObject * as_nparray( const csp::TimeSeriesProvider * ts, const TickBuff
116120{
117121 int32_t len = startIndex - endIndex + 1 ;
118122 if ( len <= 0 || !ts -> valid ()|| ( !tickBuffer && endIndex != 0 ) )
119- return empty_array ( NPY_TYPE <TimeDelta>::value );
123+ return empty_array ( NPY_OBJECT );
120124
121125 TimeDelta * values = getValues ( tickBuffer, lastValue, startIndex, endIndex, &len, tailPadding );
122126 npy_intp dims[]{ ( npy_intp ) len };
@@ -127,6 +131,7 @@ inline PyObject * as_nparray( const csp::TimeSeriesProvider * ts, const TickBuff
127131 auto timedelta_type = PyPtr<PyObject>::own ( PyUnicode_FromString ( " <m8[ns]" ) );
128132 PyArray_DescrConverter ( timedelta_type.get (), &timedelta_descr );
129133 }
134+
130135 Py_INCREF ( timedelta_descr );
131136 auto * array = PyArray_NewFromDescr ( &PyArray_Type, timedelta_descr, 1 , dims, nullptr , values, 0 , nullptr );
132137 PyArray_ENABLEFLAGS ( ( PyArrayObject * ) array, NPY_ARRAY_OWNDATA );
@@ -232,14 +237,23 @@ PyObject * valuesAtIndexToNumpy( ValueType valueType, const csp::TimeSeriesProvi
232237 autogen::TimeIndexPolicy startPolicy, autogen::TimeIndexPolicy endPolicy,
233238 DateTime startDt = DateTime::NONE (), DateTime endDt = DateTime::NONE() );
234239
235- int64_t scalingFromNumpyDtUnit ( NPY_DATETIMEUNIT base );
236- NPY_DATETIMEUNIT datetimeUnitFromDescr ( PyArray_Descr* descr );
240+ CSPIMPL_EXPORT int64_t scalingFromNumpyDtUnit ( NPY_DATETIMEUNIT base );
241+ CSPIMPL_EXPORT NPY_DATETIMEUNIT datetimeUnitFromDescr ( PyArray_Descr* descr );
237242
238243// for getting strings from elems of numpy arrays of strings
239244void stringFromNumpyStr ( void * data, std::string& out, char numpy_type, int elem_size_bytes );
240245
241246void validateNumpyTypeVsCspType ( const CspTypePtr & type, char numpy_type_char );
242247
248+ // Reshape a flat 1D numpy array into an NDArray using the given dimensions.
249+ CSPIMPL_EXPORT DialectGenericType numpyReshape ( DialectGenericType flatData, const std::vector<int64_t > & dims );
250+
251+ // Extract shape from a numpy NDArray as a vector of dimension sizes.
252+ CSPIMPL_EXPORT std::vector<int64_t > numpyShape ( DialectGenericType ndarray );
253+
254+ // Convert a Python type object (passed as DialectGenericType) to an NPY type constant.
255+ CSPIMPL_EXPORT int npyTypeFromPyType ( DialectGenericType pyTypeObj );
256+
243257}
244258
245259#endif
0 commit comments