# 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
```python
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
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/numpytypechecker",
"name": "numpytypechecker",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "numpytypechecker,np",
"author": "Johannes Fischer",
"author_email": "aulasparticularesdealemaosp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/82/1d/80be8aa50a17743c3b399f0bb3974056bca863712d1ac4bb95911ad201a5/numpytypechecker-0.11.tar.gz",
"platform": null,
"description": "\r\n# Check and convert the data type of a NumPy array based on a predefined set of data types.\r\n\r\n\r\n## pip install numpytypechecker\r\n\r\n### Tested against Windows / Python 3.11 / Anaconda\r\n\r\n\r\n\r\n```python\r\ndtypecheck(array, filterna=True, float2int=True, dtypes=(np.uint8,\r\n np.int8,\r\n np.uint16,\r\n np.int16,\r\n np.uint32,\r\n np.int32,\r\n np.uint64,\r\n np.int64,\r\n np.uintp,\r\n np.intp,\r\n np.float16,\r\n np.float32,\r\n np.float64,\r\n 'M',\r\n 'm',\r\n 'O',\r\n 'P',\r\n 'S',\r\n 'U',\r\n 'V',\r\n 'p',\r\n 's',\r\n np.complex64,\r\n np.complex128,\r\n np.datetime64,\r\n\r\n np.timedelta64,\r\n np.void, bool, np.bool_,\r\n object\r\n )):\r\n r\"\"\"\r\n Check and convert the data type of a NumPy array based on a predefined set of data types.\r\n\r\n Parameters:\r\n - array (numpy.ndarray): Input NumPy array.\r\n - filterna (bool, optional): If True, remove NaN values from the array before type checking.\r\n Default is True.\r\n - float2int (bool, optional): If True, convert float arrays to integer if they contain only integers.\r\n Default is True.\r\n - dtypes (tuple, optional): Tuple of NumPy data types to check against. Default includes various numeric,\r\n datetime, timedelta, complex, boolean, and object types.\r\n\r\n Returns:\r\n - numpy.ndarray: NumPy array with the converted data type.\r\n\r\n Examples:\r\n\r\n from numpytypechecker import dtypecheck\r\n import numpy as np\r\n # Example\r\n a1D = np.array([1, 2, 3, 4])\r\n a2D = np.array([[1, 2], [3, 4]])\r\n a3D = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])\r\n b1 = np.array([2., 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9])\r\n b2 = np.array([[1., 1., 1., 0.],\r\n [1., 1., 0., 1.],\r\n [0., 0., -3., 0.],\r\n [0., 0., np.nan, -4.]])\r\n\r\n print(dtypecheck(a1D, filterna=True, float2int=True, ).dtype)\r\n print(dtypecheck(a2D, filterna=True, float2int=True, ).dtype)\r\n print(dtypecheck(a3D, filterna=True, float2int=True, ).dtype)\r\n print(dtypecheck(b1, filterna=True, float2int=True, ).dtype)\r\n print(dtypecheck(b2, filterna=False, float2int=True, ).dtype)\r\n\r\n # uint8\r\n # uint8\r\n # uint8\r\n # float64\r\n # float64\r\n\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Check and convert the data type of a NumPy array based on a predefined set of data types.",
"version": "0.11",
"project_urls": {
"Homepage": "https://github.com/hansalemaos/numpytypechecker"
},
"split_keywords": [
"numpytypechecker",
"np"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "80f3b5fd595b04243ae3829c3ee58132242eb84bd3bdad88e29f9fa9c0ea6d02",
"md5": "bfcf14fa67bc47a12f02cefb662956bb",
"sha256": "17be6850188885670278e2630d10d7957bfe9133b035504d319786602a472cd6"
},
"downloads": -1,
"filename": "numpytypechecker-0.11-py3-none-any.whl",
"has_sig": false,
"md5_digest": "bfcf14fa67bc47a12f02cefb662956bb",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 21401,
"upload_time": "2023-12-03T08:09:28",
"upload_time_iso_8601": "2023-12-03T08:09:28.358851Z",
"url": "https://files.pythonhosted.org/packages/80/f3/b5fd595b04243ae3829c3ee58132242eb84bd3bdad88e29f9fa9c0ea6d02/numpytypechecker-0.11-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "821d80be8aa50a17743c3b399f0bb3974056bca863712d1ac4bb95911ad201a5",
"md5": "8bb113692e9b5bccff885a8c9c34d2c7",
"sha256": "721a6feb6cfa6885e669bb314d35bac4ae179edc299cd7e901ffb2579f05cfec"
},
"downloads": -1,
"filename": "numpytypechecker-0.11.tar.gz",
"has_sig": false,
"md5_digest": "8bb113692e9b5bccff885a8c9c34d2c7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 20918,
"upload_time": "2023-12-03T08:09:30",
"upload_time_iso_8601": "2023-12-03T08:09:30.429582Z",
"url": "https://files.pythonhosted.org/packages/82/1d/80be8aa50a17743c3b399f0bb3974056bca863712d1ac4bb95911ad201a5/numpytypechecker-0.11.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-03 08:09:30",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hansalemaos",
"github_project": "numpytypechecker",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "numpytypechecker"
}