0

I have a table with dates and comments.

dob        | comment
---------------------------
1960-12-01 | this is useful

And I want a new column with this type:

value_type = T.StructType(
    [
       T.StructField("extra",T.MapType(T.StringType(), T.StringType(), True), True),
       T.StructField("date", T.StringType(), True),
       T.StructField("from_date", T.StringType(), True),
       T.StructField("to_date", T.StringType(), True),
       T.StructField("value", T.StringType(), True),
    ]
)

I need to:

  1. put the df.date into the date field of the struct and
  2. put the df.comment into the extra map of the struct

thanks to blackbishop I figured out how to do the first part here - and i tried to use .withField() to update the map but it throws an error:

I tried:

(df
.withColumn("new_col", 
            F.struct(*[F.lit(None).cast(f.dataType).alias(f.name) 
                       for f in value_type.fields]))
.withColumn("new_col", (F.col("new_col")
                         .withField("date", F.col("dob"))
                         .withField("extra.value", F.col("comment")))))

But I get the following error:

AnalysisException: cannot resolve 'update_fields(update_fields(new_col, WithField(dob), WithField(dob)).extra, WithField(dob))' due to data type mismatch: struct argument should be struct type, got: map<string,string>; 

I am confused as per why it would not work with the map inside the struct?

Thanks :)

1 Answer 1

0

I figured it out!

(df
.withColumn("new_col", 
            F.struct(*[F.lit(None).cast(f.dataType).alias(f.name) 
                       for f in value_type.fields]))
.withColumn("new_col", (F.col("new_col")
                         .withField("date", F.col("dob"))
                         .withField("extra", 
                                    F.create_map(F.lit("my_key"), F.col("comment")))))

The problem was that I was not actually passing a map to a map type!

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.