KansaiRobot
KansaiRobot

Reputation: 9982

Using dataframes with pytest

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:

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?

Upvotes: 0

Views: 100

Answers (1)

jezrael
jezrael

Reputation: 863166

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)

Upvotes: 3

Related Questions