Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-26449

Missing Dataframe.transform API in Python API

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.4.0
    • Fix Version/s: 3.0.0
    • Component/s: PySpark, SQL
    • Labels:
      None
    • Flags:
      Patch

      Description

      I would like to chain custom transformations as is suggested in this blog post

      This will allow to write something like the following:

       

       

       
      def with_greeting(df):
          return df.withColumn("greeting", lit("hi"))
      
      def with_something(df, something):
          return df.withColumn("something", lit(something))
      
      data = [("jose", 1), ("li", 2), ("liz", 3)]
      source_df = spark.createDataFrame(data, ["name", "age"])
      
      actual_df = (source_df
          .transform(with_greeting)
          .transform(lambda df: with_something(df, "crazy")))
      print(actual_df.show())
      +----+---+--------+---------+
      |name|age|greeting|something|
      +----+---+--------+---------+
      |jose|  1|      hi|    crazy|
      |  li|  2|      hi|    crazy|
      | liz|  3|      hi|    crazy|
      +----+---+--------+---------+
      
      

      The only thing needed to accomplish this is the following simple method for DataFrame:

      from pyspark.sql.dataframe import DataFrame 
      def transform(self, f): 
          return f(self) 
      DataFrame.transform = transform
      

      I volunteer to do the pull request if approved (at least the python part)

       

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                EC83 Erik Christiansen
                Reporter:
                chanansh Hanan Shteingart
                Shepherd:
                Maciej Szymkiewicz
              • Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - 24h
                  24h
                  Remaining:
                  Remaining Estimate - 24h
                  24h
                  Logged:
                  Time Spent - Not Specified
                  Not Specified