Python Forum
How to fill datetime64 field in numpy structured array? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How to fill datetime64 field in numpy structured array? (/thread-30414.html)



How to fill datetime64 field in numpy structured array? - AlekseyPython - Oct-20-2020

Python 3.8.5, numpy 1.19.2, numba 0.51.2

I use method from official manual:

import numpy as np
import numba as nb


#get data from txt- file
regular_expression = _create_regular_expression(ticker, by_mask_ticker)
dtype = _create_dtype()
array_data = np.fromregex(file=path, regexp=regular_expression, dtype=dtype)

#add new column
lenght = array_data.shape[0]
array_for_date_time = np.empty(lenght, dtype='datetime64[s]')
array_data = recfunctions.append_fields(array_data, 'date_time', array_for_date_time)

#fill new column
fill_date_time_column(array_data)

@nb.jit(nopython=True)
def fill_date_time_column(array_data):
   for current in array_data:
      s = current['YEAR'] + '-' + current['MONTH'] + '-' + current['DAY'] + 'T' + current['TIME']
      #d = np.datetime64(s) #ERROR: Cannot cast unicode_type to datetime64[]
      #d = nb.types.NPDatetime(s) #ERROR: Unknown attribute 'NPDatetime' of type Module(<module 'numba.core.types'
      #d = nb.types.NPDatetime(Y=current['YEAR'], M=current['MONTH'], D=current['DAY']) #ERROR: Unknown attribute 'NPDatetime' of type Module(<module 'numba.core.types'
      d = nb.NPDatetime(s) #ERROR: module 'numba' has no attribute 'NPDatetime'
      current['date_time'] = d
If I read function like this (without numba):
def fill_date_time_column(array_data):
   for current in initial_data:
      s = current['YEAR'] + '-' + current['MONTH'] + '-' + current['DAY'] + 'T' + current['TIME']
      d = np.datetime64(s)
      current['date_time'] = d
, then all work ok. How I can fill column by datetime values?