将具有多个参数的自定义函数应用于pandas列时的UnboundLocalError

时间:2016-11-08 17:48:11

标签: python pandas

我通过应用具有多个参数的函数来练习从这些数据中获取大熊猫系列。我的代码和数据如下

from datetime import datetime
import pandas as pd
from dateutil.relativedelta import relativedelta

def getage(x,now,status):
    if x != None:
        date = pd.to_datetime(x,format='%d%m%Y').date()
        age = relativedelta(now, date).years
        if status:
            age = relativedelta(now, date).years
        else:
            age = 'Unknown'
    return age

today = datetime.now().date()
data = pd.DataFrame({'ID':['001','002','003'],
                   'BirthDate':['25031978',None,'12122012'],
                   'Values':[5,10,15]})
customer_age = df['BirthDate'].apply(getage,args=(today,True,))

所以,我的数据看起来像这样

Index   BirthDate   ID   Values
0       25031978    001   5
1       None        002   10
3       12122012    003   15

将getage函数应用于“生日日期”列后,我想获得一个pandas系列(customer_age),如下所示:

Index   0
0      38       # because the date difference (year) between BirthDate and today is 38
1      Unknown
2      4        # because the date difference (year) between BirthDate and today is 4

但是,我收到如下错误:

UnboundLocalError: local variable 'age' referenced before assignment

我可以提出修复建议吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

正如user2357112指出的那样,当x为1时,你需要为年龄设置一个值。你可以通过添加另一个条件来实现。

def getage(x,now,status):
    if x != None:
        date = pd.to_datetime(x,format='%d%m%Y').date()
        age = relativedelta(now, date).years
        if status:
            age = relativedelta(now, date).years

    if x == None or not status: # accounts for when x is None
       age = 'Unknown'
    return age