今天爱分享给大家带来pandas中如何将字符串object 转化 datetime 类型【附代码】,希望能够帮助到大家。
import pandas as pd from pandas import DataFrame from dateutil.parser import parse
数据
data = DataFrame(columns=['date'], data=['2020-11-01','2020-11-05','2020-11-08','2020-11-11']) data
data.info() """RangeIndex: 4 entries, 0 to 3 Data columns (total 1 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 date 4 non-null object dtypes: object(1) memory usage: 160.0+ bytes"""
转化
data['date'] = data['date'].apply(parse) data.info() """RangeIndex: 4 entries, 0 to 3 Data columns (total 1 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 date 4 non-null datetime64[ns] dtypes: datetime64[ns](1) memory usage: 160.0 bytes """