Skip to content

Commit 6c074d1

Browse files
PhyNerdjreback
authored andcommitted
Numpy bool msgpack bugfix (#18395)
1 parent c634352 commit 6c074d1

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v0.21.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ I/O
8787
- :func:`read_parquet` now allows to specify the columns to read from a parquet file (:issue:`18154`)
8888
- :func:`read_parquet` now allows to specify kwargs which are passed to the respective engine (:issue:`18216`)
8989
- Bug in parsing integer datetime-like columns with specified format in ``read_sql`` (:issue:`17855`).
90+
- Bug in :meth:`DataFrame.to_msgpack` when serializing data of the numpy.bool_ datatype (:issue:`18390`)
9091

9192

9293
Plotting

pandas/io/msgpack/_packer.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from libc.limits cimport *
88

99
from pandas.io.msgpack.exceptions import PackValueError
1010
from pandas.io.msgpack import ExtType
11+
import numpy as np
1112

1213

1314
cdef extern from "../../src/msgpack/pack.h":
@@ -133,7 +134,7 @@ cdef class Packer(object):
133134
while True:
134135
if o is None:
135136
ret = msgpack_pack_nil(&self.pk)
136-
elif isinstance(o, bool):
137+
elif isinstance(o, (bool, np.bool_)):
137138
if o:
138139
ret = msgpack_pack_true(&self.pk)
139140
else:

pandas/tests/io/test_packers.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ def test_scalar_float(self):
180180
x_rec = self.encode_decode(x)
181181
tm.assert_almost_equal(x, x_rec)
182182

183+
def test_scalar_bool(self):
184+
x = np.bool_(1)
185+
x_rec = self.encode_decode(x)
186+
tm.assert_almost_equal(x, x_rec)
187+
188+
x = np.bool_(0)
189+
x_rec = self.encode_decode(x)
190+
tm.assert_almost_equal(x, x_rec)
191+
183192
def test_scalar_complex(self):
184193
x = np.random.rand() + 1j * np.random.rand()
185194
x_rec = self.encode_decode(x)
@@ -263,7 +272,7 @@ def test_numpy_array_complex(self):
263272
x.dtype == x_rec.dtype)
264273

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

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

414425
def test_basic(self):
415426

0 commit comments

Comments
 (0)