question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Converting NaN objects falsely turns NaN into None

See original GitHub issue

When converting a DataFrame/Series of type object (i.e. Strings) with np.nan values to Koalas DataFrames and back, the former np.nan values are replaced with None as can be seen below:

>>> ks.Series(['a', np.nan]).to_pandas()
0       a
1    None
Name: 0, dtype: object

However, the following output would be expected instead:

0      a
1    NaN
Name: 0, dtype: object

I assume this behavior is caused by the fact that Spark does not support NaN values for String columns but uses None instead. Consequently, there is probably no definite way to decide whether a None value in Spark should be converted to a Python NaN or None at the time when the conversion from Spark to pandas happens. However, I would argue that in doubt converting to NaN makes more sense in most cases than None and should thus be the default.

What is your opinion on this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
thunterdbcommented, May 23, 2019

Note that performance is not going to be an issue here in any case: the internal representation in spark of None is a bit mask and the scalar values are always allocated.

Here is what I suggest to do when we convert the data between pandas and spark:

  • for scalar values that have an intrinsic representation of NaN (float, double): pandas -> spark: NaN is converted to non-null NaN spark -> pandas: null or non-null NaN is converted to NaN
  • for scalar values that do not have an intrinsic representation of NaN (int, short, …): pandas does not allow that to begin with so there is a loss of precision here. it will depend on the behaviour of the python pickler and of arrow
  • for objects (strings, structs, …): Spark can optimize the memory layout when the value is null, so: pandas -> spark: NaN -> null, None -> non-null None (I did not try, may not always work) spark -> pandas: null -> NaN non-null None -> None

@floscha does that cover all the use cases that you are thinking of? Can you write a couple of test cases to validate that this is the expected behaviour.

Also, be aware that the nullability support in spark can be brittle, some corner cases especially after UDFs or joins will forget nullability info.

0reactions
itholiccommented, Aug 17, 2021

Let me close this for now, since It can’t be supported because Spark can’t tell if the null value of object type should be None or NaN when converting to pandas anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Replacing Pandas or Numpy Nan with a None to use ...
MysqlDB doesn't seem understand 'nan' and my database throws out an error saying nan is not in the field list. I need to...
Read more >
Dealing with Missing Values NaN and None in Python - Medium
As summary, NaN and None are different data types in Python. However, when it comes to missing values detection and elimination, pandas.
Read more >
Missing values in pandas (nan, None, pd.NA) - nkmk note
In pandas, a missing value (NA: not available) is mainly represented by nan (not a number). None is also considered a missing value....
Read more >
pandas.DataFrame.to_json — pandas 1.5.2 documentation
Convert the object to a JSON string. Note NaN's and None will be converted to null and datetime objects will be converted to...
Read more >
Handling Missing Data | Python Data Science Handbook
Notice that in addition to casting the integer array to floating point, Pandas automatically converts the None to a NaN value. (Be aware...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found