Asserting column(s) data type in Pandas

前端 未结 2 1498
执念已碎
执念已碎 2021-02-05 15:27

I\'m trying to find a better way to assert the column data type in Python/Pandas of a given dataframe.

For example:

import pandas as pd
t = pd.DataFrame(         


        
2条回答
  •  鱼传尺愫
    2021-02-05 15:36

    You can do this

    import numpy as np
    numeric_dtypes = [np.dtype('int64'), np.dtype('float64')]
    # or whatever types you want
    
    assert t[numeric_cols].apply(lambda c: c.dtype).isin(numeric_dtypes).all()
    

提交回复
热议问题