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

Fixes string fill values converted to "nan" #11

Merged
merged 4 commits into from
Jan 21, 2024
Merged

Conversation

5tefan
Copy link
Owner

@5tefan 5tefan commented Jan 21, 2024

Summary

Fixes an issue where fill values in string type variables were improperly converted to "nan".

The issue was caused by the following code:

def get_fill_for(variable):
    datatype = np.dtype(variable["datatype"])
    try:
        return datatype.type(np.nan)
    except ValueError:
        # for an integer type, there is no concept of nan, this will raise
        # ValueError: cannot convert float NaN to integer, so use -9999 instead
        # main reason for this complexity is to handle exis integer datatypes
        nc_default_fill = datatype.type(nc.default_fillvals[datatype.str[1:]])
        return datatype.type(variable["attributes"].get("_FillValue", nc_default_fill))

Surprisingly, the call datatype.type(np.nan) does not raise a ValueError for string types. For example, np.dtype("<U0").type(np.nan) returns in the string "nan". Fixed by explicitly detecting datatypes.

I think I'd handle fill values all together differently if I were doing this again today... but that would be a big effort.

Thank you @A-Mahon for finding and reporting this bug!

Fixes an issue where the fill value for a string variable was converted
to the string "nan". Adds a test for this case.
@5tefan 5tefan merged commit 7260981 into main Jan 21, 2024
6 checks passed
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.

1 participant