How do I tell if a column in a pandas dataframe is of type datetime? How do I tell if a column is numerical?

前端 未结 5 1923
情书的邮戳
情书的邮戳 2020-12-30 18:58

I am trying to filter the columns in a pandas dataframe based on whether they are of type date or not. I can figure out which ones are, but then would have to parse that ou

5条回答
  •  一整个雨季
    2020-12-30 19:39

    This code automatically identify the date column and change datatype from object to 'datetime64[ns]'. Once you got date datatype you can easily perform other operations.

    for col in data.columns:
        if data[col].dtype == 'object':
            try:
                data[col] = pd.to_datetime(data[col])
            except ValueError:
                pass
    

提交回复
热议问题