0

import pandas as pd

class OrdersList:
    def __init__(self):
        self.shopping_list = pd.DataFrame()
        self.shopping_list = self.shopping_list.assign(Order=0, Type=0, Price=0, Quantity=0)

    def __repr__(self):
        return repr(self.shopping_list)

    def add_item(self, order: str, type_transaction: str, price: float, quantity: int):
        new_item_data = pd.DataFrame({"Order": [order],
                                      "Type": [type_transaction],
                                      "Price": [price],
                                      "Quantity": [quantity]})
        self.shopping_list = pd.concat([self.shopping_list, new_item_data], ignore_index=True)
        self.best_buy_order()
        self.best_sell_order()

    def best_buy_order(self):
        best_buy_items = self.shopping_list[self.shopping_list['Order'] == 'Buy']
        best_buy_items['Total_buy'] = best_buy_items['Price'] * best_buy_items['Quantity']
        print(best_buy_items[best_buy_items['Total_buy'] == best_buy_items['Total_buy'].max()])

    def best_sell_order(self):
        best_buy_items = self.shopping_list[self.shopping_list['Order'] == 'Sell']
        best_buy_items['Total_Sell'] = best_buy_items['Price'] * best_buy_items['Quantity']
        print(best_buy_items[best_buy_items['Total_Sell'] == best_buy_items['Total_Sell'].max()])

next to i add iteams:

my_object = OrdersList()
my_object.add_item(order="Buy", type_transaction="Add", price=20.0, quantity=100)
my_object.add_item(order="Sell", type_transaction="Add", price=25.0, quantity=200)
my_object.add_item(order="Buy", type_transaction="Add", price=10.0, quantity=10)
my_object.add_item(order="Sell", type_transaction="Add", price=5.0, quantity=200)
my_object.add_item(order="Buy", type_transaction="Add", price=50.0, quantity=100)

and i get always warning:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  best_buy_items['Total_buy'] = best_buy_items['Price'] * best_buy_items['Quantity']

I try solve it by using .loc but something always go wrong and like program show error. This warning is annoying

Robson
  • 21
  • 5
  • 1
    You're unsafely subsetting your DataFrame a few places. _e.g._ in `best_buy_order` the statement `best_buy_items = self.shopping_list[self.shopping_list['Order'] == 'Buy']` should be a copy. `best_buy_items = self.shopping_list[self.shopping_list['Order'] == 'Buy'].copy()`. This is explained in more detail [here](https://stackoverflow.com/a/66362915/15497888). – Henry Ecker Nov 17 '22 at 23:20
  • 1
    Same issue in the `best_sell_order` function you should be making a copy _i.e._ `best_buy_items = self.shopping_list[self.shopping_list['Order'] == 'Sell'].copy()` <- also you might want to reconsider that variable name. – Henry Ecker Nov 17 '22 at 23:21
  • thank you so much Henry Ecker – Robson Nov 17 '22 at 23:28

0 Answers0