5
d = [{'ID': '1', 'pID': 1000, 'startTime':'2018.07.02T03:34:20', 'endTime':'2018.07.03T02:40:20'}, {'ID': '1', 'pID': 1000, 'startTime':'2018.07.02T03:45:20', 'endTime':'2018.07.03T02:50:20'}, {'ID': '2', 'pID': 2000, 'startTime':'2018.07.02T03:34:20', 'endTime':'2018.07.03T02:40:20'}, {'ID': '2', 'pID': 2000, 'startTime':'2018.07.02T03:45:20', 'endTime':'2018.07.03T02:50:20'}]

df = spark.createDataFrame(d)

Dates = namedtuple("Dates", "startTime endTime")


def MergeAdjacentUsage(timeSets):
  DatesArray = []
  for times in timeSets:
    DatesArray.append(Dates(startTime=times.startTime, endTime=times.endTime))
  return DatesArray


MergeAdjacentUsages = udf(MergeAdjacentUsage,ArrayType(Dates()))

df1=df.groupBy(['ID','pID']).agg(MergeAdjacentUsages(F.collect_list(struct('startTime','endTime'))).alias("Times"))

display(df1)

All I want is to set column value to an array of stuct that is returned by UDF. It is giving me error as:

TypeError: new() takes exactly 3 arguments (1 given)

TypeError Traceback (most recent call last) in () 22 return DatesArray 23 ---> 24 MergeAdjacentUsages = udf(MergeAdjacentUsage,ArrayType(Dates())) 25 26 df1=df.groupBy(['ID','pID']).agg(MergeAdjacentUsages(F.collect_list(struct('startTime','endTime'))).alias("Times"))

Any help, idea or hint will be appreciated.

1 Answer 1

7

pyspark does not let user defined Class objects as Dataframe Column Types. Instead we need to create the StructType which can be used similar to a class / named tuple in python.

For example:

from pyspark.sql.types import *
from pyspark.sql.functions import udf
from pyspark.sql import functions as F
# from pyspark.sql.functions import *

d = [{'ID': '1', 'pID': 1000, 'startTime': '2018.07.02T03:34:20', 'endTime': '2018.07.03T02:40:20'},
     {'ID': '1', 'pID': 1000, 'startTime': '2018.07.02T03:45:20', 'endTime': '2018.07.03T02:50:20'},
     {'ID': '2', 'pID': 2000, 'startTime': '2018.07.02T03:34:20', 'endTime': '2018.07.03T02:40:20'},
     {'ID': '2', 'pID': 2000, 'startTime': '2018.07.02T03:45:20', 'endTime': '2018.07.03T02:50:20'}]

df = spark.createDataFrame(d)

# Dates = namedtuple("Dates", "startTime endTime")

schema = ArrayType(StructType([
    StructField("startTime", StringType(), False),
    StructField("endTime", StringType(), False)
]))


MergeAdjacentUsages = udf(lambda xs: xs, schema)

df1 = df.groupBy(['ID', 'pID']).agg(MergeAdjacentUsages(
    F.collect_list(F.struct('startTime', 'endTime'))).alias("Times"))
df1.show(truncate=False)

+---+----+----------------------------------------------------------------------------------------+
|ID |pID |Times                                                                                   |
+---+----+----------------------------------------------------------------------------------------+
|2  |2000|[[2018.07.02T03:34:20, 2018.07.03T02:40:20], [2018.07.02T03:45:20, 2018.07.03T02:50:20]]|
|1  |1000|[[2018.07.02T03:34:20, 2018.07.03T02:40:20], [2018.07.02T03:45:20, 2018.07.03T02:50:20]]|
+---+----+----------------------------------------------------------------------------------------+

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.