Skip to content

Commit 970d7e3

Browse files
authored
Merge pull request numpy#26384 from WarrenWeckesser/fix-gh-26375
BUG: Fix incorrect return type of item with length 0 from chararray.__getitem__
2 parents a0e6492 + 2bd6dca commit 970d7e3

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

numpy/_core/defchararray.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -577,14 +577,8 @@ def __array_finalize__(self, obj):
577577

578578
def __getitem__(self, obj):
579579
val = ndarray.__getitem__(self, obj)
580-
581580
if isinstance(val, character):
582-
temp = val.rstrip()
583-
if len(temp) == 0:
584-
val = ''
585-
else:
586-
val = temp
587-
581+
return val.rstrip()
588582
return val
589583

590584
# IMPLEMENTATION NOTE: Most of the methods of this class are

numpy/_core/tests/test_defchararray.py

+10
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,16 @@ def test_slice(self):
738738

739739
assert_(arr[0, 0] == b'abc')
740740

741+
@pytest.mark.parametrize('data', [['plate', ' ', 'shrimp'],
742+
[b'retro', b' ', b'encabulator']])
743+
def test_getitem_length_zero_item(self, data):
744+
# Regression test for gh-26375.
745+
a = np.char.array(data)
746+
# a.dtype.type() will be an empty string or bytes instance.
747+
# The equality test will fail if a[1] has the wrong type
748+
# or does not have length 0.
749+
assert_equal(a[1], a.dtype.type())
750+
741751

742752
class TestMethodsEmptyArray:
743753
def setup_method(self):

0 commit comments

Comments
 (0)