Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use nullable inferred schema in function apply #1897

Merged
merged 1 commit into from
Nov 9, 2020

Conversation

HyukjinKwon
Copy link
Member

This PR proposes to use nullable schema in when to apply a function. We take some to infer the schema. Usually types are matched but null or NaN is sparse often. This behaviour is batch with Spark's JSON schema inference as well.

from pyspark.sql import SparkSession
import databricks.koalas as ks
import numpy as np

spark = SparkSession.builder.getOrCreate()
data = list()
for i in range(1, 10000):
  data.append((str(i % 100), np.nan if i % 9999 == 0 else float(i),))

sdf = spark.createDataFrame(data, 'a string, b float').repartition(10)
kdf = sdf.to_koalas()

def f(df):
  return df

df = kdf\
  .groupby('a')\
  .apply(f)

df.to_spark().printSchema()
df[df['b'] == -1]

Before:

root
 |-- a: string (nullable = false)
 |-- b: float (nullable = false)

java.lang.IllegalStateException: Value at index is null
...

After:

root
 |-- a: string (nullable = true)
 |-- b: float (nullable = true)

Empty DataFrame
Columns: [a, b]
Index: []

Resolves #1885

Copy link
Collaborator

@ueshin ueshin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@ueshin
Copy link
Collaborator

ueshin commented Nov 9, 2020

Thanks! merging.

@ueshin ueshin merged commit 848f107 into databricks:master Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

udf schema inference should not set column nullable=false
2 participants