Skip to content

BUG: Boolean indexing on an empty series loses index names #4236

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

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 4 additions & 4 deletions pandas/src/inference.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ def infer_dtype(object _values):
_values = list(_values)
values = list_to_object_array(_values)

n = len(values)
if n == 0:
return 'empty'

val_kind = values.dtype.type
if val_kind in _TYPE_MAP:
return _TYPE_MAP[val_kind]

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

n = len(values)
if n == 0:
return 'empty'

val = util.get_value_1d(values, 0)

if util.is_datetime64_object(val):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,13 @@ def test_getitem_boolean(self):
assert_series_equal(result, expected)
self.assert_(np.array_equal(result.index, s.index[mask]))

def test_getitem_boolean_empty(self):
s = Series([], dtype=np.int64)
s.index.name = 'index_name'
s = s[s.isnull()]
self.assertEqual(s.index.name, 'index_name')
self.assertEqual(s.dtype, np.int64)

def test_getitem_generator(self):
gen = (x > 0 for x in self.series)
result = self.series[gen]
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/test_tseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,9 @@ class TestTypeInference(unittest.TestCase):

def test_length_zero(self):
result = lib.infer_dtype(np.array([], dtype='i4'))
self.assertEqual(result, 'empty')
self.assertEqual(result, 'integer')

result = lib.infer_dtype(np.array([], dtype='O'))
result = lib.infer_dtype([])
self.assertEqual(result, 'empty')

def test_integers(self):
Expand Down