title: How do you calculate interquartile range (IQR) correctly using Python?
body: <p>I am trying to understand the way to compute iqr (interquartile range).</p>
  
  <p>according <a href="https://stackoverflow.com/questions/23228244/how-do-you-find-the-iqr-in-numpy">this</a>, <a href="https://stackoverflow.com/questions/27472330/how-should-the-interquartile-range-be-calculated-in-python">this</a> and <a href="https://en.wikipedia.org/wiki/Interquartile_range" rel="nofollow noreferrer">this</a>, I tried 3 solutions to do this.</p>
  
  <p>solution_1</p>
  
  <pre><code>a = numpy.array([1, 2, 3, 4, 5, 6, 7])
  q1_a = numpy.percentile(a, 25)
  q3_a = numpy.percentile(a, 75)
  q3_a - q1_a
  </code></pre>
  
  <p>solution_2</p>
  
  <pre><code>from scipy.stats import iqr
  iqr(a)
  </code></pre>
  
  <p>solution_3</p>
  
  <pre><code>q1_am = np.median(numpy.array([1, 2, 3, 4]))
  q3_am = np.median(numpy.array([4, 5, 6, 7]))
  q3_am - q1_am
  </code></pre>
  
  <p>3 of them give the same result 3 which is correct.</p>
  
  <p>when I tried another set of numbers, things were going weird.</p>
  
  <p>both solution_1 and 2 output 0.95 which is not correct.</p>
  
  <pre><code>x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
  q1_x = numpy.percentile(x, 25)
  q3_x = numpy.percentile(x, 75)
  q3_x - q1_x
  </code></pre>
  
  <p>solution_3 gives 1.2 which is correct</p>
  
  <pre><code>q1_xm = np.median(np.array([4.1, 6.2, 6.7,7.25]))
  q3_xm = np.median(np.array([7.25,7.4, 7.9, 8.1]))
  q3_xm - q1_xm
  </code></pre>
  
  <p>What am I missing with the solutions?</p>
  
  <p>any clue would be appreciated.</p>