Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Python
  4. Optimizing Slow For Loop Function Issue

Optimizing Slow For Loop Function Issue

Scheduled Pinned Locked Moved Python
helpdatabasesalesregexannouncement
1 Posts 1 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alagost
    wrote on last edited by
    #1

    I am creating a first in first out inventory style function. The input is a dataframe with a DateTimeIndex that is already sorted in ascending order. The two other columns are the price and quantity. Quantity can be either positive (purchases) or negative (sales). The logic evaluates row by row any offsetting transaction in which the buy_price <= sell_price and the position is a non-zero value. The resulting output should show all rows and the remaining quantities which were unable offset with future dated transactions. I started with a for loop which was extremely slow but produces the correct results. So I attempted optimize it and made some good progress but I can't seem to get the results to match. Any help is appreciated.

    def fifo(df):
    '''
    Iterate through chronologically sorted df and determine remaining
    buying or selling at each price level based on a first in first out
    method
    '''
    i = 0
    while i < len(df) - 1:
    if df.iloc[i]['quantity'] == 0:
    i += 1
    continue
    elif df.iloc[i]['quantity'] > 0:
    for j in range(i + 1, len(df)):
    if df.iloc[j]['quantity'] < 0:
    if df.iloc[i]['price'] <= df.iloc[j]['price']:
    qty = min(abs(df.iloc[j]['quantity']), df.iloc[i]['quantity'])
    df.at[df.index[i], 'quantity'] -= qty
    df.at[df.index[j], 'quantity'] += qty
    if df.iloc[i]['quantity'] == 0:
    break
    elif df.iloc[i]['quantity'] < 0:
    for j in range(i + 1, len(df)):
    if df.iloc[j]['quantity'] > 0:
    if df.iloc[j]['price'] <= df.iloc[i]['price']:
    qty = min(abs(df.iloc[i]['quantity']), df.iloc[j]['quantity'])
    df.at[df.index[i], 'quantity'] += qty
    df.at[df.index[j], 'quantity'] -= qty
    if df.iloc[i]['quantity'] == 0:
    break
    i += 1
    return df

    Here is my optimized version which does produce the net quantity correctly but the row by row price quantity comparison is incorrect.

    def fifo_optimized(df):
    buy_index = np.where(df.quantity > 0)[0]
    sell_index = np.where(df.quantity < 0)[0]
    buy_quantity = df.quantity.iloc[buy_index].values
    sell_qu

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups