ppc64le/linux/: cykhash-2.0.0 metadata and description

Homepage Simple index

cython wrapper for khash-sets/maps, efficient implementation of isin and unique

author Egor Dranischnikow
classifiers
  • Programming Language :: Python :: 3
  • Environment :: MetaData :: IBM Python Ecosystem
description_content_type text/markdown
dynamic
  • author
  • classifier
  • description
  • description-content-type
  • home-page
  • license
  • license-file
  • summary
license MIT
license_file
  • LICENSE
File Tox results History
cykhash-2.0.0-cp310-cp310-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl
Size
4 MB
Type
Python Wheel
Python
3.10
  • Replaced 1 time(s)
  • Uploaded to ppc64le/linux by ppc64le 2026-08-20 10:34:18
cykhash-2.0.0-cp311-cp311-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl
Size
4 MB
Type
Python Wheel
Python
3.11
  • Replaced 1 time(s)
  • Uploaded to ppc64le/linux by ppc64le 2026-08-20 10:34:19
cykhash-2.0.0-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl
Size
4 MB
Type
Python Wheel
Python
3.12
  • Replaced 1 time(s)
  • Uploaded to ppc64le/linux by ppc64le 2026-08-20 10:34:19
cykhash-2.0.0-cp313-cp313-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl
Size
4 MB
Type
Python Wheel
Python
3.13
  • Replaced 1 time(s)
  • Uploaded to ppc64le/linux by ppc64le 2026-08-20 10:34:21
cykhash-2.0.0-cp314-cp314-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl
Size
4 MB
Type
Python Wheel
Python
3.14
  • Replaced 1 time(s)
  • Uploaded to ppc64le/linux by ppc64le 2026-08-20 10:34:22

cykhash

cython wrapper for khash-sets/maps, efficient implementation of isin and unique

About:

Installation:

The recommended way to install the library is via conda package manager using the conda-forge channel:

conda install -c conda-forge cykhash

You can also install the library using pip. To install the latest release:

pip install cykhash

To install the most recent version of the module:

pip install https://github.com/realead/cykhash/zipball/master

Dependencies:

To build the library from source, Cython>=0.28 is required as well as a c-build tool chain.

See (https://github.com/realead/cykhash/blob/master/doc/README4DEVELOPER.md) for dependencies needed for development.

Quick start

Hash set and isin

Creating a hashset and using it in isin:

# prepare data:
>>> import numpy as np 
>>> a = np.arange(42, dtype=np.int64)
>>> b = np.arange(84, dtype=np.int64)
>>> result = np.empty(b.size, dtype=np.bool)

# actually usage
>>> from cykhash import Int64Set_from_buffer, isin_int64

>>> lookup = Int64Set_from_buffer(a) # create a hashset
>>> isin_int64(b, lookup, result)    # running time O(b.size)
>>> isin_int64(b, lookup, result)    # lookup is reused and not recreated

unique

Finding unique in O(n) (compared to numpy's np.unique - O(n*logn)) and smaller memory-footprint than pandas' pd.unique:

# prepare input
>>> import numpy as np
>>> a = np.array([1,2,3,3,2,1], dtype=np.int64)

# actual usage:
>>> from cykhash import unique_int64
>>> unique_buffer = unique_int64(a) # unique element are exposed via buffer-protocol

# can be converted to a numpy-array without copying via
>>> unique_array = np.ctypeslib.as_array(unique_buffer)
>>> unique_array.shape
(3,)

Hash map

Maps and sets handle nan-correctly (try it out with Python's dict/set):

>>> from cykhash import Float64toInt64Map
>>> my_map = Float64toInt64Map() # values are 64bit integers
>>> my_map[float("nan")] = 1
>>> my_map[float("nan")]
1

Functionality overview

Hash sets

Int64Set, Int32Set, Float64Set, Float32Set ( and PyObjectSet) are implemented. They are more or less drop-in replacements for Python's set. Furthermore, given the Cython-interface, efficient extensions of functionality are easily done.

The biggest advantage of these sets is that they need about 4-8 times less memory than the usual Python-sets and are somewhat faster for integers or floats.

As PyObjectSet is somewhat slower than the usual set and needs about the same amount of memory, it should be used only if all nans should be treated as equivalent.

The most efficient way to create such sets is to use XXXXSet_from_buffer(...), e.g. Int64Set_from_buffer, if the data container at hand supports buffer protocol (e.g. numpy-arrays, array.array or ctypes-arrays). Or XXXXSet_from(...) for any iterator.

Hash maps

Int64toInt64Map, Int32toInt32Map, Float64toInt64Map, Float32toInt32Map ( and PyObjectMap) are implemented. They are more or less drop-in replacements for Python's dict (however, not every piece of dict's functionality makes sense, for example setdefault(x, default) without default-argument, because None cannot be inserted, also the khash-maps don't preserve the insertion order, so there is also no reversed). Furthermore, given the Cython-interface, efficient extensions of functionality are easily done.

Biggest advantage of these sets is that they need about 4-8 times less memory than the usual Python-dictionaries and are somewhat faster for integers or floats.

As PyObjectMap is somewhat slower than the usual dict and needs about the same amount of memory, it should be used only if all nans should be treated as equivalent.

isin

all, none, any, and count_if

unique

As pandas uses maps instead of sets internally for unique, it needs about 4 times more peak memory and is 1.6-3 times slower.

Floating-point numbers as keys

There is a problem with floating-point sets or maps, i.e. Float64Set, Float32Set, Float64toInt64Map and Float32toInt32Map: The standard definition of "equal" and hash-function based on the bit representation don't define a meaningful or desired behavior for the hash set:

This problem is resolved through following special case handling:

A consequence of the above rule, that the equivalence classes of {0.0, -0.0} and e{x | x is not a number} have more than one element. In the set these classes are represented by the first seen element from the class.

The above holds also for PyObjectSet (this behavior is not the same as fro Python-set which shows a different behavior for nans).

Examples:

Hash sets

Python: Creates a set from a numpy-array and looks up whether an element is in the resulting set:

>>> import numpy as np
>>> from cykhash import Int64Set_from_buffer
>>> a =  np.arange(42, dtype=np.int64)
>>> my_set = Int64Set_from_buffer(a) # no reallocation will be needed
>>> 41 in my_set 
True
>>> 42 not in my_set
True

Python: Create a set from an iterable and looks up whether an element is in the resulting set:

>>> from cykhash import Int64Set_from
>>> my_set = Int64Set_from(range(42)) # no reallocation will be needed
>>> assert 41 in my_set and 42 not in my_set

Cython: Create a set and put some values into it:

from cykhash.khashsets cimport Int64Set
my_set = Int64Set(number_of_elements_hint=12)  # reserve place for at least 12 integers
cdef Py_ssize_t i
for i in range(12):
   my_set.add(i)
assert 11 in my_set and 12 not in my_set

Hash maps

Python: Creating int64->float64 map using Int64toFloat64Map_from_buffers:

>>> import numpy as np
>>> from cykhash import Int64toFloat64Map_from_buffers
>>> keys = np.array([1, 2, 3, 4], dtype=np.int64)
>>> vals = np.array([5, 6, 7, 8], dtype=np.float64)
>>> my_map = Int64toFloat64Map_from_buffers(keys, vals) # there will be no reallocation
>>> assert my_map[4] == 8.0

Python: Creating int64->int64 map from scratch:

>>> import numpy as np
>>> from cykhash import Int64toInt64Map

# my_map will not need reallocation for at least 12 elements
>>> my_map = Int64toInt64Map(number_of_elements_hint=12)
>>> for i in range(12):  my_map[i] = i+1
>>> assert my_map[5] == 6

isin

Python: Creating look-up data structure from a numpy-array, performing isin-query

>>> import numpy as np
>>> from cykhash import Int64Set_from_buffer, isin_int64
>>> a = np.arange(42, dtype=np.int64)
>>> lookup = Int64Set_from_buffer(a)

>>> b = np.arange(84, dtype=np.int64)
>>> result = np.empty(b.size, dtype=np.bool)

>>> isin_int64(b, lookup, result)    # running time O(b.size)
>>> assert np.sum(result.astype(np.int)) == 42

unique

Python: using unique_int64:

>>> import numpy as np
>>> from cykhash import unique_int64
>>> a = np.array([1,2,3,3,2,1], dtype=np.int64)
>>> u = np.ctypeslib.as_array(unique_int64(a)) # there will be no reallocation
>>> assert set(u) == {1,2,3}

Python: using unique_stable_int64:

>>> import numpy as np
>>> from cykhash import unique_stable_int64
>>> a = np.array([3,2,1,1,2,3], dtype=np.int64)
>>> u = np.ctypeslib.as_array(unique_stable_int64(a)) # there will be no reallocation
>>> assert list(u) == [3,2,1] 

API

See (https://github.com/realead/cykhash/blob/master/doc/README_API.md) for a more detailed API description.

Performance

See (https://github.com/realead/cykhash/blob/master/doc/README_PERFORMANCE.md) for results of performance tests.

Trivia

History:

Release 2.0.0 (09.11.2021):

Release 1.0.2 (30.05.2020):

Release 1.0.1 (27.05.2020):

Older:

Export Classification Notice

The software hosted on this website consists of publicly available open‑source packages. To the extent U.S. export regulations apply, software that is publicly available as described in 15 C.F.R. §§ 734.7 (for non-encryption software) or 742.15(b) (for encryption software) is not subject to the Export Administration Regulations (EAR). Users are responsible for complying with all applicable export laws and regulations.