【问题标题】:getting the age from RFC Mex从 RFC Mex 获取年龄
【发布时间】:2020-04-20 02:17:18
【问题描述】:

我有一个数据框,我试图在其中获取用户的年龄,但问题是没有出生日期,因此在我的国家/地区存在某种税号,您可以在其中获取此数据:

ABCD971021XZYABCD971021

其中前 4 个字母代表姓名和姓氏,数字是生日日期 在上面的情况下是 1997/10/21

此时我已经尝试过:

# To slice the RFC
df_v['new_column'] = df_v['RFC'].apply(lambda x: x[4:10])


# Trying to gt the date
from datetime import datetime, timedelta
s = "971021"
date = datetime(year=int(s[0:2]), month=int(s[2:4]), day=int(s[4:6]))

OUT: 0097-10-21

我正在寻找的是看起来像这样的东西。

1997-10-21

【问题讨论】:

    标签: python pandas date datetime slice


    【解决方案1】:

    问题在于税号中没有明确给出千禧年和世纪,并且没有单一的方法可以将两位数的年份转换为四位数的年份。

    例如971021 告诉您出生年份是 xx97,但对于所有 datetime 都知道,这可能意味着 1597 年或 1097 年或 2397 年。

    作为程序员,您必须决定如何编码您对一个人最有可能出生在哪个千年和世纪的假设。例如,一个简单(未经测试)的解决方案可能是:

    year_last_two = int(s[0:2])
    # If the year given is less than 20, this person was most likely born in the 2000's
    if year_last_two < 20:
        year = 2000 + year_last_two
    # Otherwise, the person was most likely born in the 1900's
    else:
        year = 1900 + year_last_two
    date = datetime(year=year, month=int(s[2:4]), day=int(s[4:6]))
    

    当然,此解决方案仅适用于 2019 年,并且还假设没有人超过 100 岁。您可以通过使用当前年份作为拆分点来使其变得更好。

    【讨论】:

      猜你喜欢
      • 2023-02-15
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      相关资源
      最近更新 更多