Null, infinity, and NaN values
Null, infinity, and not-a-number (NaN) values can unexpectedly appear in your datasets and disrupt calculations if not handled properly. This guide shows you how to detect, filter, and replace these special values to keep your data analysis running smoothly. Not all numeric types support each of these special values.
In computing, null, infinity, and NaN are special values that represent missing, undefined, or indeterminate data. Nulls exist for all Java primitive types, while NaN and infinity exist only for floating-point types.
Nulls represent missing data. Each data type reserves a single, specific value to represent null. For example, Deephaven reserves -32768 as the null value for the primitive short data type, resulting in an adjusted minimum value of -32767 instead of the traditional -32768.
NaNs represent undefined mathematical results. For instance, the logarithm of a negative number is undefined, so that result is represented as NaN.
Infinities represent mathematically infinite values. Unlike NaNs, infinities are well-defined mathematically. For example, any positive number divided by 0 is undefined numerically but mathematically is defined as positive infinity.
Available constants
Deephaven provides constant values representing null, infinity, and NaN values for each numeric type. These values are available in Python as well as built into the query language.
Nulls
The following null constants are available in Deephaven:
NaNs
The following NaN constants are available in Deephaven:
Infinities
The following infinity constants are available in Deephaven:
Built-in vs Python constants
As shown in the code blocks above, the same constants are made available in both the Python API as well as the query language. As with all other operations done in query strings, the built-in constants are the recommended way to use these constants. The Python API constants are provided for convenience and times where the constants are needed in Python code.
Use null, infinity, and NaN values
Nulls, NaNs, and infinities are handled in similar ways in table operations.
Built-in functions
The functions built into the query language gracefully handle null, NaN, and infinity values. For instance, the built-in sin function handles them with no additional steps:
Replace values
Replacing null, NaN, and infinity values is a common practice when a dataset should have a default value. The following code block replaces null values with -1 using built-in methods:
Remove values
Built-in methods can also be used to filter out null and NaN values. There is no built-in method specifically to remove infinity values.
User-defined functions
When passing null, NaN, or infinity values from tables into Python functions, the deephaven.constants module is useful. You can check input values as follows: