07-07-2023 11:13 AM
I get an PythonException: float() argument must be a string or a number, not 'NoneType' when attempting to save a DataFrame as a Delta Table.
Here's the line of code that I am running:
```
df.write.format("delta").saveAsTable("schema1.df_table", mode="overwrite")
root
|-- ts: timestamp (nullable = true)
|-- _source: string (nullable = true)
|-- lat: decimal(9,6) (nullable = true)
|-- lng: decimal(9,6) (nullable = true)
|-- id: string (nullable = false)
|-- mm-yyyy: date (nullable = true)
|-- hid: string (nullable = true)
```
PythonException: An exception was thrown from the Python worker. Please see the stack trace below. 'TypeError: float() argument must be a string or a number, not 'NoneType'', from , line 3. Full traceback below: Traceback (most recent call last): File "", line 3, in TypeError: float() argument must be a string or a number, not 'NoneType'
How do we handle `None` in the spark DataFrame? I'd like to identify which rows / columns contain `None` and drop them?
07-07-2023 02:49 PM
@kll - can you please try using try/catch option to convert None to an unique value for the decimal columns.
07-07-2023 02:49 PM
something like the below
try:
if lng is not None:
new_lng = lng
else:
new_lng = 0
07-12-2023 02:07 AM
Hi @kll
Hope you are well. Just wanted to see if you were able to find an answer to your question and would you like to mark an answer as best? It would be really helpful for the other members too.
Cheers!
07-13-2023 09:30 AM
Even though the code throws the issue while write, the issue can be in the code before as spark is lazily evaluated. The error "TypeError: float() argument must be a string or a number, not 'NoneType'" generally comes when we pass a variable to float() function and the value of the variable at that time is None. I would recommend you to check for such issues.
Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.
If there isn’t a group near you, start one and help create a community that brings people together.
Request a New Group