Skip to main content

Mapping values in NumPy arrays (any shape!) with high speed - Cython -

pip install nparraymapper

Tested against Python 3.11 / Windows 10

Cython (and a C/C++ compiler) must be installed to use the optimized Cython implementation.

This module provides functions for mapping values in NumPy arrays based on a specified mapping dictionary. It works on any shape and with almost all dtypes (OBJECT NOT!!!)

It always returns a copy! The original data doesn't change!

import numpy as np

from nparraymapper import map_numpy_array, map_array_with_strings

np.random.seed(0)
a = np.random.randint(1, 9, (10000, 10000, 4))
ds = map_numpy_array(a, mapdict={4: 400000, 3: 1021, 2: -1}, keepnotmapped=True)
print(a)
print(ds)
print('----------------------------')
a = np.random.randint(2, 9, (100, 100, 3), dtype=np.uint32)
ds = map_numpy_array(a, mapdict={4: 4000.1232, 3: 1021.32}, keepnotmapped=True)
print(a)
print(ds)
print('----------------------------')
a = np.random.randint(2, 9, (1000, 100, 8)).astype(np.float32)

ds = map_numpy_array(a, mapdict={4: 40, 3: 1021}, keepnotmapped=False)

print(a)
print(ds)
print('----------------------------')

subsdi = {110: 'babça', 14: 'bsdfvd', 9: 'çbba'}
src = np.random.randint(1, 12, (1000, 1000, 3))
print(src)
out = map_array_with_strings(src, mapdict=subsdi)
print(out)

# [[[5 8 6 1]
#   [4 4 4 8]
#   [2 4 6 3]
#   ...
#   [6 4 4 4]
#   [2 5 8 2]
#   [3 7 5 5]]
#  [[6 1 4 1]
#   [1 2 5 8]
#   [1 7 6 5]
#   ...
#   [6 7 7 1]
#   [5 5 4 7]
#   [5 8 7 6]]
#  [[1 2 8 5]
#   [2 2 8 6]
#   [1 3 4 4]
#   ...
#   [3 4 3 2]
#   [4 6 8 5]
#   [4 7 6 4]]
#  ...
#  [[2 7 5 5]
#   [2 2 8 8]
#   [8 8 7 8]
#   ...
#   [8 7 5 8]
#   [4 6 5 8]
#   [3 8 5 8]]
#  [[6 4 1 5]
#   [1 7 7 1]
#   [2 4 5 3]
#   ...
#   [1 5 5 4]
#   [1 8 6 7]
#   [4 5 8 4]]
#  [[4 2 3 5]
#   [5 7 8 2]
#   [1 3 4 6]
#   ...
#   [3 8 1 5]
#   [7 8 4 3]
#   [6 8 3 4]]]
# [[[     5      8      6      1]
#   [400000 400000 400000      8]
#   [    -1 400000      6   1021]
#   ...
#   [     6 400000 400000 400000]
#   [    -1      5      8     -1]
#   [  1021      7      5      5]]
#  [[     6      1 400000      1]
#   [     1     -1      5      8]
#   [     1      7      6      5]
#   ...
#   [     6      7      7      1]
#   [     5      5 400000      7]
#   [     5      8      7      6]]
#  [[     1     -1      8      5]
#   [    -1     -1      8      6]
#   [     1   1021 400000 400000]
#   ...
#   [  1021 400000   1021     -1]
#   [400000      6      8      5]
#   [400000      7      6 400000]]
#  ...
#  [[    -1      7      5      5]
#   [    -1     -1      8      8]
#   [     8      8      7      8]
#   ...
#   [     8      7      5      8]
#   [400000      6      5      8]
#   [  1021      8      5      8]]
#  [[     6 400000      1      5]
#   [     1      7      7      1]
#   [    -1 400000      5   1021]
#   ...
#   [     1      5      5 400000]
#   [     1      8      6      7]
#   [400000      5      8 400000]]
#  [[400000     -1   1021      5]
#   [     5      7      8     -1]
#   [     1   1021 400000      6]
#   ...
#   [  1021      8      1      5]
#   [     7      8 400000   1021]
#   [     6      8   1021 400000]]]
# ----------------------------
# [[[2 8 2]
#   [3 4 3]
#   [2 7 7]
#   ...
#   [4 6 4]
#   [3 7 5]
#   [7 7 8]]
#  [[4 4 4]
#   [2 7 4]
#   [2 7 2]
#   ...
#   [2 7 8]
#   [4 6 8]
#   [8 8 7]]
#  [[6 5 3]
#   [5 5 5]
#   [8 6 3]
#   ...
#   [4 3 6]
#   [4 6 7]
#   [2 8 7]]
#  ...
#  [[3 3 4]
#   [8 4 4]
#   [5 5 2]
#   ...
#   [4 2 5]
#   [7 3 7]
#   [6 5 2]]
#  [[3 3 2]
#   [4 6 5]
#   [8 8 4]
#   ...
#   [7 7 5]
#   [6 6 2]
#   [7 5 4]]
#  [[5 8 2]
#   [8 5 4]
#   [6 7 6]
#   ...
#   [4 8 6]
#   [2 8 3]
#   [2 8 4]]]
# [[[2.0000000e+00 8.0000000e+00 2.0000000e+00]
#   [1.0213200e+03 4.0001232e+03 1.0213200e+03]
#   [2.0000000e+00 7.0000000e+00 7.0000000e+00]
#   ...
#   [4.0001232e+03 6.0000000e+00 4.0001232e+03]
#   [1.0213200e+03 7.0000000e+00 5.0000000e+00]
#   [7.0000000e+00 7.0000000e+00 8.0000000e+00]]
#  [[4.0001232e+03 4.0001232e+03 4.0001232e+03]
#   [2.0000000e+00 7.0000000e+00 4.0001232e+03]
#   [2.0000000e+00 7.0000000e+00 2.0000000e+00]
#   ...
#   [2.0000000e+00 7.0000000e+00 8.0000000e+00]
#   [4.0001232e+03 6.0000000e+00 8.0000000e+00]
#   [8.0000000e+00 8.0000000e+00 7.0000000e+00]]
#  [[6.0000000e+00 5.0000000e+00 1.0213200e+03]
#   [5.0000000e+00 5.0000000e+00 5.0000000e+00]
#   [8.0000000e+00 6.0000000e+00 1.0213200e+03]
#   ...
#   [4.0001232e+03 1.0213200e+03 6.0000000e+00]
#   [4.0001232e+03 6.0000000e+00 7.0000000e+00]
#   [2.0000000e+00 8.0000000e+00 7.0000000e+00]]
#  ...
#  [[1.0213200e+03 1.0213200e+03 4.0001232e+03]
#   [8.0000000e+00 4.0001232e+03 4.0001232e+03]
#   [5.0000000e+00 5.0000000e+00 2.0000000e+00]
#   ...
#   [4.0001232e+03 2.0000000e+00 5.0000000e+00]
#   [7.0000000e+00 1.0213200e+03 7.0000000e+00]
#   [6.0000000e+00 5.0000000e+00 2.0000000e+00]]
#  [[1.0213200e+03 1.0213200e+03 2.0000000e+00]
#   [4.0001232e+03 6.0000000e+00 5.0000000e+00]
#   [8.0000000e+00 8.0000000e+00 4.0001232e+03]
#   ...
#   [7.0000000e+00 7.0000000e+00 5.0000000e+00]
#   [6.0000000e+00 6.0000000e+00 2.0000000e+00]
#   [7.0000000e+00 5.0000000e+00 4.0001232e+03]]
#  [[5.0000000e+00 8.0000000e+00 2.0000000e+00]
#   [8.0000000e+00 5.0000000e+00 4.0001232e+03]
#   [6.0000000e+00 7.0000000e+00 6.0000000e+00]
#   ...
#   [4.0001232e+03 8.0000000e+00 6.0000000e+00]
#   [2.0000000e+00 8.0000000e+00 1.0213200e+03]
#   [2.0000000e+00 8.0000000e+00 4.0001232e+03]]]
# ----------------------------
# [[[2. 2. 4. ... 7. 5. 4.]
#   [7. 3. 4. ... 4. 7. 3.]
#   [5. 2. 3. ... 8. 6. 6.]
#   ...
#   [4. 2. 2. ... 5. 5. 4.]
#   [4. 5. 8. ... 4. 2. 2.]
#   [5. 2. 6. ... 6. 2. 8.]]
#  [[7. 5. 7. ... 2. 4. 8.]
#   [6. 4. 6. ... 8. 5. 4.]
#   [4. 8. 7. ... 6. 6. 8.]
#   ...
#   [5. 7. 4. ... 5. 8. 7.]
#   [3. 2. 8. ... 7. 6. 4.]
#   [5. 6. 2. ... 3. 4. 4.]]
#  [[2. 2. 3. ... 3. 2. 3.]
#   [7. 2. 2. ... 6. 7. 8.]
#   [8. 5. 7. ... 3. 6. 3.]
#   ...
#   [3. 8. 5. ... 6. 8. 5.]
#   [6. 8. 2. ... 2. 3. 4.]
#   [2. 7. 4. ... 2. 5. 2.]]
#  ...
#  [[4. 6. 3. ... 7. 6. 2.]
#   [4. 4. 2. ... 5. 8. 4.]
#   [6. 7. 8. ... 4. 2. 6.]
#   ...
#   [7. 3. 6. ... 2. 7. 4.]
#   [2. 6. 7. ... 3. 5. 3.]
#   [5. 4. 8. ... 3. 4. 5.]]
#  [[8. 7. 8. ... 5. 8. 2.]
#   [7. 3. 2. ... 5. 4. 8.]
#   [4. 8. 8. ... 2. 2. 5.]
#   ...
#   [6. 3. 2. ... 4. 6. 7.]
#   [7. 7. 6. ... 2. 7. 3.]
#   [8. 4. 3. ... 3. 6. 8.]]
#  [[6. 3. 4. ... 2. 7. 7.]
#   [2. 3. 6. ... 3. 5. 6.]
#   [7. 6. 2. ... 7. 6. 8.]
#   ...
#   [4. 3. 2. ... 3. 4. 3.]
#   [6. 2. 5. ... 2. 5. 5.]
#   [7. 8. 4. ... 7. 7. 8.]]]
# [[[   0    0   40 ...    0    0   40]
#   [   0 1021   40 ...   40    0 1021]
#   [   0    0 1021 ...    0    0    0]
#   ...
#   [  40    0    0 ...    0    0   40]
#   [  40    0    0 ...   40    0    0]
#   [   0    0    0 ...    0    0    0]]
#  [[   0    0    0 ...    0   40    0]
#   [   0   40    0 ...    0    0   40]
#   [  40    0    0 ...    0    0    0]
#   ...
#   [   0    0   40 ...    0    0    0]
#   [1021    0    0 ...    0    0   40]
#   [   0    0    0 ... 1021   40   40]]
#  [[   0    0 1021 ... 1021    0 1021]
#   [   0    0    0 ...    0    0    0]
#   [   0    0    0 ... 1021    0 1021]
#   ...
#   [1021    0    0 ...    0    0    0]
#   [   0    0    0 ...    0 1021   40]
#   [   0    0   40 ...    0    0    0]]
#  ...
#  [[  40    0 1021 ...    0    0    0]
#   [  40   40    0 ...    0    0   40]
#   [   0    0    0 ...   40    0    0]
#   ...
#   [   0 1021    0 ...    0    0   40]
#   [   0    0    0 ... 1021    0 1021]
#   [   0   40    0 ... 1021   40    0]]
#  [[   0    0    0 ...    0    0    0]
#   [   0 1021    0 ...    0   40    0]
#   [  40    0    0 ...    0    0    0]
#   ...
#   [   0 1021    0 ...   40    0    0]
#   [   0    0    0 ...    0    0 1021]
#   [   0   40 1021 ... 1021    0    0]]
#  [[   0 1021   40 ...    0    0    0]
#   [   0 1021    0 ... 1021    0    0]
#   [   0    0    0 ...    0    0    0]
#   ...
#   [  40 1021    0 ... 1021   40 1021]
#   [   0    0    0 ...    0    0    0]
#   [   0    0   40 ...    0    0    0]]]
# ----------------------------
# [[[ 3  4 11]
#   [ 2  9  6]
#   [ 5  5  5]
#   ...
#   [11  9  3]
#   [ 9  3 10]
#   [ 5  5  4]]
#  [[ 5  6  8]
#   [ 7  8  9]
#   [ 9  8  9]
#   ...
#   [ 5  6 11]
#   [ 7  5  9]
#   [ 1 11  3]]
#  [[ 3  3 10]
#   [ 4  7  2]
#   [ 5  1  9]
#   ...
#   [11  8  5]
#   [ 6 11 11]
#   [ 2 10  1]]
#  ...
#  [[ 1  4  6]
#   [ 8  7  6]
#   [10  3  2]
#   ...
#   [10  8  3]
#   [ 7  7 11]
#   [ 7  7  7]]
#  [[ 3  8  6]
#   [ 5  5  6]
#   [ 7  7  7]
#   ...
#   [ 1  9  9]
#   [ 9  3  9]
#   [10  9  3]]
#  [[ 2  9 11]
#   [ 7  7  9]
#   [10 11  3]
#   ...
#   [ 6  7 11]
#   [11  3  4]
#   [ 5  3  2]]]
# [[['' '' '']
#   ['' 'çbba' '']
#   ['' '' '']
#   ...
#   ['' 'çbba' '']
#   ['çbba' '' '']
#   ['' '' '']]
#  [['' '' '']
#   ['' '' 'çbba']
#   ['çbba' '' 'çbba']
#   ...
#   ['' '' '']
#   ['' '' 'çbba']
#   ['' '' '']]
#  [['' '' '']
#   ['' '' '']
#   ['' '' 'çbba']
#   ...
#   ['' '' '']
#   ['' '' '']
#   ['' '' '']]
#  ...
#  [['' '' '']
#   ['' '' '']
#   ['' '' '']
#   ...
#   ['' '' '']
#   ['' '' '']
#   ['' '' '']]
#  [['' '' '']
#   ['' '' '']
#   ['' '' '']
#   ...
#   ['' 'çbba' 'çbba']
#   ['çbba' '' 'çbba']
#   ['' 'çbba' '']]
#  [['' 'çbba' '']
#   ['' '' 'çbba']
#   ['' '' '']
#   ...
#   ['' '' '']
#   ['' '' '']
#   ['' '' '']]]

Metadata

Release files for nparraymapper 0.10

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

Source distribution (sdist)

Source distribution for nparraymapper 0.10
File Size Uploaded Provenance Actions
nparraymapper-0.10.tar.gz 25.4 kB Without provenance Details for nparraymapper-0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nparraymapper 0.10
File Interpreter ABI Platform Actions
nparraymapper-0.10-py3-none-any.whl Python 3 none any Details for nparraymapper-0.10-py3-none-any.whl

Total release size: 50.4 kB

Release files / nparraymapper-0.10.tar.gz

Download URL nparraymapper-0.10.tar.gz
Size 25.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d5fe77e6d0ea25c58862ca3e21af84f001a6faaef529a6cf00ff26771f44456d
BLAKE2b-256 checksum
How to use checksums
1bb803bea0c55c22dc211520b2cabf8462639500fb15ada9722a65c0109d6c12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / nparraymapper-0.10-py3-none-any.whl

Download URL nparraymapper-0.10-py3-none-any.whl
Size 25.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea0cf0d00195c5bc0d6220dcc6545194ad4c81e2da32edc7bb93017cbc049ebe
BLAKE2b-256 checksum
How to use checksums
c69ec033fdeafd78afe6e3061416dd21c615f8fc6482fc80a4e65052a7c1fc68
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Security for nparraymapper 0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10 This release

2 release files No provenance

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