Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DataFrame: test if index name is set

I have a DataFrame with multiple columns of which one is of type datetime. Sometimes this column is used as index via df.set_index(...).

On other occasions I need to reset that index in order to keep the datetime column. Now I'm looking for a way how to check if the dataframe has a default index or not. I tried this, but this is not working for all cases:

if df.index.name is not None:
    df.reset_index(inplace=True)

I could test if the index is of type datetime, but I really wonder if there is a general method like df.is_index_set(). Any recommendations?

like image 859
Matthias Avatar asked Dec 05 '17 09:12

Matthias


1 Answers

You should not test the index.name attribute as it is not set in all scenarios. You can do the following tests :

In[13]:

df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'))
type(df.index) == pd.RangeIndex

Out[13]: True

In[14]:    
df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'), index=pd.date_range(dt.datetime(2017,1,1), periods=5))
df.index.is_all_dates

Out[14]: True

So the default index is a pd.RangeIndex, if your index is a DatetimeIndex you can just call is_all_dates or compare against pd.DatetimeIndex

The first method will handle even if you set the index to an int column which is monotonic:

In[27]:
df = pd.DataFrame({'a':[0,1,2,3,4], 'b':35,'c':np.random.randn(5)})
df = df.set_index('a')
type(df.index) == pd.RangeIndex

Out[27]: False

the index dtype here is an Int64Index:

In[28]:
df.index

Out[28]: Int64Index([0, 1, 2, 3, 4], dtype='int64', name='a')
like image 121
EdChum Avatar answered Oct 29 '22 18:10

EdChum