0

I get a TypeError:

Traceback (most recent call last):
  File "...Transients_tP100us.py", line 20, in <module>
    temp[key] = data[key]["Temperature"].reshape((num_temp, num_dp)).T
TypeError: 'numpy.int32' object does not support item assignment

running this script:

import numpy as np

transient_files = {"50ms": "Transients_50ms.TXT",
                   "500ms": "Transients_500ms.TXT",
                   "5000ms": "Transients_5000ms.TXT"}

num_dp = 513
num_temp = 37

data = {}
time = {}
temp = {}
cap = {}

for key, value in transient_files.items():
    data[key] = np.genfromtxt(value, delimiter=";", names=[
                              "Time", "Temperature", "Capacitance"])

    time[key] = data[key]["Time"].reshape((num_temp, num_dp)).T    
    temp[key] = data[key]["Temperature"].reshape((num_temp, num_dp)).T
    cap[key] = data[key]["Capacitance"].reshape((num_temp, num_dp)).T * 1e-12

If I only run the Transient_50ms.TXT file, it runs without an error, it's only thrown for the two other files. The only difference between the files is, that the 50ms file doesn't have negative values in the third column, the others have. So two lines, with and without negative values in the datafiles look like this:

 5.065E-01; 3.270E+02;-1.182E-01
 6.832E-03; 3.391E+02; 7.501E-01

I made a check with:

for i in np.arange(0, len(data[key]["Time"])):
    if type(data[key]["Time"][i]) is not np.float64:
        print(i)

for i in np.arange(0, len(data[key]["Temperature"])):
    if type(data[key]["Temperature"][i]) is not np.float64:
        print(i)

for i in np.arange(0, len(data[key]["Capacitance"])):
    if type(data[key]["Capacitance"][i]) is not np.float64:
        print(i)

to see if all entries are really recognized as np.float64, which is the case. I guess it's a problem with parsing the data file, especially with the negative values, but I have no idea where the problem is and how to solve it.

5
  • 1
    Please post the error message, including the hint to the line which throwed the error. In the meantime: A reason why not use pandas? Commented Jun 22, 2018 at 9:25
  • @SpghttCd so far I didn't had the feeling that I need the features of pandas, but if it can solve my problem I will consider using it. I updated the error message with the line number. Commented Jun 22, 2018 at 9:31
  • 1
    Is this the whole code you use or is there sth left you didn't post? Is it possible that you redefine temp before going into the second Loop? Commented Jun 22, 2018 at 10:17
  • omg yeah thanks... I left out the plotting commands and there I had a typo. Commented Jun 22, 2018 at 10:57
  • You're welcome, good to know it works now. Commented Jun 22, 2018 at 11:02

1 Answer 1

1

Paraphrased from @SpghttCd-s comment:

It is likely that you redefine temp before going into the second loop, which seem to solve the problem as per the comments.

I made this answer community wiki since the question was answered in the comments by @SpghttCd.

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.