Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed difference between bracket notation and dot notation for accessing columns in pandas

Let's have a small dataframe: df = pd.DataFrame({'CID': [1,2,3,4,12345, 6]})

When I search for membership the speed is vastly different based on whether I ask to search in df.CID or in df['CID'].

In[25]:%timeit 12345 in df.CID
Out[25]:89.8 µs ± 254 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In[26]:%timeit 12345 in df['CID']
Out[26]:42.3 µs ± 334 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In[27]:type( df.CID)
Out[27]: pandas.core.series.Series

In[28]:type( df['CID'])
Out[28]: pandas.core.series.Series

Why is that?

like image 256
dozyaustin Avatar asked May 21 '19 14:05

dozyaustin


People also ask

What is the difference between bracket and dot notation?

The dot notation is used mostly as it is easier to read and comprehend and also less verbose. The main difference between dot notation and bracket notation is that the bracket notation allows us to access object properties using variable.

Why are dot notation and bracket notation so useful?

Dot notation is faster to write and clearer to read. Square bracket notation allows access to properties containing special characters and selection of properties using variables.

Why do we use bracket notation?

Bracket Notation & Variables Bracket notation gives us the ability to use variables to access values in an object. This is especially helpful with the value of the variable changes.

Why do we use double brackets in pandas?

Indexing DataFrames You can either use a single bracket or a double bracket. The single bracket will output a Pandas Series, while a double bracket will output a Pandas DataFrame. Square brackets can also be used to access observations (rows) from a DataFrame.


1 Answers

df['CID'] delegates to NDFrame.__getitem__ and it is more obvious you are performing an indexing operation.

On the other hand, df.CID delegates to NDFrame.__getattr__, which has to do some additional heavy lifting, mainly to determine whether 'CID' is an attribute, a function, or a column you're calling using the attribute access (a convenience, but not recommended for production code).


Now, why is it not recommended? Consider,

df = pd.DataFrame({'A': [1, 2, 3]})
df.A

0    1
1    2
2    3
Name: A, dtype: int64

There are no issues referring to column "A" as df.A, because it does not conflict with any attribute or function namings in pandas. However, consider the pop function (just as an example).

df.pop
# <bound method NDFrame.pop of ...>

df.pop is a bound method of df. Now, I'd like to create a column called "pop" for various reasons.

df['pop'] = [4, 5, 6]
df
   A  pop
0  1    4
1  2    5
2  3    6

Great, but,

df.pop
# <bound method NDFrame.pop of ...>

I cannot use the attribute notation to access this column. However...

df['pop']

0    4
1    5
2    6
Name: pop, dtype: int64

Bracket notation still works. That's why this is better.

like image 85
cs95 Avatar answered Oct 13 '22 06:10

cs95