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

如何将DataFrame中FY19 Jun格式日期转换为2019-01-01格式

将「FY19 Jun」格式日期转换为「YYYY-01-01」格式的方法

核心思路

从「FY19 Jun」格式的字符串中提取财年的年份数字,直接构造对应年份的1月1日标准日期。

具体实现(Pandas)

假设你的DataFrame日期列名为date_col,可按以下步骤处理:

  1. 提取财年年份:用字符串提取工具获取FY后的两位数字,转换为四位完整年份(如19转为2019)。
  2. 生成目标日期:将提取到的年份与固定的-01-01拼接,转换为标准日期格式。

示例代码:

import pandas as pd

# 示例DataFrame
df = pd.DataFrame({'date_col': ['FY19 Jun', 'FY20 Feb', 'FY21 Dec']})

# 转换为datetime类型的YYYY-01-01
df['target_date'] = pd.to_datetime(
    df['date_col'].str.extract('FY(\d+)')[0].astype(int) + 2000,
    format='%Y'
)

# 若需要字符串格式的"YYYY-01-01",添加以下步骤
df['target_date_str'] = df['target_date'].dt.strftime('%Y-%m-%d')

特殊情况适配

如果财年包含19xx年(如FY99对应1999年),可通过年份范围判断调整转换逻辑:

def convert_fy_year(fy_str):
    year = int(fy_str.str.extract('FY(\d+)')[0])
    return 2000 + year if year <= 23 else 1900 + year

df['target_date'] = pd.to_datetime(df['date_col'].apply(convert_fy_year), format='%Y')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 21:39:20