Skip to content

Boolean indexing on an empty series loses index names (rebase of 4236) #4658

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 2 commits into from
Aug 23, 2013
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
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
- Fix bug in ``pd.read_clipboard`` on windows with PY3 (:issue:`4561`); not decoding properly
- ``tslib.get_period_field()`` and ``tslib.get_period_field_arr()`` now raise
if code argument out of range (:issue:`4519`, :issue:`4520`)
- Fix boolean indexing on an empty series loses index names (:issue:`4235`),
infer_dtype works with empty arrays.
- Fix reindexing with multiple axes; if an axes match was not replacing the current axes, leading
to a possible lazay frequency inference issue (:issue:`3317`)
- Fixed issue where ``DataFrame.apply`` was reraising exceptions incorrectly
Expand Down
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 @@ -740,6 +740,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 @@ -565,9 +565,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