title: Skip NaN values to get distance
body: <p>Part of my dataset (in reality my dataset size <code>(106,1800)</code>):</p>
  
  <p>df = </p>
  
  <pre><code>    1           1.1     2           2.1     3           3.1     4           4.1     5           5.1
  0   43.1024     6.7498  NaN         NaN     NaN         NaN     NaN         NaN     NaN         NaN
  1   46.0595     1.6829  25.0695     3.7463  NaN         NaN     NaN         NaN     NaN         NaN
  2   25.0695     5.5454  44.9727     8.6660  41.9726     2.6666  84.9566     3.8484  44.9566     1.8484
  3   35.0281     7.7525  45.0322     3.7465  14.0369     3.7463  NaN         NaN     NaN         NaN
  4   35.0292     7.5616  45.0292     4.5616  23.0292     3.5616  45.0292     6.7463  NaN         NaN
  </code></pre>
  
  <p>What I am able to do now based on Tom's answer:</p>
  
  <ul>
  <li>I manually wrote 1-st 2 rows like p and q value:</li>
  </ul>
  
  <p>p =</p>
  
  <pre><code>[[45.1024,7.7498],[45.1027,7.7513],[45.1072,7.7568],[45.1076,7.7563]]
  </code></pre>
  
  <p>q=</p>
  
  <pre><code>[[45.0595,7.6829],[45.0595,7.6829],[45.0564,7.6820],[45.0533,7.6796],[45.0501,7.6775]]
  </code></pre>
  
  <p>THEN:</p>
  
  <pre><code>__all__ = ['frdist']
  
  
  def _c(ca, i, j, p, q):
  
      if ca[i, j] &gt; -1:
          return ca[i, j]
      elif i == 0 and j == 0:
          ca[i, j] = np.linalg.norm(p[i]-q[j])
      elif i &gt; 0 and j == 0:
          ca[i, j] = max(_c(ca, i-1, 0, p, q), np.linalg.norm(p[i]-q[j]))
      elif i == 0 and j &gt; 0:
          ca[i, j] = max(_c(ca, 0, j-1, p, q), np.linalg.norm(p[i]-q[j]))
      elif i &gt; 0 and j &gt; 0:
          ca[i, j] = max(
              min(
                  _c(ca, i-1, j, p, q),
                  _c(ca, i-1, j-1, p, q),
                  _c(ca, i, j-1, p, q)
              ),
              np.linalg.norm(p[i]-q[j])
              )
      else:
          ca[i, j] = float('inf')
  
      return ca[i, j]
  </code></pre>
  
  <p>THEN:</p>
  
  <pre><code>def frdist(p, q):
  
      # Remove nan values from p
      p = np.array([i for i in p if np.any(np.isfinite(i))], np.float64)
      q = np.array([i for i in q if np.any(np.isfinite(i))], np.float64)
  
      len_p = len(p)
      len_q = len(q)
  
      if len_p == 0 or len_q == 0:
          raise ValueError('Input curves are empty.')
  
      # p and q will no longer be the same length
      if len(p[0]) != len(q[0]):
          raise ValueError('Input curves do not have the same dimensions.')
  
      ca = (np.ones((len_p, len_q), dtype=np.float64) * -1)
  
      dist = _c(ca, len_p-1, len_q-1, p, q)
      return(dist)
  
  frdist(p, q)
  </code></pre>
  
  <p>It works. But how I could apply p and q to the whole dataset? Not by choosing row by row?</p>
  
  <p>Finally I need to get <code>106 to 106</code> symmetric matrix with <code>0</code> diagonal</p>