duke_nukem
duke_nukem

Reputation: 647

zero item added at the end while creating numpy array

As you can see from the following ipdb log, extra zero dates are added to pre and post arrays. How can i fix this? And why this happens?

ipdb> pre
(datetime.datetime(2013, 12, 31, 9, 58), 0, 1)

ipdb> post
(datetime.datetime(2013, 12, 31, 13, 15), 0, 1)

ipdb> RDT
RDT = [(COL_TIME, 'M8[s]'), (COL_STATUS, 'b'), (COL_MOBILE, 'b')]

ipdb> RMATCH
RMATCH = [(COL_PRE, RDT), (COL_POST, RDT)]

ipdb> ppre, ppost = np.array(pre, dtype=RDT), np.array(post, dtype=RDT)

ipdb> ppre
array((datetime.datetime(2013, 12, 31, 9, 58), 0, 1), 
      dtype=[('TIME', '<M8[s]'), ('STATUS', 'i1'), ('MOBILE', 'i1')])

ipdb> np.array([ppre, ppost], dtype=RMATCH)
array([ ((datetime.datetime(2013, 12, 31, 9, 58), 0, 1), (datetime.datetime(1970, 1, 1, 0, 0), 0, 0)),
       ((datetime.datetime(2013, 12, 31, 13, 15), 0, 1), (datetime.datetime(1970, 1, 1, 0, 0), 0, 0))], 
      dtype=[('PRE', [('TIME', '<M8[s]'), ('STATUS', 'i1'), ('MOBILE', 'i1')]), ('POST', [('TIME', '<M8[s]'), ('STATUS', 'i1'), ('MOBILE', 'i1')])])

Upvotes: 0

Views: 47

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114811

Your last result is an array with dtype RMATCH containing two elements. ppre and ppost were each converted to this dtype.

If you expected an array with a single element, with ppre filling the first field of the RMATCH dtype and ppost filling the second, try this:

np.array([(ppre, ppost)], dtype=RMATCH)

(Note the extra parentheses.)

I'm not sure if it is a bug, a wart or a feature that np.array([ppre, ppost], dtype=RMATCH) didn't raise an error, and instead filled in the unused fields with 0.

Upvotes: 1

Related Questions