Do we need to check that py_{min,max}_val aren't NULL before using them? I would be surprised that it there is an error, but it could happen that we can't allocated the new output array.

Otherwise, I think this PR is ready to merge.