Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to check if value in a DataFrame is a type Decimal

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')

>Solution :

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

import pandas as pd
from decimal import Decimal
is_decimal: bool = pd.core.dtypes.common.is_dtype_equal(result["foo1"], Decimal)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading