Skip to content

Commit aa94ae4

Browse files
max-sixtyjreback
authored andcommitted
BUG: squeeze works on 0 length arrays, #11299, #8999
1 parent 55f83d5 commit aa94ae4

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v0.17.1.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ Performance Improvements
4242

4343
Bug Fixes
4444
~~~~~~~~~
45-
Bug in ``.to_latex()`` output broken when the index has a name (:issue: `10660`)
45+
46+
- Bug in ``.to_latex()`` output broken when the index has a name (:issue: `10660`)
47+
- Bug in ``squeeze()`` with zero length arrays (:issue:`11230`, :issue:`8999`)

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def pop(self, item):
513513
def squeeze(self):
514514
""" squeeze length 1 dimensions """
515515
try:
516-
return self.ix[tuple([slice(None) if len(a) > 1 else a[0]
516+
return self.iloc[tuple([0 if len(a) == 1 else slice(None)
517517
for a in self.axes])]
518518
except:
519519
return self

pandas/tests/test_generic.py

+9
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,15 @@ def test_squeeze(self):
17171717
p4d = tm.makePanel4D().reindex(labels=['label1'],items=['ItemA'])
17181718
tm.assert_frame_equal(p4d.squeeze(),p4d.ix['label1','ItemA'])
17191719

1720+
# don't fail with 0 length dimensions GH11229 & GH8999
1721+
empty_series=pd.Series([], name='five')
1722+
empty_frame=pd.DataFrame([empty_series])
1723+
empty_panel=pd.Panel({'six':empty_frame})
1724+
1725+
[tm.assert_series_equal(empty_series, higher_dim.squeeze())
1726+
for higher_dim in [empty_series, empty_frame, empty_panel]]
1727+
1728+
17201729
def test_equals(self):
17211730
s1 = pd.Series([1, 2, 3], index=[0, 2, 1])
17221731
s2 = s1.copy()

0 commit comments

Comments
 (0)