Python Forum

Full Version: How to speed up work with pandas index?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Python 3.7.3, pandas 0.25.1

I wrote a program using the DataFrame of the pandas library. But the speed is tens (or even a hundred) times less, than using dict and tuple. Index built on the lightest data type- 8bit unsigned int ('u1'). When adding new data, I always sort the index (although without this, the performance is just as terribly low). Moreover, the file contains about 6,000,000 lines, and there are only 2 different clients (that is, rebuilding the index is very rare, and the number of lines in the DataFrame is very small).


#create dataframe
dtype=np.dtype([('day_begin','u4'), ('day_end','u4'), ('price_begin','f4'), ('price_end','f4'), ('Client','u1')])      
auxiliary_array = np.empty(0, dtype=dtype)       
periods_clients = pd.DataFrame(auxiliary_array)        
periods_clients.set_index(['Client'], inplace=True)
#fill dataframe from file
with open(path_file) as csv_file:
        reader = csv.reader(csv_file)
        fieldnames = ['Date', 'Client', 'Price']
        reader = csv.DictReader(csv_file, fieldnames=fieldnames, delimiter=';')
        for dict_str in reader:
            Client = dict_str['Client']
            if Client not in periods_clients.index:
                periods_clients.loc[Client] = [current_date, current_date, current_price, current_price]
                periods_tickers.sort_index(level=0, inplace=True)
                periods_clients.loc[Client].day_end = current_date
                periods_clients.loc[Client].price_end = current_price
When I create an index, its type independently changes to uint64 (from uint8):
periods_clients.set_index(['Client'], inplace=True)
and when I add the first value:

periods_clients.loc[Client] = [current_date, current_date, current_price, current_price]

to float64. Client is equal 0, which type I tried change to: int, int8, int64, uint8, uint64. Why?