0

consider this numpy array:

array([('jona', (1,), '2021-01-08 07:38:10.000700 UTC', 1835.0, 1594.0, 0.07, 0.06, 0.08, 30.0, 272.0, nan),
       ('veep', (2,), '2021-01-08 07:38:10.000700 UTC', 52.0, 1594.0, 0.07, 0.07, 0.08, 0.0, 272.0, nan),
       ('hristo', (3,), '2021-01-08 07:38:10.000700 UTC', 52.0, 1594.0, 0.07, 0.07, 0.08, 0.0, 272.0, nan),
      dtype=[('name', 'O'), ('SIZE', 'O'), ('DATETIME', 'O'), ('OTH_SIZE', 'O'), ('THO_SIZE', 'O'), ('HAP', 'O'), ('NAP', 'O'), ('NOPE', 'O'), ('IMPLIED_NOPE', 'O'), ('IMPLIED_NAP', 'O'), ('Emptier', 'O')])

I would like to copy it to a numpy new array with the following types

array([('jona', (1,), '2021-01-08 07:38:10.000700 UTC', 1835.0, 1594.0, 0.07, 0.06, 0.08, 30.0, 272.0, nan),
       ('veep', (2,), '2021-01-08 07:38:10.000700 UTC', 52.0, 1594.0, 0.07, 0.07, 0.08, 0.0, 272.0, nan),
       ('hristo', (3,), '2021-01-08 07:38:10.000700 UTC', 52.0, 1594.0, 0.07, 0.07, 0.08, 0.0, 272.0, nan),
      dtype=[('name', 'O'), ('SIZE', '<i2'), ('DATETIME', 'O'), ('OTH_SIZE', '<i2'), ('THO_SIZE', '<i2'), ('HAP', '<f8'), ('NAP', '<f8'), ('NOPE', '<f8'), ('IMPLIED_NOPE', '<i2'), ('IMPLIED_NAP', '<i2'), ('Emptier', 'O')])

The one thing is I can't use pandas (the application need to run using only numpy).

1 Answer 1

1

One way is to change the dtype at array creation:

import numpy as np
a = np.array([('jona', (1,), '2021-01-08 07:38:10.000700 UTC', 1835.0, 1594.0, 0.07, 0.06, 0.08, 30.0, 272.0, np.nan),
              ('veep', (2,), '2021-01-08 07:38:10.000700 UTC', 52.0, 1594.0, 0.07, 0.07, 0.08, 0.0, 272.0, np.nan),
              ('hristo', (3,), '2021-01-08 07:38:10.000700 UTC', 52.0, 1594.0, 0.07, 0.07, 0.08, 0.0, 272.0, np.nan)],
              dtype=[('name', 'O'), ('SIZE', 'O'), ('DATETIME', 'O'), ('OTH_SIZE', 'O'), ('THO_SIZE', 'O'), ('HAP', 'O'), 
                     ('NAP', 'O'), ('NOPE', 'O'), ('IMPLIED_NOPE', 'O'), ('IMPLIED_NAP', 'O'), ('Emptier', 'O')] )
              
b = np.array(a.tolist(),dtype=[('name', 'O'), ('SIZE', '(1,)<i2'), ('DATETIME', 'O'), ('OTH_SIZE', '<i2'), ('THO_SIZE', '<i2'), ('HAP', '<f8'), 
                      ('NAP', '<f8'), ('NOPE', '<f8'), ('IMPLIED_NOPE', '<i2'), ('IMPLIED_NAP', '<i2'), ('Emptier', 'O')])

The only caveat is, that changing to ('SIZE', '(1,)<i2') requires to set the shape and the additional list-conversion step a.tolist() - not necessary, if you keep it as ('SIZE', 'O').

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.