Python Forum
How to fill datetime64 field in numpy structured array?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to fill datetime64 field in numpy structured array?
#1
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 287 Mar-26-2024, 02:18 PM
Last Post: snippsat
  [Numpy] Load date/time from .txt to 'datetime64' type. water 4 403 Mar-01-2024, 11:16 PM
Last Post: Gribouillis
  GroupBy - Sum = Error [datetime64 type does not support sum operations] BSDevo 4 2,542 Oct-27-2023, 07:22 PM
Last Post: BSDevo
  reshaping 2D numpy array paul18fr 3 969 Jan-03-2023, 06:45 PM
Last Post: paul18fr
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,527 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  Numpy array BrianPA 13 4,836 Jan-23-2021, 09:36 AM
Last Post: Serafim
  Adding data in 3D array from 2D numpy array asmasattar 0 2,168 Jul-23-2020, 10:55 AM
Last Post: asmasattar
  converting dataframe to int numpy array glennford49 1 2,290 Apr-04-2020, 06:15 AM
Last Post: snippsat
  Replacing sub array in Numpy array ThemePark 5 4,086 Apr-01-2020, 01:16 PM
Last Post: ThemePark
  How to prepare a NumPy array which include float type array elements subhash 0 1,884 Mar-02-2020, 06:46 AM
Last Post: subhash

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020