<h4 dir="auto">Code Sample</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import pandas as pd
S = pd.Series([np.nan,1.5,np.nan,2,np.nan,np.nan,np.nan,1,1,2,2,2,3])
print S; print; print set(S)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-v">S</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">Series</span>([<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-c1">1.5</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-c1">2</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-c1">1</span>,<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">2</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>])
<span class="pl-k">print</span> <span class="pl-v">S</span>; <span class="pl-s1">print</span>; <span class="pl-k">print</span> <span class="pl-en">set</span>(<span class="pl-v">S</span>)</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">The NaNs show up multiple times in set(S), which is not a problem if</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="S = [np.nan,1.5,np.nan,2,np.nan,np.nan,np.nan,1,1,2,2,2,3]"><pre class="notranslate"><span class="pl-v">S</span> <span class="pl-c1">=</span> [<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-c1">1.5</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-c1">2</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-c1">1</span>,<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">2</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>]</pre></div>
<p dir="auto">is used instead. The multiple NaNs in set(S) also become very slow to run if</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="S = pd.Series([np.nan,1.5,np.nan,2,np.nan,np.nan,np.nan,1,1,2,2,2,3] + [np.nan] * 100000)"><pre class="notranslate"><span class="pl-v">S</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">Series</span>([<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-c1">1.5</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-c1">2</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-c1">1</span>,<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">2</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>] <span class="pl-c1">+</span> [<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>] <span class="pl-c1">*</span> <span class="pl-c1">100000</span>)</pre></div>
<p dir="auto">is used. Without pd.Series it's a flash of a second.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [21]: df = DataFrame({'A' : np.random.randn(5),
 'B' : np.random.randn(5),'C' : np.random.randn(5), 
  'D':['a','b','c','d','e'] })

In [22]: df
Out[22]: 
          A         B         C  D
0 -0.941264  0.272726 -0.547948  a
1  0.069432  1.398414  0.039093  b
2 -0.073638 -1.264676 -1.339994  c
3  1.520017 -0.562979 -0.739326  d
4 -0.395157  0.542807  0.766582  e

[5 rows x 4 columns]

In [23]: df.take([2,0,1,2,3], axis=1).drop('C',axis=1)
Out[23]: 
          A         B  D
0 -0.941264  0.272726  a
1  0.069432  1.398414  b
2 -0.073638 -1.264676  c
3  1.520017 -0.562979  d
4 -0.395157  0.542807  e

[5 rows x 3 columns]

In [24]: df.take([2,0,1,2,1], axis=1).drop('C',axis=1)
IndexError: index 3 is out of bounds for axis 0 with size 3"><pre class="notranslate"><code class="notranslate">In [21]: df = DataFrame({'A' : np.random.randn(5),
 'B' : np.random.randn(5),'C' : np.random.randn(5), 
  'D':['a','b','c','d','e'] })

In [22]: df
Out[22]: 
          A         B         C  D
0 -0.941264  0.272726 -0.547948  a
1  0.069432  1.398414  0.039093  b
2 -0.073638 -1.264676 -1.339994  c
3  1.520017 -0.562979 -0.739326  d
4 -0.395157  0.542807  0.766582  e

[5 rows x 4 columns]

In [23]: df.take([2,0,1,2,3], axis=1).drop('C',axis=1)
Out[23]: 
          A         B  D
0 -0.941264  0.272726  a
1  0.069432  1.398414  b
2 -0.073638 -1.264676  c
3  1.520017 -0.562979  d
4 -0.395157  0.542807  e

[5 rows x 3 columns]

In [24]: df.take([2,0,1,2,1], axis=1).drop('C',axis=1)
IndexError: index 3 is out of bounds for axis 0 with size 3
</code></pre></div>