0

Trying to take output of a code and pass it to another statement.

t = ", ".join(DTYPE)
output = ('data', list), ('freq', str)

Would like to using "output" and pass into the following:

DF_TYPE = numpy.dtype([t)])
DF = numpy.empty(0, dtype=DF_TYPE)

That is, evaluate

DF_TYPE = numpy.dtype([('data', list), ('freq', str)])
DF = numpy.empty(0, dtype=DF_TYPE)

Error received:

Exception has occurred: TypeError
data type not understood

Sample data:

JSON_DATA = {
  "data": [
    {
      "Members": 29,
      "period": "2020-06-30",
      "TCOC": 5880070,
    },
    {
      "Members": 21,
      "period": "2020-03-31",
      "TCOC": 5689074,
    }
  ],
  "freq": "quarterly"
}

Have tried using exec() and eval() to no avail.

2 Answers 2

1

Apparently I need 50 rep to comment. But could you provide additional information like toy data or any error messages? Thanks

Also, I noticed you refer to numpy as numpy and np. The system will only recognize whatever your import statement says so:

import numpy is referred to as numpy.whatever and import numpy as np is referred to as np.whatever

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

Comments

0

With a few corrections, these structured array creations work:

In [244]: dt = np.dtype([('data',object), ('freq', 'U10')])                                            
In [245]: x = np.empty(3, dtype=dt)                                                                    
In [246]: x                                                                                            
Out[246]: 
array([(None, ''), (None, ''), (None, '')],
      dtype=[('data', 'O'), ('freq', '<U10')])

or data as a list of tuples:

In [247]: x = np.array([([1,2,3], 'quarterly'), ([4,5], 'yearly')], dt)                                
In [248]: x                                                                                            
Out[248]: 
array([(list([1, 2, 3]), 'quarterly'), (list([4, 5]), 'yearly')],
      dtype=[('data', 'O'), ('freq', '<U10')])
In [249]: x['data']                                                                                    
Out[249]: array([list([1, 2, 3]), list([4, 5])], dtype=object)

and with the JSON_DATA dictionary:

In [251]: JSON_DATA['data']                                                                            
Out[251]: 
[{'Members': 29, 'period': '2020-06-30', 'TCOC': 5880070},
 {'Members': 21, 'period': '2020-03-31', 'TCOC': 5689074}]

In [254]: x = np.empty(2, dt)                                                                          
In [255]: x['data'] = JSON_DATA['data']                                                                
In [256]: x['freq'] = JSON_DATA['freq']                                                                

In [257]: x                                                                                            
Out[257]: 
array([({'Members': 29, 'period': '2020-06-30', 'TCOC': 5880070}, 'quarterly'),
       ({'Members': 21, 'period': '2020-03-31', 'TCOC': 5689074}, 'quarterly')],
      dtype=[('data', 'O'), ('freq', '<U10')])
In [258]: x['data']                                                                                    
Out[258]: 
array([{'Members': 29, 'period': '2020-06-30', 'TCOC': 5880070},
       {'Members': 21, 'period': '2020-03-31', 'TCOC': 5689074}],
      dtype=object)

An element of the 'data' field is actually a dictionary:

In [259]: x['data'][0]                                                                                 
Out[259]: {'Members': 29, 'period': '2020-06-30', 'TCOC': 5880070}

1 Comment

Thanks for answering. I am trying to make such that I don't have to enter the values of 'data' or 'freq' manually. These values are outputs form the join statement. I want to pass them into another line of code and run it

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.