Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dataframe 如何将一年中的月份转换为第一个月_Dataframe_Apache Spark_Date_Pyspark_Apache Spark Sql - Fatal编程技术网

Dataframe 如何将一年中的月份转换为第一个月

Dataframe 如何将一年中的月份转换为第一个月,dataframe,apache-spark,date,pyspark,apache-spark-sql,Dataframe,Apache Spark,Date,Pyspark,Apache Spark Sql,我正在尝试获取从当前日期到前3年的日期范围 而之前的3年数据应该从1月1日开始。 下面是我尝试过的代码片段 dateDF = spark.sql("select current_date() as current_date, add_months(current_date(),-36) as end_date") dateDF = dateDF.withColumn("end_date_first_date", F.trunc("end_dat

我正在尝试获取从当前日期到前3年的日期范围 而之前的3年数据应该从1月1日开始。 下面是我尝试过的代码片段

dateDF = spark.sql("select current_date() as current_date, add_months(current_date(),-36) as end_date")
dateDF =  dateDF.withColumn("end_date_first_date", F.trunc("end_date", "month")).withColumn("end_date_first_date_first_month",lit(''))
dateDF.show()

+------------+----------+-------------------+-------------------------------+
|current_date|  end_date|end_date_first_date|end_date_first_date_first_month|
+------------+----------+-------------------+-------------------------------+
|  2021-04-09|2018-04-09|         2018-04-01|                               |
+------------+----------+-------------------+-------------------------------+
在这里,我可以得到第一次约会,但我如何才能得到第一个月。是否有任何预定义的功能

预期产量

+------------+----------+-------------------+-------------------------------+
|current_date|  end_date|end_date_first_date|end_date_first_date_first_month|
+------------+----------+-------------------+-------------------------------+
|  2021-04-09|2018-04-09|         2018-04-01|   2018-01-01                  |
+------------+----------+-------------------+-------------------------------+

只需在
F.trunc
中使用
year
而不是
month

dateDF = dateDF.withColumn(
    "end_date_first_date", 
    F.trunc("end_date", "month")
).withColumn(
    "end_date_first_date_first_month",
    F.trunc("end_date", "year")
)

只需在
F.trunc
中使用
year
而不是
month

dateDF = dateDF.withColumn(
    "end_date_first_date", 
    F.trunc("end_date", "month")
).withColumn(
    "end_date_first_date_first_month",
    F.trunc("end_date", "year")
)

是否有一种方法可以告诉您从结束日期选择一个特定的月份。例如:这里的结束日期是2018-01-01,假设我需要的月份是“10月”,即2018-10-01,使用F.trunc是否也可以实现这一点?您可以使用
add\u months
,例如
F.add\u months(F.trunc(“结束日期”,“年份”),9)
哦,好的。这可以通过调用add_月来实现。有一种方法可以告诉你从结束日期中选择一个特定的月份。例如:这里的结束日期是2018-01-01,假设我需要的月份是“10月”,即2018-10-01,使用F.trunc是否也可以实现这一点?您可以使用
add\u months
,例如
F.add\u months(F.trunc(“结束日期”,“年份”),9)
哦,好的。这可以通过调用add_月来实现。知道了