The toPandas method in pyspark is not consistent for null values in numerical columns. Is there a way to force it to be more consistent?
An example
sc is the sparkContext. The spark version is 2.3.2. I'm not sure how to include notebook results, but I'll just comment the outputs. It's pretty straightforward, and you can check it yourself in a notebook.
sparkTest = sc.createDataFrame(
[
(1, 1 ),
(2, None),
(None, None),
],
['a', 'b']
)
sparkTest.show() # all None values are neatly converted to null
pdTest1 = sparkTest.toPandas()
pdTest1 # all None values are NaN
np.isnan(pdTest1['b']) # this a series of dtype bool
pdTest2 = sparkTest.filter(col('b').isNull()).toPandas()
pdTest2 # the null value in column a is still NaN, but the two null in column b are now None
np.isnan(pdTest2['b']) # this throws an error
This is of course problematic when programming, and not being able to predict beforehand if a column will be all nulls.
Incidentally I wanted to report this as an issue, but I'm not sure where. The github page doesn't seem to have an issues section?