How can I stricly enforce a dtype Decimal in a pandas DataFrame?
To clarify: I am not looking for weak workarounds, such as rounding every time I write to or read from a column (and hope that no other operations happend elsewhere that might lead to unwanted results).
I really want to be 100% sure that whatever is written in that column, no matter where it might have come from, will always have exactly 2 significant digits behind the decimal point, end of story. And if a user intends to write something that's not in agreement, the whole thing should blow up (either producing a TypeError or ValueError). --> To avoid theoretical dicussions and motivate the usage a bit: I am dealing with a trading system, that's why saving anything other than 2 decimal points in that frame would be a hard error, always.
I have tried to assign a dtype, but without success:
from decimal import Decimal
df[col].astype(Decimal)
Pydantic comes to mind: but if I bake the df into a class (say class MyDfType
), then do I need to write my own setter/getter functions into MyDfType
for the contained dataframe (MyDFType().df
) ensure that all values to/from certain cols are manually enforced to be Decimal?