Testing if a pandas DataFrame exists
<p>In my code, I have several variables which can either contain a pandas DataFrame or nothing at all.  Let's say I want to test and see if a certain DataFrame has been created yet or not.  My first thought would be to test for it like this:</p>

<pre><code>if df1:
    # do something
</code></pre>

<p>However, that code fails in this way:</p>

<pre><code>ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
</code></pre>

<p>Fair enough.  Ideally, I would like to have a presence test that works for either a DataFrame or Python None.  </p>

<p>Here is one way this can work:</p>

<pre><code>if not isinstance(df1, type(None)):
    # do something
</code></pre>

<p>However, testing for type is really slow.</p>

<pre><code>t = timeit.Timer('if None: pass')
t.timeit()
# approximately 0.04
t = timeit.Timer('if isinstance(x, type(None)): pass', setup='x=None')
t.timeit()
# approximately 0.4
</code></pre>

<p>Ouch.  Along with being slow, testing for NoneType isn't very flexible, either.  </p>

<p>A different solution would be to initialize <code>df1</code> as an empty DataFrame, so that the type would be the same in both the null and non-null cases.  I could then just test using <code>len()</code>, or <code>any()</code>, or something like that.  Making an empty DataFrame seems kind of silly and wasteful, though.</p>

<p>Another solution would be to have an indicator variable: <code>df1_exists</code>, which is set to False until <code>df1</code> is created.  Then, instead of testing <code>df1</code>, I would be testing <code>df1_exists</code>.  But this doesn't seem all that elegant, either.</p>

<p>Is there a better, more Pythonic way of handling this issue?  Am I missing something, or is this just an awkward side effect all the awesome things about pandas? </p>

<p><strong><em>Option 1</em></strong> (my preferred option)</p>

<h3>This is @Ami Tavory's</h3>

<p><strong><em>Please select his answer if you like this approach</em></strong></p>

<p>It is very idiomatic python to initialize a variable with <code>None</code> then check for <code>None</code> prior to doing something with that variable.</p>

<pre><code>df1 = None

if df1 is not None:
    print df1.head()
</code></pre>

<hr>

<p><strong><em>Option 2</em></strong></p>

<p>However, setting up an empty dataframe isn't at all a bad idea.</p>

<pre><code>df1 = pd.DataFrame()

if not df1.empty:
    print df1.head()
</code></pre>

<hr>

<p><strong><em>Option 3</em></strong></p>

<p>Just try it.</p>

<pre><code>try:
    print df1.head()
# catch when df1 is None
except AttributeError:
    pass
# catch when it hasn't even been defined
except NameError:
    pass
</code></pre>

<hr>

<h3>Timing</h3>

<p><strong><em>When <code>df1</code> is in initialized state or doesn't exist at all</em></strong></p>

<p><a href="http://i.stack.imgur.com/b1Dd6.png" rel="nofollow"><img src="http://i.stack.imgur.com/b1Dd6.png" alt="enter image description here"></a></p>

<p><strong><em>When <code>df1</code> is a dataframe with something in it</em></strong></p>

<pre><code>df1 = pd.DataFrame(np.arange(25).reshape(5, 5), list('ABCDE'), list('abcde'))
df1
</code></pre>

<p><a href="http://i.stack.imgur.com/iCAQK.png" rel="nofollow"><img src="http://i.stack.imgur.com/iCAQK.png" alt="enter image description here"></a></p>

<p><a href="http://i.stack.imgur.com/AgsSN.png" rel="nofollow"><img src="http://i.stack.imgur.com/AgsSN.png" alt="enter image description here"></a></p>

python pandas