Home > other >  how to check if value in a DataFrame is a type Decimal
how to check if value in a DataFrame is a type Decimal

Time:01-19

I am writing a data test for some api calls that return a DataFrame with a date type and a type Decimal. I can't find a way to verify the Decimal

the DataFrame is returned as 2022-01-18 12:35:00 2856.8430

So I have

result = ds.DataService.get_recent(5, 24)
assert not result.empty
assert ptypes.is_datetime64_any_dtype(result['foo'])

but if I try

assert all(ptypes.is_float_dtype(result[col]) for col in result["foo1"])

 raise KeyError(key) from err
KeyError: Decimal('2873.6408')

CodePudding user response:

So given a series like result["foo1"], you can check that with

from decimal import Decimal

import pandas as pd

is_decimal: bool = pd.core.dtypes.common.is_dtype_equal(result["foo1"], Decimal)
  •  Tags:  
  • Related