question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

numpy.packbits doesn't accept a bool array

See original GitHub issue

Example of problem

data = numpy.array([True])
byte_values = numpy.packbits(data)

This results with an exception – TypeError: Expected an input array of integer data type. This seems to go against the documentation of the function, which says it expects a ‘binary-valued array’.

To make this work, you need to first convert the data type of the array to int, which seems redundant. eg.

byte_values = numpy.packbits(numpy.array(data, dtype=int))

A better use case of where I encountered the problem.

data = numpy.random.sample(1024)
truth_values = data >= threshold
bits = numpy.packbits(truth_values)

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
alimuldalcommented, Dec 29, 2015

@LuisBL There is no need to cast the input array to the native int dtype - you can avoid generating a copy simply by viewing it as np.uint8, which also uses a single byte per element, e.g.:

px = np.packbits(x.view(np.uint8))
0reactions
jaimefriocommented, Dec 29, 2015

That feature was added in #5319, it does work in current master, so closing this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy boolean array with 1 bit entries - Stack Overflow
Is there a way in numpy to create an array of booleans that uses just 1 bit for each entry? The standard np....
Read more >
numpy.packbits — NumPy v1.24 Manual
Packs the elements of a binary-valued array into bits in a uint8 array. ... An array of integers or booleans whose elements should...
Read more >
The Best Technical Questions And Answers - Tutorialspoint
To pack the elements of a binary-valued array into bits in a uint8 array, use the numpy.packbits() method in Python Numpy. The result...
Read more >
jax.numpy package - JAX documentation - Read the Docs
Notably, since JAX arrays are immutable, NumPy APIs that mutate arrays in-place cannot be ... Returns a bool array, where True if input...
Read more >
One bit data type in NumPy? : r/Python - Reddit
Right now I'm using NumPy arrays but they only allow bits to be stored ... It does not specifically pack bits, but it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found