Pyspark 以 yyyy/mm/dd 格式过滤日期

Pyspark to filter date in format yyyy/mm/dd

我想以 yyy-mm-dd 格式过滤日期时间列的数据。但是,它的字符串值和日期有一个时间戳相关联。我不想在我的专栏中使用这个时间戳。我正在为此使用 Pyspark。

日期格式- 2021/09/23 09:00:00+00,

格式待完成- 2021-09-23

from pyspark.sql.functions import to_date
df = df_pyspark.withColumn("date_only",to_date(col("DateTime"))) #col name in data is DateTime

date_only 显示空值。我应该如何接近这里?

使用功能时 to_date, you need to pass a format string. The format string can be created using the official documentation for simpleDateFormat - 可直接从 spark 文档中获得。
在您的情况下,格式为 yyyy/MM/dd HH:mm:ssX :

df.withColumn("t", F.to_date("datetime", "yyyy/MM/dd HH:mm:ssX")).show(truncate=False)                                                                                              
+----------------------+----------+
|DateTime              |t         |
+----------------------+----------+
|2021/09/23 09:00:00+00|2021-09-23|
+----------------------+----------+

然后您可以过滤日期:

df.where(F.to_date("datetime", "yyyy/MM/dd HH:mm:ssX") == "2021-09-23").show()                                                                                                       
+--------------------+                                                          
|            DateTime|
+--------------------+
|2021/09/23 09:00:...|
+--------------------+