gpt4 book ai didi

pyspark - 属性错误 : 'datetime.timedelta' object has no attribute '_get_object_id'

转载 作者:行者123 更新时间:2023-12-05 07:24:39 24 4
gpt4 key购买 nike

尝试创建一个 30 分钟的时间段,但出现以下属性错误:

'datetime.timedelta' object has no attribute '_get_object_id'

被摄取的列称为时间戳并保存模式类型时间戳。例如

2019-02-01T15:53:44Z

鉴于以下代码应该能够获取时间戳,我无法弄清楚为什么我会收到错误消息。

    def ceil_dt(dt, delta):
return dt + (datetime.min - dt) % delta

df = df.withColumn("bucket_timestamp", ceil_dt(df.timestamp, timedelta(minutes=30)))
return df

最佳答案

您需要使用用户定义函数 (UDF):

from pyspark.sql.types import *
from pyspark.sql import functions as f
from pyspark.sql import Row
from datetime import datetime, timedelta

# example DF
date = datetime.strptime('2019-02-01T15:53:44', '%Y-%m-%dT%H:%M:%S')
df = sc.parallelize([Row(timestamp=date)]).toDF()

# define UDF based on OP's function
ceil_dt = (f.udf(lambda dt, delta: dt + (datetime.min - dt) % timedelta(minutes=delta),
TimestampType()))

# now apply to timestamp columns
df = df.withColumn("bucket_timestamp", ceil_dt(df.timestamp, f.lit(30)))

关于pyspark - 属性错误 : 'datetime.timedelta' object has no attribute '_get_object_id' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55277955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com