Speed-up search in pandas python

Viewed 6

I am trying to perform an analysis on some data, however, the speed shall be quite faster! these are the steps that I follow. Please recommend any solutions that you think might speed up the processing time. ts is a datetime object and the "time" column in Data is in epoch time. Note that Data might include up to 500000 records.

Data = pd.DataFrame(RawData) # (RawData is a list of lists)
Data.loc[:, 'time'] = pd.to_datetime(Data.loc[:, 'time'], unit='s')

I find the index of the first row in Data which has a time object greater than my ts as follows:

StartIndex = Data.loc[:, 'time'].searchsorted(ts)

StartIndex is usually very low and is found within a few records from the beginning, however, I have no idea if the size of Data would affect fining this index.

Now we get to the hard part: within Data there is column called "PDNumber". I have two other variables called Max_1 and Min_1. I have to find the index of the the row in which the "PDNumber" value goes above Max_1 or comes below Min_1. Note that this search shall start from StartIndex through the end of dataframe. whichever happens first, the search shall stop and the found Index is called SecondStartIndex. Now we have another two variables called Max_2 and Min_2. Again, we have to search the "PDNumber" column to find the index of the first row that goes above 'Max_2' or comes below Min_2; this index is called ThirdIndex

right now, I use a for loop to go through data adding the index by 1 in each step and see if I have reached the SecondIndex and when reached, I use a while loop till the end of dataframe to find the ThirdIndex. I use a counter in while loop as well.

Any suggestions on speeding up the process time?

0 Answers
Related