Reputation: 13
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')
Upvotes: 1
Views: 1711
Reputation: 41
Sadly the usage of is_dtype_equal
function as suggested yields True
if the Series is of another object
types. What worked for me was using isinstance(x, Decimal)
on of the the Series elements.
See example:
from decimal import Decimal
import pandas as pd
df = pd.DataFrame(
{
'a': ['A','B','C'] ,
'b': [11.1, 23.3,55.5],
'c': [Decimal(10.0), Decimal(11.1), Decimal(12.1)]
}
)
print("Using isinstance of")
print(isinstance(df['a'].values[0], Decimal))
print(isinstance(df['b'].values[0], Decimal))
print(isinstance(df['c'].values[0], Decimal))
print("Using is_dtype_equal")
print(pd.core.dtypes.common.is_dtype_equal(df['a'], Decimal))
print(pd.core.dtypes.common.is_dtype_equal(df['b'], Decimal))
print(pd.core.dtypes.common.is_dtype_equal(df['c'], Decimal))
Upvotes: 4
Reputation: 4529
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)
Upvotes: 1