You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Pandas中将DDMMMYYYY格式日期字符串列转为YYYYMMDD格式

Pandas日期格式转换:DDMMMYYYY转YYYYMMDD

需要将Pandas DataFrame中以字符串存储、格式为DDMMMYYYY(如31Oct2023)的日期列,转换为YYYYMMDD格式(如20231031)。

示例数据

序号GivenInputExpectedDates
031Jan202320230131
128Feb202320230228
231Mar202320230331
330Apr202320230430
431May202320230531
530Jun202320230630
631Jul202320230731
731Aug202320230831
830Sep202320230930
931Oct202320231031
1030Nov202320231130
1131Dec202320231231
1231Jan202420240131
1329Feb202420240229
1431Mar202420240331
1530Apr202420240430
1631May202420240531

加载示例数据代码

import pandas as pd
from io import StringIO

s = """
GivenInput
31Jan2023
28Feb2023
31Mar2023
30Apr2023
31May2023
30Jun2023
31Jul2023
31Aug2023
30Sep2023
31Oct2023
30Nov2023
31Dec2023
31Jan2024
29Feb2024
31Mar2024
30Apr2024
31May2024
"""
s = s.strip()

df = pd.read_csv(StringIO(s))
print(df)

转换方案

核心代码

# 执行日期格式转换
df['ConvertedDates'] = pd.to_datetime(df['GivenInput'], format='%d%b%Y').dt.strftime('%Y%m%d')

# 查看转换结果
print(df[['GivenInput', 'ConvertedDates']])

代码说明

  • pd.to_datetime(df['GivenInput'], format='%d%b%Y'):按照DDMMMYYYY格式将字符串列解析为Pandas日期时间类型
  • .dt.strftime('%Y%m%d'):将日期时间类型重新格式化为YYYYMMDD的字符串格式

内容的提问来源于stack exchange,提问作者CypherX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 04:06:21