From 915dcc2357cb3e29c9fec85efeee7c202a05dfe1 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sat, 3 Oct 2015 14:01:16 -0400 Subject: [PATCH] squeeze works on 0 length arrays --- doc/source/whatsnew/v0.17.1.txt | 2 ++ pandas/core/generic.py | 2 +- pandas/tests/test_generic.py | 9 +++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.17.1.txt b/doc/source/whatsnew/v0.17.1.txt index 736554672a089..b4ed00189c710 100755 --- a/doc/source/whatsnew/v0.17.1.txt +++ b/doc/source/whatsnew/v0.17.1.txt @@ -42,3 +42,5 @@ Performance Improvements Bug Fixes ~~~~~~~~~ + +- Bug in ``squeeze()`` with zero length arrays (:issue:`11230`, :issue:`8999`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3473dd0f7cd88..53ce5c6b4b751 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -513,7 +513,7 @@ def pop(self, item): def squeeze(self): """ squeeze length 1 dimensions """ try: - return self.ix[tuple([slice(None) if len(a) > 1 else a[0] + return self.iloc[tuple([0 if len(a) == 1 else slice(None) for a in self.axes])] except: return self diff --git a/pandas/tests/test_generic.py b/pandas/tests/test_generic.py index 3a26be2ca1032..061382e0e16de 100644 --- a/pandas/tests/test_generic.py +++ b/pandas/tests/test_generic.py @@ -1717,6 +1717,15 @@ def test_squeeze(self): p4d = tm.makePanel4D().reindex(labels=['label1'],items=['ItemA']) tm.assert_frame_equal(p4d.squeeze(),p4d.ix['label1','ItemA']) + # don't fail with 0 length dimensions GH11229 & GH8999 + empty_series=pd.Series([], name='five') + empty_frame=pd.DataFrame([empty_series]) + empty_panel=pd.Panel({'six':empty_frame}) + + [tm.assert_series_equal(empty_series, higher_dim.squeeze()) + for higher_dim in [empty_series, empty_frame, empty_panel]] + + def test_equals(self): s1 = pd.Series([1, 2, 3], index=[0, 2, 1]) s2 = s1.copy()