Commit c30ee6db authored by Kirill Smelkov's avatar Kirill Smelkov

kpi: Fix NA(dtype) to return object with the same dtype

We were not caring about that and so previously e.g. NA(np.int16) was
giving int instead of np.int16 .

Fix it.
parent e1a5ceea
# -*- coding: utf-8 -*-
# Copyright (C) 2022 Nexedi SA and Contributors.
# Copyright (C) 2022-2023 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com>
#
# This program is free software: you can Use, Study, Modify and Redistribute
......@@ -634,15 +634,19 @@ def _newscalar(typ, dtype):
# NA returns "Not Available" value for dtype.
def NA(dtype):
typ = dtype.type
# float
if issubclass(dtype.type, np.floating):
return np.nan
if issubclass(typ, np.floating):
na = np.nan
# int: NA is min value
if issubclass(dtype.type, np.signedinteger):
return np.iinfo(dtype.type).min
elif issubclass(typ, np.signedinteger):
na = np.iinfo(typ).min
else:
raise AssertionError("NA not defined for dtype %s" % (dtype,))
return typ(na) # return the same type as dtype has, e.g. np.int32, not int
# isNA returns whether value represent NA.
# value must be numpy scalar.
......
# -*- coding: utf-8 -*-
# Copyright (C) 2022 Nexedi SA and Contributors.
# Copyright (C) 2022-2023 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com>
#
# This program is free software: you can Use, Study, Modify and Redistribute
......@@ -422,7 +422,10 @@ def test_Calc_erab_accessibility():
def test_NA():
def _(typ):
return NA(typ(0).dtype)
na = NA(typ(0).dtype)
assert type(na) is typ
assert isNA(na)
return na
assert np.isnan( _(np.float16) )
assert np.isnan( _(np.float32) )
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment