【发布时间】:2019-08-25 10:22:19
【问题描述】:
我有一个 Pyspark 数据框,其中包含一个日期列“报告日期”(类型:字符串)。从日期中提取年份后,我想获取另一列的计数。
如果我使用字符串日期列,我可以得到计数。
crimeFile_date.groupBy("Reported Date").sum("Offence Count").show()
我得到了这个输出
+-------------+------------------+
|Reported Date|sum(Offence Count)|
+-------------+------------------+
| 13/08/2010| 342|
| 6/10/2011| 334|
| 27/11/2011| 269|
| 12/01/2012| 303|
| 22/02/2012| 286|
| 31/07/2012| 276|
| 25/04/2013| 222|
+-------------+------------------+
为了从“报告日期”中提取年份,我已将其转换为日期格式 (using this approach) 并将列命名为“日期”。 但是,当我尝试使用相同的代码按新列分组并进行计数时,我收到一条错误消息。
crimeFile_date.groupBy(year("Date").alias("year")).sum("Offence Count").show()
TypeError: strptime() argument 1 must be str, not None
这是数据架构:
root
|-- Offence Count: integer (nullable = true)
|-- Reported Date: string (nullable = true)
|-- Date: date (nullable = true)
有没有办法解决这个错误?或使用另一种方法提取年份? 谢谢
【问题讨论】: