Python pandas 使用技巧
一、 pd.to_datetime 格式化日期报错:
ValueError: time data "2023-10-14T18:44:36-07:00" doesn't match format "%Y-%m-%d %H:%M:%S", at position 0. You might want to try:
- passing `format` if your strings have a consistent format;
- passing `format='ISO8601'` if your strings are all ISO8601 but not necessarily in exactly the same format;
- passing `format='mixed'`, and the format will be inferred for each element individually. You might want to use `dayfirst` alongside this.
修改方法:
将
df['购买日期'] = pd.to_datetime(df['购买日期'], format='%Y-%m-%d %H:%M:%S')
改为
df['购买日期'] = pd.to_datetime(df['购买日期']).dt.strftime('%Y-%m-%d %H:%M:%S')
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »