title: numpy "Mean of empty slice." warning
body: <h1>UPDATE (The <em>real</em> error)</h1>
  <p>I misidentified where the error was coming from. Here is my function in its entirety (sorry if some of the lines are obscure and confusing...)</p>
  <pre><code>def removeLines(input,CRVAL1,CDELT1): #Masks out the Balmer lines from the spectrum
      #Numbers 4060, 4150, 4300, 4375, 4800, and 4950 obtained from fit_RVs.pro.
      #Other numbers obtained from the Balmer absorption series lines
      
      for i in range(0,len(lineWindows),2):
          left = toIndex(lineWindows[i],CRVAL1,CDELT1)
          right = toIndex(lineWindows[i+1],CRVAL1,CDELT1)
          
          print &quot;left = &quot;, left
          print &quot;right = &quot;, right
          print &quot;20 from right =\n&quot;, input[right:right+20]
          print &quot;mean of 20 = &quot;, numpy.mean(input[right:right+20])
          
          #Find the averages on the left and right sides
          left_avg = numpy.mean(input[left-20:left])
          right_avg = numpy.mean(input[right:right+20])   #&lt;--- NOT here
          
          print &quot;right_avg = &quot;, right_avg
          
          #Find the slope between the averages
          slope = (left_avg - right_avg)/(left - right)
          
          #Find the y-intercept of the line conjoining the averages
          bval = ((left_avg - slope*left) + (right_avg - slope*right)) / 2
          
          for j in range(left,right):     #Redefine the data to follow the line conjoining
              input[j] = slope*j + bval   #the sides of the peaks
  
      left = int(input[0])
      left_avg = int(input[0])
      right = toIndex(lineWindows[0],CRVAL1,CDELT1)
      right_avg = numpy.mean(input[right:right+20])   #&lt;---- THIS IS WHERE IT IS!
      slope = (left_avg - right_avg)/(left - right)
      bval = ((left_avg - slope*left) + (right_avg - slope*right)) / 2
      
      for i in range(left, right):
          input[i] = slope*i + bval
      return input
  </code></pre>
  <p>I have investigated the issue and found the answer, which is posted below (not in this post).</p>
  <hr />
  <h1>The error (The silly <em>fake</em> error)</h1>
  <pre><code>#left  = An index in the data (on the 'left' side)
  #right = An index in the data (on the 'right' side)
  #input = The data array
  
  print &quot;left = &quot;, left
  print &quot;right = &quot;, right
  print &quot;20 from right =\n&quot;, input[right:right+20]
  print &quot;mean of 20 = &quot;, numpy.mean(input[right:right+20])
  
  #Find the averages on the left and right sides
  left_avg = numpy.mean(input[left-20:left])
  right_avg = numpy.mean(input[right:right+20])
  </code></pre>
  <p>produced the output</p>
  <pre><code>left =  1333
  right =  1490
  20 from right =
  [ 0.14138737  0.14085886  0.14038289  0.14045525  0.14078836  0.14083192
    0.14072289  0.14082283  0.14058594  0.13977806  0.13955595  0.13998236
    0.1400764   0.1399636   0.14025062  0.14074247  0.14094831  0.14078569
    0.14001536  0.13895717]
  mean of 20 =  0.140395
  Traceback (most recent call last):
  ...
    File &quot;getRVs.py&quot;, line 201, in removeLines
      right_avg = numpy.mean(input[right:right+20])
    File &quot;C:\Users\MyName\Anaconda\lib\site-packages\numpy\core\fromnumeric.py&quot;, line 2735, in mean
      out=out, keepdims=keepdims)
    File &quot;C:\Users\MyName\Anaconda\lib\site-packages\numpy\core\_methods.py&quot;, line 59, in _mean
      warnings.warn(&quot;Mean of empty slice.&quot;, RuntimeWarning)
  RuntimeWarning: Mean of empty slice.
  </code></pre>
  <p>It would appear that <code>numpy.mean</code> runs correctly when I print it, but differently when I assign it to a value. Any feedback would be very appreciated. Thank you for taking the time to read my question.</p>
  <hr />
  <h1>Brief explanation</h1>
  <p>In short, I am writing a code to handle scientific data and part of the code involves taking the mean of about 20 values.</p>
  <pre><code>#left  = An index in the data (on the 'left' side)
  #right = An index in the data (on the 'right' side)
  #input = The data array
  
  #Find the averages on the left and right sides
  left_avg = numpy.mean(input[left-20:left])
  right_avg = numpy.mean(input[right:right+20])
  </code></pre>
  <p>This code returns a numpy &quot;Mean of empty slice.&quot; warning and annoyingly prints it in my precious output! I decided to try and track down the source of the warning as seen <a href="https://stackoverflow.com/questions/29688168/mean-nanmean-and-warning-mean-of-empty-slice">here</a>, for example, so I placed</p>
  <pre><code>import warnings
  warnings.simplefilter(&quot;error&quot;)
  </code></pre>
  <p>at the top of my code, which then returned the following snipped Traceback:</p>
  <pre><code>  File &quot;getRVs.py&quot;, line 201, in removeLines
      right_avg = numpy.mean(input[right:right+20])
    File &quot;C:\Users\MyName\Anaconda\lib\site-packages\numpy\core\fromnumeric.py&quot;, line 2735, in mean
      out=out, keepdims=keepdims)
    File &quot;C:\Users\MyName\Anaconda\lib\site-packages\numpy\core\_methods.py&quot;, line 59, in _mean
      warnings.warn(&quot;Mean of empty slice.&quot;, RuntimeWarning)
  RuntimeWarning: Mean of empty slice.
  </code></pre>
  <p>I omitted about 2/3 of the Traceback because it moves through about 5 difficult-to-explain functions that do not affect the readability or size of the data.</p>
  <p>So I decided to print out the whole operation to see if <code>right_avg</code> really was attempting a <code>numpy.mean</code> of an empty slice... And that's when things got <em>really weird</em>.</p>