Skip to main content

PyPI version Python package

is-numeric

Really simple. A method to test if a value is_numeric.

from is_numeric import is_numeric
print( is_numeric(1) )           # True
print( is_numeric(-1) )          # True
print( is_numeric(123) )         # True
print( is_numeric(123.456) )     # True
print( is_numeric("123.456") )   # True
print( is_numeric("x") )         # False
print( is_numeric("1x") )        # False

Installation

pip install is-numeric

Notes on the algorithm

I tested 4 algorithms:

  • str.isinstance
  • error-driven
  • regex
  • regex (plus str.isinstance)

str.isinstance

Using the built in .isinstance is limited in functionality, as it only looks for all characters being a digit, and fails on '-1' or '1.2'. Thus discard this approach.

def _str_isnumeric(value):
    if isinstance(value, (int, float)):
        return True
    if isinstance(value, str) and value.isnumeric():
        return True
    return False

error-driven

This is ugly in that it relies on errors in flow control. I hate it. It is like nails on chalk board to me.

def _is_numeric_using_error(value):
    try:
        float(value)
        return True
    except ValueError:
        return False

regex

This seems elegant. Check for a pattern.

def _is_numeric_regex(value):
    if isinstance(value, (int, float)):
        return True
    if isinstance(value, str):
        return bool(re.match(r"^-?\d+(\.\d+)?$", value))
    return False

regex (plus str.isinstance)

The regex solution, with a small change to check if all numeric digits prior to doing regex.

def _is_numeric_regex_plus_isnumeric(value):
    if isinstance(value, (int, float)):
        return True
    if isinstance(value, str) and value.isnumeric():
        return True
    if isinstance(value, str):
        return bool(re.match(r"^-?\d+(\.\d+)?$", value))
    return False

Performance Comparison

I ran a very non-scientific test.

Approach
Create a list of inputs, with a mix of float/int, strings that represent numeric values, and non-numeric values. Iterate x time over the list for a given algorithm. Repeat for each algorithm.

Variable

  • % of inputs that are non-numeric (theory is that error-driven will perform better when the value is numeric, and less efficient when the value cannot be cast to float and an error is raised)

Findings
When most values are numeric 1 (0-50%), the error-driven approach outperforms other approaches (up to 3x).
When most values are non-numeric 2 (80%), the regex (plus str.isinstance) approach has a very slight advantage (4%).

Conclusion
without knowning the % of non-numeric values, it is recommended to use the error-driven approach. As such, the error-drive approach is exposed through the is_numeric function in this package.

Raw Data

const: iterations=1000000

Algorithm % Non-Numeric Time in Seconds Iterations per Second
is_numeric_regex_plus_isnumeric 0.0 4.176071 239,459.52
is_numeric_using_error 0.0 1.469144 680,668.39
is_numeric_regex 0.0 4.719131 211,903.44
is_numeric_regex_plus_isnumeric 0.2 5.893955 169,665.37
is_numeric_using_error 0.2 3.295613 303,433.65
is_numeric_regex 0.2 6.379531 156,751.32
is_numeric_regex_plus_isnumeric 0.5 10.687538 93,566.92
is_numeric_using_error 0.5 8.338870 119,920.32
is_numeric_regex 0.5 10.848000 92,182.89
is_numeric_regex_plus_isnumeric 0.8 29.750488 33,612.89
is_numeric_using_error 0.8 28.508906 35,076.76
is_numeric_regex 0.8 28.439334 35,162.57
  1. numeric = float/int, strings that represent numeric values ↩

  2. non-numeric = strings that cannot be cast to float/int ↩

Metadata

Release files for is-numeric 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for is-numeric 1.0.1
File Size Uploaded
is-numeric-1.0.1.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for is-numeric 1.0.1
File Interpreter ABI Platform
is_numeric-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.8 kB

Release files / is-numeric-1.0.1.tar.gz

Download URL is-numeric-1.0.1.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
bbee639abcc946e49bd57ac10c180f38915007ee7c72942b7dffc181c31fbdde
BLAKE2b-256 checksum
How to use checksums
e0c23805470d4056c8eb4f121ebf7906889b60b96b0c664fe6e739725af0037e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / is_numeric-1.0.1-py3-none-any.whl

Download URL is_numeric-1.0.1-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
75fada43516c536aa18004a1241df3a42b391ed5cddbc5c950b1ea6fca2fb815
BLAKE2b-256 checksum
How to use checksums
1553538986934103d039a19de13949e83118ad9cd904b9865c0ef707fddacabb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page