【问题标题】:Get age from DOB - Dask从 DOB 获取年龄 - Dask
【发布时间】:2023-02-15 19:18:45
【问题描述】:

当我有一个“DOB”列时,如何包含一个名为“Age_cust”的新列?

from datetime import date
today = date.today()

ddf = ddf.assign(Age_cust=cust.map_partitions(lambda x: x['DOB'] - today))

从这里我得到错误

ValueError: Metadata inference failed in `lambda`.

You have supplied a custom function and Dask is unable to 
determine the type of output that that function returns. 

To resolve this please provide a meta= keyword.
The docstring of the Dask function you ran should have more information.

Original error is below:
------------------------
TypeError("unsupported operand type(s) for -: 'DatetimeArray' and 'datetime.date'")

【问题讨论】:

    标签: datetime dask


    【解决方案1】:

    这可能不是最有效的方法,而且我不熟悉 DASK,但假设您的数据框称为 df,以下方法有效:

    import datetime as dt
    from datetime import date
    
    year = df["DOB"].dt.year.values
    month = df["DOB"].dt.month.values
    day = df["DOB"].dt.day.values
    
    today = date.today()
    
    df["age"] = 0
    for i in range(len(year)):
        age[i] = today.year - age[i] - ((today.month, today.day) < (month[i], day[i]))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      相关资源
      最近更新 更多