Home > Back-end >  Using dataframes with pytest
Using dataframes with pytest

Time:10-25

I am trying to design a test with pytest.

This involves some operation on some data that gives as a result a pandas dataframe.

I would like to assert that all elements of the dataframe are:

  • zero
  • below a small threshold

EDIT: Also I would like to assert only elements from certain columns to be below a threshold

How can I use pandas dataframes together with pytest?

CodePudding user response:

Use DataFrame.all with axis=None for test if all values are Trues, here if all values are 0 or less like threshold, for numeric columns use DataFrame.select_dtypes with np.number:

#test only numeric columns
df1 = df.select_dtypes(np.number) 
th = 0.01
assert (df1.eq(0) | df1.lt(th)).all(axis=None)
  • Related