Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to speed up work with pandas index?
#1
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).

Why?

#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)
            else:                  
                periods_clients.loc[Client].day_end = current_date
                periods_clients.loc[Client].price_end = current_price
Quote
#2
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?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Applying operation to a pandas multi index dataframe subgroup Nuovoq 1 407 Sep-04-2019, 10:04 PM
Last Post: Nuovoq
  pandas: can we look for the index of a string paul18fr 2 409 Jul-31-2019, 08:25 AM
Last Post: paul18fr
  How to get first and last row index of each unique names in pandas dataframe SriRajesh 1 1,320 Oct-13-2018, 07:04 AM
Last Post: perfringo
  Select in Multi Index Pandas diego_last 0 644 Aug-01-2018, 12:56 PM
Last Post: diego_last
  Using pandas, index error fyec 1 1,334 Aug-01-2018, 09:25 AM
Last Post: volcano63
  I have python pandas tutorials and others I want them to work together josheeg 1 708 May-21-2018, 02:02 AM
Last Post: Larz60+
  is a pandas dataframe timeseries time index in a specified range (but ignoring date)? m_lotinga 4 13,620 Dec-12-2016, 10:51 PM
Last Post: m_lotinga
  pandas/index.pyx in pandas.index.IndexEngine.get_loc - KeyError jacobs.smith 2 3,693 Nov-17-2016, 04:07 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)