python 2.7 - `TypeError: invalid type promotion` when appending to a heterogeneous numpy array -
i have created array with:
ticket_data = np.empty((0,7), dtype='str,datetime64[m],datetime64[m],str,str,str,str') and trying append data with:
lineitem = [str(data[0][0]), opendt, closedt, str(data[0][11]), str(data[0][12]), str(data[0][13]), str(data[0][14])] where opendt , closedt created np.datetime64(dtstring, 'm')
i getting error:
traceback (most recent call last): file "daily report.py", line 25, in <module> np.append(ticket_data, np.array([lineitem]), axis=0) file "c:\python27\lib\site-packages\numpy\lib\function_base.py", line 3884, in append return concatenate((arr, values), axis=axis) typeerror: invalid type promotion edit:
print np.array([lineitem]) outputs
[['21539' '2015-06-30t10:46-0700' '2015-06-30t10:55-0700' 'testtext' 'testtext2' 'testtext3' 'testtext5']] and
print np.array([lineitem], dtype=ticket_data.dtype) outputs
[[('', 245672259890l, datetime.datetime(1970, 1, 1, 0, 0), '', '', '', '') ('', datetime.datetime(2015, 6, 30, 17, 46), datetime.datetime(1970, 1, 1, 0, 0), '', '', '', '') ('', datetime.datetime(2015, 6, 30, 17, 55), datetime.datetime(1970, 1, 1, 0, 0), '', '', '', '') ('', 7741528753124368710l, datetime.datetime(1982, 11, 21, 6, 33), '', '', '', '') ('', 7959953343691844691l, datetime.datetime(1970, 1, 1, 0, 0), '', '', '', '') ('', datetime.datetime(5205, 7, 21, 7, 42), datetime.datetime(1970, 1, 1, 0, 0), '', '', '', '') ('', 2336635297857499728l, 2338042681633169744l, '', '', '', '')]] what can resolve this?
firstly, fields in structured array not same thing dimensions in regular ndarray. want ticket_label array 1-dimensional, each row element in dimension contain 7 fields, e.g.:
ticket_data = np.empty((0,), dtype='str,datetime64[m],datetime64[m],str,str,str,str') now in order concatenate lineitem ticket_data, must first implicitly cast nested lists array. since don't specify separate dtypes each field, numpy treats lineitem homogeneous array, , finds common dtype each element can safely promoted to.
for example:
lineitem = ['foo', np.datetime64('1979-03-22t19:00', 'm'), np.datetime64('1979-03-22t19:00', 'm'), 'bar', 'baz', 'a', 'b'] np.array(lineitem) # array(['21539', '2015-06-30t10:46-0700', '2015-06-30t10:55-0700', # 'testtext', 'testtext2', 'testtext3', 'testtext5'], # dtype='|s21') in example, every element cast 21-long string. dtype of array not match of ticket_data, , since there no safe way cast '|s21' 'np.datetime64[m]' invalid type promotion error.
you avoid error explicitly casting lineitem array, specifying correct dtypes each field:
np.array([tuple(lineitem)], dtype=ticket_data.dtype) note i'm casting lineitem tuple - necessary in order elements in lineitem interpreted separate fields rather separate elements. result array of shape (1,) (not (1, 7)):
np.array([tuple(lineitem)], dtype=ticket_data.dtype).shape # (1,) if don't cast lineitem tuple (1, 7) array, each individual element in lineitem interpreted sequence of 'str,datetime64[m],datetime64[m],str,str,str,str', resulting in nonsense showed in edit.
the result can concatenated ticket_label.
as aside, recommend using pandas instead of structured arrays dealing heterogeneous data such this.
Comments
Post a Comment