Check and convert the data type of a NumPy array based on a predefined set of data types.
Project description
Check and convert the data type of a NumPy array based on a predefined set of data types.
pip install numpytypechecker
Tested against Windows / Python 3.11 / Anaconda
dtypecheck(array, filterna=True, float2int=True, dtypes=(np.uint8,
np.int8,
np.uint16,
np.int16,
np.uint32,
np.int32,
np.uint64,
np.int64,
np.uintp,
np.intp,
np.float16,
np.float32,
np.float64,
'M',
'm',
'O',
'P',
'S',
'U',
'V',
'p',
's',
np.complex64,
np.complex128,
np.datetime64,
np.timedelta64,
np.void, bool, np.bool_,
object
)):
r"""
Check and convert the data type of a NumPy array based on a predefined set of data types.
Parameters:
- array (numpy.ndarray): Input NumPy array.
- filterna (bool, optional): If True, remove NaN values from the array before type checking.
Default is True.
- float2int (bool, optional): If True, convert float arrays to integer if they contain only integers.
Default is True.
- dtypes (tuple, optional): Tuple of NumPy data types to check against. Default includes various numeric,
datetime, timedelta, complex, boolean, and object types.
Returns:
- numpy.ndarray: NumPy array with the converted data type.
Examples:
from numpytypechecker import dtypecheck
import numpy as np
# Example
a1D = np.array([1, 2, 3, 4])
a2D = np.array([[1, 2], [3, 4]])
a3D = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
b1 = np.array([2., 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9])
b2 = np.array([[1., 1., 1., 0.],
[1., 1., 0., 1.],
[0., 0., -3., 0.],
[0., 0., np.nan, -4.]])
print(dtypecheck(a1D, filterna=True, float2int=True, ).dtype)
print(dtypecheck(a2D, filterna=True, float2int=True, ).dtype)
print(dtypecheck(a3D, filterna=True, float2int=True, ).dtype)
print(dtypecheck(b1, filterna=True, float2int=True, ).dtype)
print(dtypecheck(b2, filterna=False, float2int=True, ).dtype)
# uint8
# uint8
# uint8
# float64
# float64
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
numpytypechecker-0.11.tar.gz
(20.9 kB
view details)
Built Distribution
File details
Details for the file numpytypechecker-0.11.tar.gz
.
File metadata
- Download URL: numpytypechecker-0.11.tar.gz
- Upload date:
- Size: 20.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 721a6feb6cfa6885e669bb314d35bac4ae179edc299cd7e901ffb2579f05cfec |
|
MD5 | 8bb113692e9b5bccff885a8c9c34d2c7 |
|
BLAKE2b-256 | 821d80be8aa50a17743c3b399f0bb3974056bca863712d1ac4bb95911ad201a5 |
File details
Details for the file numpytypechecker-0.11-py3-none-any.whl
.
File metadata
- Download URL: numpytypechecker-0.11-py3-none-any.whl
- Upload date:
- Size: 21.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17be6850188885670278e2630d10d7957bfe9133b035504d319786602a472cd6 |
|
MD5 | bfcf14fa67bc47a12f02cefb662956bb |
|
BLAKE2b-256 | 80f3b5fd595b04243ae3829c3ee58132242eb84bd3bdad88e29f9fa9c0ea6d02 |