How to check whether a DataFrame is empty?

Antoine Coppin picture Antoine Coppin · Aug 21, 2017 · Viewed 10.9k times · Source

I want to check whether a DataFrame is empty :

    BTC_ewma_24  ETH_ewma_24  DASH_ewma_24
24  4011.235578   334.597119        281.15
25  4011.285662   334.591056        281.15
26  4011.373673   334.603479        281.15
27  4011.453068   334.614686        281.15
28  4011.526571   334.624813        281.15
29  4011.591356   334.633980        281.15
30  4011.650075   334.642288        281.15
31  4011.703366   334.649828        281.15

I tried if(self.mean_exp.bool() == False): but it answers me :

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

As if it didn't even noticed that I used .bool()

I then used a.empty and it answered me :

AttributeError: 'list' object has no attribute 'empty'

Answer

MaxU picture MaxU · Aug 21, 2017

IIUC: there is .empty attribute:

DataFrame:

In [86]: pd.DataFrame().empty
Out[86]: True

In [87]: pd.DataFrame([1,2,3]).empty
Out[87]: False

Series:

In [88]: pd.Series().empty
Out[88]: True

In [89]: pd.Series([1,2,3]).empty
Out[89]: False

NOTE: checking the length of DF (len(df)) might save you a few milliseconds compared to df.empty method ;-)

In [142]: df = pd.DataFrame()

In [143]: %timeit df.empty
8.25 µs ± 22.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [144]: %timeit len(df)
2.35 µs ± 7.56 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [145]: df = pd.DataFrame(np.random.randn(10*5, 3), columns=['a', 'b', 'c'])

In [146]: %timeit df.empty
15.3 µs ± 269 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [147]: %timeit len(df)
3.58 µs ± 12.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)