Python function redefines variables that are not passed
<p>I made a function that operates on a numpy array of latitude/longitude values.  </p>

<pre><code>from __future__ import division, print_function

import pandas as pd, numpy as np


def regrid2(lats, lons, lat_res=0.25, lon_res=0.25):

    # round lat/lon values to nearest decimal degree according to specified
    # resolution and reshape the array
    lats[lats&lt;=0] = lat_res*(np.round(lats[lats&lt;=0]/lat_res)) - lat_res/2
    lats[lats&gt;0]  = lat_res*(np.round(lats[lats&gt;0]/lat_res))  + lat_res/2

    lons[lons&lt;=0] = lon_res*(np.round(lons[lons&lt;=0]/lon_res)) + lon_res/2
    lons[lons&gt;0]  = lon_res*(np.round(lons[lons&gt;0]/lon_res))  - lon_res/2

    lats = np.reshape(lats, (lats.size,1),  order='F')
    lons = np.reshape(lons, (lons.size,1),  order='F')

    lats = 0

    df = pd.DataFrame()
    return df


lat = np.arange(80.111, 90, 5)
lon = np.arange(170.11, 180,0.33)

df = regrid2(lat,lon)
</code></pre>

<p>When I call regrid2 my lat/lon arrays change, even though the function is not returning new arrays for lat/lon.  </p>

<p>E.g., before calling regrid: </p>

<pre><code>&gt;&gt;&gt; lat.min()
    80.111000000000004
&gt;&gt;&gt; lon.min()
    170.11000000000001
</code></pre>

<p>AFTER calling regrid: </p>

<pre><code>&gt;&gt;&gt; lat.min()
    80.125
&gt;&gt;&gt; lon.min()
    169.875
</code></pre>

<p>I do not recall having similar problems before.  What makes this particularly strange is that I ran the same script last night without problems, but this morning the function is redefining my lat/lon variables.  I have re-started my IDE, but I have not been able to determine why this is happening.  </p>

<p>That being said, if I copy lat/lon into regrid then I have no problems.  e.g.</p>

<pre><code>df = regrid2(lat.copy(),lon.copy())
&gt;&gt;&gt; lat.min()
    80.111000000000004
&gt;&gt;&gt; lon.min()
    170.11000000000001
</code></pre>

<p>I would like to identify the sudden change in behavior.  I typically work in Pandas these days, not so much with numpy, so maybe there has been no change in behavior, rather I am just noticing this.</p>

<p>Python 2.7, Numpy 1.10.4</p>

<p>You're passing mutable data to the function, then mutating it. </p>

<p>You could use </p>

<pre><code>def regrid2(lats, lons, lat_res=0.25, lon_res=0.25):
    lats, lons = np.copy(lats), np.copy(lons)
</code></pre>

<p>to work with copies instead?</p>

python arrays pandas numpy