Pyspark日期列上的条件

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

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 -

  1. today = datetime.date.today()
  2. 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:

  1. 看起来你的`闭合括号`放得不对
  2. 请尝试以下语法
  3. ```python
  4. from pyspark.sql.functions import *
  5. import datetime
  6. df = spark.createDataFrame([('2023-07-23', '2023-07-25'), ('2023-07-24', '2023-07-24')], ['start_date', 'end_date'])
  7. today = datetime.date.today()
  8. df = df.filter((col('start_date') < today) & (col('end_date') > today))
  9. df.show(10, False)
  10. # +----------+----------+
  11. # |start_date|end_date |
  12. # +----------+----------+
  13. # |2023-07-23|2023-07-25|
  14. # +----------+----------+

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

英文:

Seems your closing brackets are not in right place.

Try with below syntax.

  1. from pyspark.sql.functions import *
  2. import datetime
  3. 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;))
  4. today = datetime.date.today()
  5. df=df.filter((col(&quot;start_date&quot;)&lt; today) &amp; (col(&quot;end_date&quot;)&gt;today))
  6. df.show(10,False)
  7. #+----------+----------+
  8. #|start_Date|end_date |
  9. #+----------+----------+
  10. #|2023-07-23|2023-07-25|
  11. #+----------+----------+

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:

确定