valdezt / sigfig

A python module aimed to aid the user with visual display of scientific numbers, i.e. significant figures, rounding, spacing, etc.
12 stars 4 forks source link

Add support for numpy dtypes #5

Open fookatchu opened 3 years ago

fookatchu commented 3 years ago

Currently (v1.1.8) round() does not support numpy dtypes:

In [9]: sigfig.round(np.float64(23.0))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\code\analysis\unc_validation.py in <module>
----> 1 sigfig.round(np.float64(23.0))

~\code\analysis\venv\lib\site-packages\sigfig\sigfig.py in round(*args, **kwargs)
    562         warn("no input number given, nothing to return")
    563         return None
--> 564     given = _arguments_parse(args, kwargs)
    565     num = given['num']
    566

~\code\analysis\venv\lib\site-packages\sigfig\sigfig.py in _arguments_parse(args, kwargs)
    256     given = {'output_type': type(args[0])}
    257     if type(args[0]) not in types:
--> 258         raise TypeError('Invalid input type of %s, expecting 1 of %s' % (type(args[0]), str(types)))
    259     given['num'] = _num_parse(args[0])
    260     if len(args) >= 2:

TypeError: Invalid input type of <class 'numpy.float64'>, expecting 1 of {<class 'float'>, <class 'decimal.Decimal'>, <class 'str'>, <class 'NoneType'>, <class 'sigfig.sigfig._Number'>, <class 'int'>}

Could this functionality be added? I am not in the position to write a pull request for this, but a simple fix woult be to try to convert an numpy object via it's .item() method, as described here