I want to make an empty df with preserved datatypes as a template. Code is as follows:
import pandas as pd
import datetime
from dataclasses import dataclass
@dataclass
class OpenOrder:
symbol: str = "Dummy"
secType: str = "STK"
dt: datetime.datetime = datetime.datetime.now()
price: float = 0.0
status: str = None
def empty(self):
open_ord = self()
empty_df = pd.DataFrame([open_ord.__dict__])
return empty_df.iloc[0:0]
Instantiation works, but emptying doesn't.
open_order = OpenOrder()
order_df = open_order.empty()
How can I make this work?
You cannot call
self()asselfis a reference to the object and not to the class. Just use