Backport 1.8 3866

Backport #3868: `BUG: Refactor nanfunctions to behave as agreed on for 1.8.`