Pyspark udf function error in lambda function





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have written a udf function below and it throws me an error. Please help.



Below is my dataset;



df1 = sqlContext.range(0, 1000)
.withColumn('normal1',func.abs(10*func.round(randn(seed=1),2)))
.withColumn('normal2',func.abs(100*func.round(randn(seed=2),2)))
.withColumn('normal3',func.abs(func.round(randn(seed=3),2)))

df1 = df1.withColumn('Y',when(df1.normal1*df1.normal2*df1.normal3>750, 1)
.otherwise(0))


udf function below:



from pyspark.sql import types as T
balancingRatio=0.8
calculateWeights = udf(lambda d:(1 * balancingRatio) if d==0 else (1 * (1.0 - balancingRatio)),T.IntegerType())
weightedDataset = df1.withColumn('classWeightCol', calculateWeights('Y'))
weightedDataset.show()


It takes some time and throw me an error;



Py4JJavaError: An error occurred while calling o670.showString.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0
in stage 25.0 failed 1 times, most recent failure: Lost task 0.0 in stage
25.0 (TID 427, localhost, executor driver): org.apache.spark.SparkException:
Python worker failed to connect back.


What might be the problem?
Thank you.



A simple example on internet that I found is not working also



maturity_udf = udf(lambda age: "adult" if age >=18 else "child", 
T.StringType())
df = sqlContext.createDataFrame([{'name': 'Alice', 'age': 1}])
df.withColumn("maturity", maturity_udf(df.age)).show()


Not: I got python 3.7.1 and spark 2.4










share|improve this question

























  • What is T.IntegerType() exactly? shouldn't be just IntegerType()?

    – Ali AzG
    Nov 24 '18 at 12:52











  • from pyspark.sql import types as T

    – melik
    Nov 24 '18 at 12:53













  • What is your pyspark version?

    – Ali AzG
    Nov 24 '18 at 12:56











  • my spark version is 2.4

    – melik
    Nov 24 '18 at 13:01













  • It seems some version issues. try installing version 2.3 and try again.

    – Ali AzG
    Nov 24 '18 at 13:03


















0















I have written a udf function below and it throws me an error. Please help.



Below is my dataset;



df1 = sqlContext.range(0, 1000)
.withColumn('normal1',func.abs(10*func.round(randn(seed=1),2)))
.withColumn('normal2',func.abs(100*func.round(randn(seed=2),2)))
.withColumn('normal3',func.abs(func.round(randn(seed=3),2)))

df1 = df1.withColumn('Y',when(df1.normal1*df1.normal2*df1.normal3>750, 1)
.otherwise(0))


udf function below:



from pyspark.sql import types as T
balancingRatio=0.8
calculateWeights = udf(lambda d:(1 * balancingRatio) if d==0 else (1 * (1.0 - balancingRatio)),T.IntegerType())
weightedDataset = df1.withColumn('classWeightCol', calculateWeights('Y'))
weightedDataset.show()


It takes some time and throw me an error;



Py4JJavaError: An error occurred while calling o670.showString.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0
in stage 25.0 failed 1 times, most recent failure: Lost task 0.0 in stage
25.0 (TID 427, localhost, executor driver): org.apache.spark.SparkException:
Python worker failed to connect back.


What might be the problem?
Thank you.



A simple example on internet that I found is not working also



maturity_udf = udf(lambda age: "adult" if age >=18 else "child", 
T.StringType())
df = sqlContext.createDataFrame([{'name': 'Alice', 'age': 1}])
df.withColumn("maturity", maturity_udf(df.age)).show()


Not: I got python 3.7.1 and spark 2.4










share|improve this question

























  • What is T.IntegerType() exactly? shouldn't be just IntegerType()?

    – Ali AzG
    Nov 24 '18 at 12:52











  • from pyspark.sql import types as T

    – melik
    Nov 24 '18 at 12:53













  • What is your pyspark version?

    – Ali AzG
    Nov 24 '18 at 12:56











  • my spark version is 2.4

    – melik
    Nov 24 '18 at 13:01













  • It seems some version issues. try installing version 2.3 and try again.

    – Ali AzG
    Nov 24 '18 at 13:03














0












0








0








I have written a udf function below and it throws me an error. Please help.



Below is my dataset;



df1 = sqlContext.range(0, 1000)
.withColumn('normal1',func.abs(10*func.round(randn(seed=1),2)))
.withColumn('normal2',func.abs(100*func.round(randn(seed=2),2)))
.withColumn('normal3',func.abs(func.round(randn(seed=3),2)))

df1 = df1.withColumn('Y',when(df1.normal1*df1.normal2*df1.normal3>750, 1)
.otherwise(0))


udf function below:



from pyspark.sql import types as T
balancingRatio=0.8
calculateWeights = udf(lambda d:(1 * balancingRatio) if d==0 else (1 * (1.0 - balancingRatio)),T.IntegerType())
weightedDataset = df1.withColumn('classWeightCol', calculateWeights('Y'))
weightedDataset.show()


It takes some time and throw me an error;



Py4JJavaError: An error occurred while calling o670.showString.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0
in stage 25.0 failed 1 times, most recent failure: Lost task 0.0 in stage
25.0 (TID 427, localhost, executor driver): org.apache.spark.SparkException:
Python worker failed to connect back.


What might be the problem?
Thank you.



A simple example on internet that I found is not working also



maturity_udf = udf(lambda age: "adult" if age >=18 else "child", 
T.StringType())
df = sqlContext.createDataFrame([{'name': 'Alice', 'age': 1}])
df.withColumn("maturity", maturity_udf(df.age)).show()


Not: I got python 3.7.1 and spark 2.4










share|improve this question
















I have written a udf function below and it throws me an error. Please help.



Below is my dataset;



df1 = sqlContext.range(0, 1000)
.withColumn('normal1',func.abs(10*func.round(randn(seed=1),2)))
.withColumn('normal2',func.abs(100*func.round(randn(seed=2),2)))
.withColumn('normal3',func.abs(func.round(randn(seed=3),2)))

df1 = df1.withColumn('Y',when(df1.normal1*df1.normal2*df1.normal3>750, 1)
.otherwise(0))


udf function below:



from pyspark.sql import types as T
balancingRatio=0.8
calculateWeights = udf(lambda d:(1 * balancingRatio) if d==0 else (1 * (1.0 - balancingRatio)),T.IntegerType())
weightedDataset = df1.withColumn('classWeightCol', calculateWeights('Y'))
weightedDataset.show()


It takes some time and throw me an error;



Py4JJavaError: An error occurred while calling o670.showString.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0
in stage 25.0 failed 1 times, most recent failure: Lost task 0.0 in stage
25.0 (TID 427, localhost, executor driver): org.apache.spark.SparkException:
Python worker failed to connect back.


What might be the problem?
Thank you.



A simple example on internet that I found is not working also



maturity_udf = udf(lambda age: "adult" if age >=18 else "child", 
T.StringType())
df = sqlContext.createDataFrame([{'name': 'Alice', 'age': 1}])
df.withColumn("maturity", maturity_udf(df.age)).show()


Not: I got python 3.7.1 and spark 2.4







lambda error-handling pyspark user-defined-functions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 14:53







melik

















asked Nov 24 '18 at 12:06









melikmelik

175213




175213













  • What is T.IntegerType() exactly? shouldn't be just IntegerType()?

    – Ali AzG
    Nov 24 '18 at 12:52











  • from pyspark.sql import types as T

    – melik
    Nov 24 '18 at 12:53













  • What is your pyspark version?

    – Ali AzG
    Nov 24 '18 at 12:56











  • my spark version is 2.4

    – melik
    Nov 24 '18 at 13:01













  • It seems some version issues. try installing version 2.3 and try again.

    – Ali AzG
    Nov 24 '18 at 13:03



















  • What is T.IntegerType() exactly? shouldn't be just IntegerType()?

    – Ali AzG
    Nov 24 '18 at 12:52











  • from pyspark.sql import types as T

    – melik
    Nov 24 '18 at 12:53













  • What is your pyspark version?

    – Ali AzG
    Nov 24 '18 at 12:56











  • my spark version is 2.4

    – melik
    Nov 24 '18 at 13:01













  • It seems some version issues. try installing version 2.3 and try again.

    – Ali AzG
    Nov 24 '18 at 13:03

















What is T.IntegerType() exactly? shouldn't be just IntegerType()?

– Ali AzG
Nov 24 '18 at 12:52





What is T.IntegerType() exactly? shouldn't be just IntegerType()?

– Ali AzG
Nov 24 '18 at 12:52













from pyspark.sql import types as T

– melik
Nov 24 '18 at 12:53







from pyspark.sql import types as T

– melik
Nov 24 '18 at 12:53















What is your pyspark version?

– Ali AzG
Nov 24 '18 at 12:56





What is your pyspark version?

– Ali AzG
Nov 24 '18 at 12:56













my spark version is 2.4

– melik
Nov 24 '18 at 13:01







my spark version is 2.4

– melik
Nov 24 '18 at 13:01















It seems some version issues. try installing version 2.3 and try again.

– Ali AzG
Nov 24 '18 at 13:03





It seems some version issues. try installing version 2.3 and try again.

– Ali AzG
Nov 24 '18 at 13:03












1 Answer
1






active

oldest

votes


















1














You need to disable fork safety by setting the OBJC_DISABLE_INITIALIZE_FORK_SAFETY variable to YES This solved the issue for me.



import os
os.environ['OBJC_DISABLE_INITIALIZE_FORK_SAFETY'] = 'YES'





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457975%2fpyspark-udf-function-error-in-lambda-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You need to disable fork safety by setting the OBJC_DISABLE_INITIALIZE_FORK_SAFETY variable to YES This solved the issue for me.



    import os
    os.environ['OBJC_DISABLE_INITIALIZE_FORK_SAFETY'] = 'YES'





    share|improve this answer




























      1














      You need to disable fork safety by setting the OBJC_DISABLE_INITIALIZE_FORK_SAFETY variable to YES This solved the issue for me.



      import os
      os.environ['OBJC_DISABLE_INITIALIZE_FORK_SAFETY'] = 'YES'





      share|improve this answer


























        1












        1








        1







        You need to disable fork safety by setting the OBJC_DISABLE_INITIALIZE_FORK_SAFETY variable to YES This solved the issue for me.



        import os
        os.environ['OBJC_DISABLE_INITIALIZE_FORK_SAFETY'] = 'YES'





        share|improve this answer













        You need to disable fork safety by setting the OBJC_DISABLE_INITIALIZE_FORK_SAFETY variable to YES This solved the issue for me.



        import os
        os.environ['OBJC_DISABLE_INITIALIZE_FORK_SAFETY'] = 'YES'






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 11 at 20:54









        Smit ShahSmit Shah

        262




        262
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457975%2fpyspark-udf-function-error-in-lambda-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()