Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Python converts "NA" to NaN

Hello

I've recently started using Python when building queries for some more advanced data manipulation. However, it seems like Python (more specifically the Pandas library) automatically assumes the text value "NA" should be interpreted as NaN (i.e. a null value). This is problematic, for example, when working with country ISO codes, where "NA" represents Namibia. Example:

Original data:

2019-05-03 10_09_11-Untitled - Power Query Editor.png

Python script (empty, i.e. no manipulations taking place):

= Python.Execute("",[dataset=Source])

Resulting data:

2019-05-03 10_10_41-Untitled - Power Query Editor.png

From some quick Internet research, this appears to be standard functionality of the Pandas library, but can be avoided by supplying an additional argument when creating the DataFrame (see e.g. https://stackoverflow.com/questions/16596188/pandas-convert-na-to-nan). However, since Power BI creates the DataFrame before the script is executed, I can't prevent this unintended type conversion.

Has anyone encountered this problem before, or can anyone provide input on how to circumvent it?

Thanks in advance.

2 REPLIES 2
v-juanli-msft
Community Support
Community Support

Hi @Anonymous 

A temperory method you may have a try:

Replace the value "NA" with another spefic value before executing python script in Power BI, 

After importing the data in Power BI, use Query editor to replace the value (Queries editor->Transform->Replace values).


Best Regards

Maggie

 

Anonymous
Not applicable

Hi @v-juanli-msft 

 

Thanks for your response!

This does seem to work, but it is of course a bit of a hack, so I hope a more elegant solution could be found in the future. It does seem like Python/Pandas is the culprit here, but ideally Power BI would provide an option to bypass this strange behaviour.

 

Should I do anything more to report this, or is this post enough?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.