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 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2022 Nexedi SA and Contributors. # Copyright (C) 2022-2023 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com> # Kirill Smelkov <kirr@nexedi.com>
# #
# This program is free software: you can Use, Study, Modify and Redistribute # This program is free software: you can Use, Study, Modify and Redistribute
...@@ -634,15 +634,19 @@ def _newscalar(typ, dtype): ...@@ -634,15 +634,19 @@ def _newscalar(typ, dtype):
# NA returns "Not Available" value for dtype. # NA returns "Not Available" value for dtype.
def NA(dtype): def NA(dtype):
typ = dtype.type
# float # float
if issubclass(dtype.type, np.floating): if issubclass(typ, np.floating):
return np.nan na = np.nan
# int: NA is min value # int: NA is min value
if issubclass(dtype.type, np.signedinteger): elif issubclass(typ, np.signedinteger):
return np.iinfo(dtype.type).min na = np.iinfo(typ).min
else:
raise AssertionError("NA not defined for dtype %s" % (dtype,)) 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. # isNA returns whether value represent NA.
# value must be numpy scalar. # value must be numpy scalar.
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2022 Nexedi SA and Contributors. # Copyright (C) 2022-2023 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com> # Kirill Smelkov <kirr@nexedi.com>
# #
# This program is free software: you can Use, Study, Modify and Redistribute # This program is free software: you can Use, Study, Modify and Redistribute
...@@ -422,7 +422,10 @@ def test_Calc_erab_accessibility(): ...@@ -422,7 +422,10 @@ def test_Calc_erab_accessibility():
def test_NA(): def test_NA():
def _(typ): 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.float16) )
assert np.isnan( _(np.float32) ) 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