我想在 PySpark 中使用 Window() 的 rangebetween 函数,以毫秒为参数。
我正在尝试这样做:
df = df.withColumn("timestamp_ms", F.col("Dates").cast("double"))
w = (Window().orderBy("timestamp_ms").rangeBetween(-0.1, 0.0))
但是由于 rangeBetween 方法中的浮点参数 (-0.1),我得到了一个错误。
py4j.protocol.Py4JError: An error occurred while calling o73.rangeBetween. Trace:
py4j.Py4JException: Method rangeBetween([class java.lang.Double, class java.lang.Double]) does not exist
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326)
at py4j.Gateway.invoke(Gateway.java:274)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:238)
at java.lang.Thread.run(Thread.java:748)
是否有其他方法可以在(-0.1 秒,0)之间创建这些窗口?
*timestamp_ms 是一列毫秒分辨率的时间戳
*Dates 是具有以下格式的日期的列:“2019-07-26 08:56:07.171”
谢谢!
最佳答案
我这样做解决了这个问题:
df = df.withColumn("timestamp_ms", F.col("Dates").cast("double"))
df = df.withColumn("timestamp_ms", F.col("timestamp_ms")*1000)
w = (Window().orderBy("timestamp_ms").rangeBetween(-100, 0))
我注意到,如果我将“timestamp_ms”列乘以 1000,我将得到一列整数数据类型的时间戳,分辨率为毫秒。现在我可以使用 rangeBetween (-0.1 secs, 0) 来创建 Window()。
关于python - 如何在 PySpark window() 中使用毫秒作为参数。函数之间的范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62394415/