I am seeing a significantly increased read time for a CSV by pandas when I specify the index_col. I do not understand the reason behind it. Can you help me understand why that is happening and if that is actually the expected behaviour? Below is the code I am using:
import pandas as pd
#save the CSV to be used
pd.DataFrame({'id':np.arange(100000000),'b':np.random.choice(['a','b','c','d'],size=(100000000,),p=[0.25,0.25,0.25,0.25])}).to_csv('df_sp.csv',index=None)
dfpd = pd.read_csv('df_sp.csv')
#read time - 10.3 seconds
dfpd = pd.read_csv('df_sp.csv',index_col='id')
#read time - 1 minute 38.6 seconds
In fact, I am seeing significant improvement if I read the dataset without specifying index_col and then set the index by dfpd = dfpd.set_index('id'). This takes just 1.6 more seconds. Why does pandas not default to always reading the dataframe with index_col as a column and then setting it as the index internally with set_index(index_col) when index_col is specified?
I'm on linux
Adding performance images in 3 slightly different runtime configurations:
Ubuntu 20.04.3 LTS
Ubuntu 20.04.3 LTS
Ubuntu 18.04.5 LTS


