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