【问题标题】:cuDF - groupby UDF to support datetimecuDF - groupby UDF 支持日期时间
【发布时间】:2020-04-08 20:27:15
【问题描述】:

我有一个包含以下列的 cuDF 数据框:

columns = ["col1", "col2", "dt"]

datetime64[ns] 形式的 (dt)。

我想编写一个 UDF 以应用于此数据帧中的每个组,并为每个组获取最大的 dt。这是我正在尝试的,但似乎 numba 不支持 UDF 中的 datetime64[ns] 值。

def f1(dt, out):
   l = len(dt)
   maxvalue = dt[0]
   for i in  range(cuda.threadIdx.x, l, cuda.blockDim.x):
      if dt[i] > maxvalue:
         maxvalue = dt[i]
   out[:0] = maxvalue

gdf = df.groupby(["col1", "col2"], method="cudf")
df = gdf.apply_grouped(f1, incols={"dt": "dt"}, outcols=dict(out=numpy.datetime64))

这是我得到的错误:

This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<numba.cuda.compiler.DeviceFunctionTemplate object at 0x7effda063510>)
[2] During: typing of call at <string> (10)

我有类似的函数,可以很好地处理整数和浮点数。这是否意味着 numba 不支持日期时间?

【问题讨论】:

    标签: numba numba-pro cudf


    【解决方案1】:

    Apply_groups 不会给你我认为你想要的东西,这是 groupby on max dt。您需要在dt 上使用aggs 和最大值。 cudf 的 groupby 函数将完成其余的工作。要在datetime64[ms] 中获取您的值,请使用astype(),并将其保存回数据帧(非常快)。 看我的例子:

    import cudf
    a = cudf.DataFrame({"col1": [1, 1, 1, 2, 2, 2], "col2": [1, 2, 1, 1, 2, 1], "dt": [10000000, 2000000, 3000000, 100000, 2000000, 40000000]}) 
    a['dt'] = a['dt'].astype('datetime64[ns]')
    print(a)
    a['dt'] = a['dt'].astype('datetime64[ms]')
    print(a)
    gdf = a.groupby(["col1", "col2"]).agg({'dt':'max'})
    print(gdf.head())
    

    dt 列值将从 1970 年 1 月 1 日起格式化为 0.1-40 毫秒(以纳秒计),为您提供打印输出

       col1  col2                         dt
    0     1     1 1970-01-01 00:00:00.010000
    1     1     2 1970-01-01 00:00:00.002000
    2     1     1 1970-01-01 00:00:00.003000
    3     2     1 1970-01-01 00:00:00.000100
    4     2     2 1970-01-01 00:00:00.002000
    5     2     1 1970-01-01 00:00:00.040000
    
       col1  col2                      dt
    0     1     1 1970-01-01 00:00:00.010
    1     1     2 1970-01-01 00:00:00.002
    2     1     1 1970-01-01 00:00:00.003
    3     2     1 1970-01-01 00:00:00.000
    4     2     2 1970-01-01 00:00:00.002
    5     2     1 1970-01-01 00:00:00.040
    
                                   dt
    col1 col2                        
    1    1    1970-01-01 00:00:00.010
         2    1970-01-01 00:00:00.002
    2    1    1970-01-01 00:00:00.040
         2    1970-01-01 00:00:00.002
    

    【讨论】:

      猜你喜欢
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      相关资源
      最近更新 更多