Search code examples
pythonpandasdataframeassert

How to assert an entire dataframe contains only numeric dtypes?


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


Solution

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