Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if pandas Series is empty?

Tags:

python

pandas

How to check if pandas Series is empty?

I have tried this:

How to check whether a pandas DataFrame is empty?

but it seems that Series has no property 'isempty'.

like image 944
BP_ Avatar asked Jul 09 '14 11:07

BP_


People also ask

How do you check whether the series is empty or not?

empty attribute checks if the dataframe is empty or not. It return True if the dataframe is empty else it return False . Example #1: Use DataFrame. empty attribute to check if the given dataframe is empty or not.


1 Answers

I use len function. It's much faster than empty(). len(df.index) is even faster.

import pandas as pd import numpy as np  df = pd.DataFrame(np.random.randn(10000, 4), columns=list('ABCD'))  def empty(df):     return df.empty  def lenz(df):     return len(df) == 0  def lenzi(df):     return len(df.index) == 0  ''' %timeit empty(df) %timeit lenz(df) %timeit lenzi(df)  10000 loops, best of 3: 13.9 µs per loop 100000 loops, best of 3: 2.34 µs per loop 1000000 loops, best of 3: 695 ns per loop  len on index seems to be faster ''' 
like image 167
Zero Avatar answered Sep 29 '22 05:09

Zero