Python计算日期列中的年份数

时间:2017-11-21 13:29:53

标签: python

我最近开始使用Python进行编码,而且我很难计算当前日期和给定日期之间的年数。

Dataframe

我想计算每列的年数。 我尝试了这个,但它不起作用:

def Number_of_years(d1,d2):
    if d1 is not None:
        return relativedelta(d2,d1).years

for col in df.select_dtypes(include=['datetime64[ns]']):
    df[col]=Number_of_years(df[col],date.today())

任何人都可以帮我找到解决方案吗?

2 个答案:

答案 0 :(得分:0)

我看到日期的格式是日/月/年。

鉴于所有网格的格式都相同,您可以使用datetime模块解析日期,如下所示:

from datetime import datetime # import module

def numberOfYears(element):
    # parse the date string according to the fixed format
    date = datetime.strptime(element, '%d/%m/%Y')

    # return the difference in the years
    return datetime.today().year - date.year

# make things more interesting by vectorizing this function
function = np.vectorize(numberOfYears)

# This returns a numpy array containing difference between years.
# call this for each column, and you should be good
difference = function(df.Date_creation)

答案 1 :(得分:0)

您的代码基本上是正确的,但您在大熊猫系列中进行操作,因此您无法直接致电relativedelta

def number_of_years(d1,d2):
        return relativedelta(d2,d1).years

for col in df.select_dtypes(include=['datetime64[ns]']):
    df[col]= df[col].apply(lambda d: number_of_years(x, date.today()))