我試圖找到季度開始日期的日期列。我得到了預期的結果,當我把它寫使用selectExpr()但當我添加相同的邏輯.withColumn()得到TypeError:列不是iterable
我使用方法如下
解決方案:- df = df.selectExpr (“*”,“date_sub (history_effective_date, dayofmonth (history_effective_date) 1) history_effective_month”)selectExpr: df.selectExpr (“add_months (history_effective_month,(月(history_effective_month) % 3) + 1)”,“history_effective_month”),告訴(5)
輸出-
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + |四分之一| history_effective_month | + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | 2017-07-01 | 2017-07-01 | | 2016-04-01 | 2016-04-01 | | 2015-10-01 | 2015-10-01 | | 2012-01-01 | 2012-01-01 | | 2012-01-01 | 2012-01-01 | + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
withColumn (): -
df.withColumn (“history_effective_quarter add_months (history_effective_month, -(月(history_effective_month) % 3) + 1))
TypeError回溯(去年)最近調用< ipython -輸入- 259 - 0 - bb78d27d2a7 > <模塊> ()1 # pricerxDF.selectExpr (“add_months (history_effective_month,(月(history_effective_month) % 3) + 1)季度”、“history_effective_month”),告訴(5)- - - - - > 2 pricerxDF.withColumn (“history_effective_quarter add_months (history_effective_month, -(月(history_effective_month) % 3) + 1))
~ / anaconda3 / lib / python3.6 /網站/ pyspark / sql /功能。py add_months(開始,個月)968 969 sc = SparkContext“””。_active_spark_context - - > 970年返回列(sc._jvm.functions.add_months (_to_java_column(開始),個月))971 972
~ / anaconda3 / lib / python3.6 /網站/ py4j / java_gateway。py在調用(自我,* args) 1122 1123 def調用(自我,* args): - > 1124 args_command temp_args = self._build_args (* args) 1125 1126命令=原型。CALL_COMMAND_NAME + \
~ / anaconda3 / lib / python3.6 /網站/ py4j / java_gateway。py在_build_args(self, *args) 1086 def _build_args(self, *args): 1087 if self.converters is not None and len(self.converters) > 0: -> 1088 (new_args, temp_args) = self._get_args(args) 1089 else: 1090 new_args = args
~ / anaconda3 / lib / python3.6 /網站/ py4j / java_gateway。py在_get_args(self, args) 1073 for converter in self.gateway_client.converters: 1074 if converter.can_convert(arg): -> 1075 temp_arg = converter.convert(arg, self.gateway_client) 1076 temp_args.append(temp_arg) 1077 new_args.append(temp_arg)
~ / anaconda3 / lib / python3.6 /網站/ py4j / java_collections。py在convert(self, object, gateway_client) 498 ArrayList = JavaClass("java.util.ArrayList", gateway_client) 499 java_list = ArrayList() --> 500 for element in object: 501 java_list.add(element) 502 return java_list
~ / anaconda3 / lib / python3.6 /網站/ pyspark / sql /列。py在iterdef(自我)248 249iter(自我):- - > 250年籌集TypeError(列不是iterable) 251 252 #字符串方法
TypeError:列不是iterable