BirdLaw
BirdLaw

Reputation: 592

How to assert an entire dataframe contains only numeric dtypes?

How can I most easily assert an entire dataframe contains only numeric dtypes?

Upvotes: 2

Views: 580

Answers (2)

MaxNoe
MaxNoe

Reputation: 15007

If you don't want to use undocumented private methods, that might change, you can try this:

len(df.columns) == len(df.select_dtypes([np.number]).columns)

or

all(np.issubdtype(dtype, np.number) for dtype in df.dtypes)

Upvotes: 4

BENY
BENY

Reputation: 323316

For example you have following dataframe, then we using _is_numeric_mixed_type

    ID  value
0   A1     11
2   A2     13
4   A3     15
6   B1     21
8   B2     23
10  B3     25
12  C1     31
14  C2     33
16  C3     35
s._is_numeric_mixed_type
Out[847]: False

Upvotes: 3

Related Questions