Make Python Code blazing fast
Budget: $10 – $300 CAD
Hello and thank you for your time. Please read the entire description and reach out if you have any questions.
So, I am in the process of building a model in Python and the entire file has about 1500 lines of codes and there are like 5 such files. I need help making these codes blazing fast. For the benefit of both you and me, I am sharing a sample code (test code) of a situation where I am dealing with a pandas dataframe that is at least 300,000 rows and 11 columns. I have a nested for loop to label the dataframe for each row in a new column.
The code is just 11 lines and I would like to see the performance comparison from your submission. Please note that this is a test job and based on the best submission I will move ahead with the full project. There is more work that these 5 files to be done too.
You are free to try eliminating for loops, using numpy vectorization, pandas vectorization but the end goal is to execute this blazing fast. For your reference the code is given below and sample data is also attached. Kindly submit your code and comparison times running the code as it is and your vectorized code for evaluation.
This is a test job so I am not paying anything for 11 lines of code. This is the evaluation criteria for the bigger project. I will be discussing timeline and costing once finalized.
Code -
for i in range(len(df) - timeframe):
calc = df['Close'].iloc[i] * (1 + percentage)
subset = df['Close'].iloc[i+1:i+timeframe+1]
if percentage < 0:
direction = 'short'
else:
direction = 'long'
for price in subset:
if (direction == 'long' and price >= calc) or (direction == 'short' and price <= calc):
df['label'].iloc[i] = 1
break
df = dataframe
percentage = variable (assign any value you want for trial)
timeframe = variable (assign any value you want for trial)
long and short = labels
Close = column in df
label = column in df (to be created)
So, I am in the process of building a model in Python and the entire file has about 1500 lines of codes and there are like 5 such files. I need help making these codes blazing fast. For the benefit of both you and me, I am sharing a sample code (test code) of a situation where I am dealing with a pandas dataframe that is at least 300,000 rows and 11 columns. I have a nested for loop to label the dataframe for each row in a new column.
The code is just 11 lines and I would like to see the performance comparison from your submission. Please note that this is a test job and based on the best submission I will move ahead with the full project. There is more work that these 5 files to be done too.
You are free to try eliminating for loops, using numpy vectorization, pandas vectorization but the end goal is to execute this blazing fast. For your reference the code is given below and sample data is also attached. Kindly submit your code and comparison times running the code as it is and your vectorized code for evaluation.
This is a test job so I am not paying anything for 11 lines of code. This is the evaluation criteria for the bigger project. I will be discussing timeline and costing once finalized.
Code -
for i in range(len(df) - timeframe):
calc = df['Close'].iloc[i] * (1 + percentage)
subset = df['Close'].iloc[i+1:i+timeframe+1]
if percentage < 0:
direction = 'short'
else:
direction = 'long'
for price in subset:
if (direction == 'long' and price >= calc) or (direction == 'short' and price <= calc):
df['label'].iloc[i] = 1
break
df = dataframe
percentage = variable (assign any value you want for trial)
timeframe = variable (assign any value you want for trial)
long and short = labels
Close = column in df
label = column in df (to be created)