Skip to content

Commit bf169ed

Browse files
committed
Merge pull request #4658 from hayd/rebase4236
Boolean indexing on an empty series loses index names (rebase of 4236)
2 parents 5cdad82 + a4624f1 commit bf169ed

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

doc/source/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
260260
- Fix bug in ``pd.read_clipboard`` on windows with PY3 (:issue:`4561`); not decoding properly
261261
- ``tslib.get_period_field()`` and ``tslib.get_period_field_arr()`` now raise
262262
if code argument out of range (:issue:`4519`, :issue:`4520`)
263+
- Fix boolean indexing on an empty series loses index names (:issue:`4235`),
264+
infer_dtype works with empty arrays.
263265
- Fix reindexing with multiple axes; if an axes match was not replacing the current axes, leading
264266
to a possible lazay frequency inference issue (:issue:`3317`)
265267
- Fixed issue where ``DataFrame.apply`` was reraising exceptions incorrectly

pandas/src/inference.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ def infer_dtype(object _values):
4141
_values = list(_values)
4242
values = list_to_object_array(_values)
4343

44-
n = len(values)
45-
if n == 0:
46-
return 'empty'
47-
4844
val_kind = values.dtype.type
4945
if val_kind in _TYPE_MAP:
5046
return _TYPE_MAP[val_kind]
5147

5248
if values.dtype != np.object_:
5349
values = values.astype('O')
5450

51+
n = len(values)
52+
if n == 0:
53+
return 'empty'
54+
5555
val = util.get_value_1d(values, 0)
5656

5757
if util.is_datetime64_object(val):

pandas/tests/test_series.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,13 @@ def test_getitem_boolean(self):
740740
assert_series_equal(result, expected)
741741
self.assert_(np.array_equal(result.index, s.index[mask]))
742742

743+
def test_getitem_boolean_empty(self):
744+
s = Series([], dtype=np.int64)
745+
s.index.name = 'index_name'
746+
s = s[s.isnull()]
747+
self.assertEqual(s.index.name, 'index_name')
748+
self.assertEqual(s.dtype, np.int64)
749+
743750
def test_getitem_generator(self):
744751
gen = (x > 0 for x in self.series)
745752
result = self.series[gen]

pandas/tests/test_tseries.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,9 @@ class TestTypeInference(unittest.TestCase):
565565

566566
def test_length_zero(self):
567567
result = lib.infer_dtype(np.array([], dtype='i4'))
568-
self.assertEqual(result, 'empty')
568+
self.assertEqual(result, 'integer')
569569

570-
result = lib.infer_dtype(np.array([], dtype='O'))
570+
result = lib.infer_dtype([])
571571
self.assertEqual(result, 'empty')
572572

573573
def test_integers(self):

0 commit comments

Comments
 (0)