Pyspark日期列上的条件

huangapple go评论62阅读模式
英文:

Pyspark condition on date column

问题

我有一个包含两个日期列start_date和end_date的pyspark数据框。
现在我想从df中获取start_date < today < end_date的数据。

我正在按以下方式操作 -

今天 = datetime.date.today()
df = df.filter((F.col("start_date") < today) & (F.col("end_date") > today))

然而,它会抛出错误,因为“and”条件在日期类型上不起作用。

提前感谢。

英文:

I have a pyspark dataframe with two date columns start_date and end_date.
Now I want to get data from df where start_date < today < end_date

I am doing as below -

today = datetime.date.today()
df=df.filter(C(&quot;start_date&quot;)&lt;today &amp; C(&quot;end_date&quot;)&gt;today))

However it throws error as "and" condition is not working on date type.

Thank in advance.

答案1

得分: 1

Here's the code with the requested modifications:

看起来你的`闭合括号`放得不对

请尝试以下语法

```python
from pyspark.sql.functions import *
import datetime
df = spark.createDataFrame([('2023-07-23', '2023-07-25'), ('2023-07-24', '2023-07-24')], ['start_date', 'end_date'])

today = datetime.date.today()
df = df.filter((col('start_date') < today) & (col('end_date') > today))
df.show(10, False)

# +----------+----------+
# |start_date|end_date  |
# +----------+----------+
# |2023-07-23|2023-07-25|
# +----------+----------+

这是带有所请求的修改的代码。

英文:

Seems your closing brackets are not in right place.

Try with below syntax.

from pyspark.sql.functions import *
import datetime
df = spark.createDataFrame([(&#39;2023-07-23&#39;,&#39;2023-07-25&#39;), (&#39;2023-07-24&#39;,&#39;2023-07-24&#39;)], (&quot;start_Date&quot;, &quot;end_date&quot;))

today = datetime.date.today()
df=df.filter((col(&quot;start_date&quot;)&lt; today) &amp; (col(&quot;end_date&quot;)&gt;today))
df.show(10,False)

#+----------+----------+
#|start_Date|end_date  |
#+----------+----------+
#|2023-07-23|2023-07-25|
#+----------+----------+

huangapple
  • 本文由 发表于 2023年7月24日 19:29:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定