Skip to content

Numpy bool msgpack bugfix #18395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ I/O
- :func:`read_parquet` now allows to specify the columns to read from a parquet file (:issue:`18154`)
- :func:`read_parquet` now allows to specify kwargs which are passed to the respective engine (:issue:`18216`)
- Bug in parsing integer datetime-like columns with specified format in ``read_sql`` (:issue:`17855`).
- Bug in :meth:`DataFrame.to_msgpack` when serializing data of the numpy.bool_ datatype (:issue:`18390`)


Plotting
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/msgpack/_packer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ from libc.limits cimport *

from pandas.io.msgpack.exceptions import PackValueError
from pandas.io.msgpack import ExtType
import numpy as np


cdef extern from "../../src/msgpack/pack.h":
Expand Down Expand Up @@ -133,7 +134,7 @@ cdef class Packer(object):
while True:
if o is None:
ret = msgpack_pack_nil(&self.pk)
elif isinstance(o, bool):
elif isinstance(o, (bool, np.bool_)):
if o:
ret = msgpack_pack_true(&self.pk)
else:
Expand Down
13 changes: 12 additions & 1 deletion pandas/tests/io/test_packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ def test_scalar_float(self):
x_rec = self.encode_decode(x)
tm.assert_almost_equal(x, x_rec)

def test_scalar_bool(self):
x = np.bool_(1)
x_rec = self.encode_decode(x)
tm.assert_almost_equal(x, x_rec)

x = np.bool_(0)
x_rec = self.encode_decode(x)
tm.assert_almost_equal(x, x_rec)

def test_scalar_complex(self):
x = np.random.rand() + 1j * np.random.rand()
x_rec = self.encode_decode(x)
Expand Down Expand Up @@ -263,7 +272,7 @@ def test_numpy_array_complex(self):
x.dtype == x_rec.dtype)

def test_list_mixed(self):
x = [1.0, np.float32(3.5), np.complex128(4.25), u('foo')]
x = [1.0, np.float32(3.5), np.complex128(4.25), u('foo'), np.bool_(1)]
x_rec = self.encode_decode(x)
# current msgpack cannot distinguish list/tuple
tm.assert_almost_equal(tuple(x), x_rec)
Expand Down Expand Up @@ -401,6 +410,7 @@ def setup_method(self, method):
'G': [Timestamp('20130102', tz='US/Eastern')] * 5,
'H': Categorical([1, 2, 3, 4, 5]),
'I': Categorical([1, 2, 3, 4, 5], ordered=True),
'J': (np.bool_(1), 2, 3, 4, 5),
}

self.d['float'] = Series(data['A'])
Expand All @@ -410,6 +420,7 @@ def setup_method(self, method):
self.d['dt_tz'] = Series(data['G'])
self.d['cat_ordered'] = Series(data['H'])
self.d['cat_unordered'] = Series(data['I'])
self.d['numpy_bool_mixed'] = Series(data['J'])

def test_basic(self):

Expand Down