2

I'm writing a Python script as part of research on climate change and forest fires. This may be a novice question, but I am a beginner programmer. I have large numpy arrays (1) of meteorological variables (for instance: temperature, relative humidity, etc). In one part of the program, I define another array ('t0') to be equal to 'temp.' (2)

(1) `temp = N.array([[[-7.060185]],[[-17.5462963]],[[-22.43055556]],[[-16.13425926]]])`
(2) `t0 = temp`      
(3) `t0[t0 < (-1.1)] = -1.1`

This works--- 't0' is equal to 'temp' array, but after the third line (3) 'temp' has been saved over with the new values of 't0.' Is there any way to allow 'temp' to not be changed? I have tried saving other copies, etc but nothing has seemed to work.

Thanks!

1
  • By t0 = temp, you're binding a new name t0 to the same object (in this case a numpy array)
    – mg007
    Commented Jul 22, 2013 at 17:00

2 Answers 2

0
t0 = temp

doesn't actually perform a copy. It makes the names t0 and temp both refer to the same array. You probably want

t0 = temp.copy()

which makes a new, independent array.

0

You want to use deep copy see the documentation here. Deep copy will create a new array t0 that has unique memory locations in which the values are copied from temp. What is happening to you is that you are saying t0 is the same object as temp, then when you change t0 temp is changed since you stated that they are the same object.

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.