Skip to content

Commit c6b2cff

Browse files
jrebackwesm
authored andcommitted
ENH: provide squeeze method for removing 1-len dimensions, close (GH #2544)
1 parent 19d15a3 commit c6b2cff

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pandas 0.11.0
3333
DataFrames and propogate in operations
3434
- Add function to pandas.io.data for retrieving stock index components from
3535
Yahoo! finance (#2795)
36+
- Add ``squeeze`` function to reduce dimensionality of 1-len objects
3637

3738
**Improvements to existing features**
3839

doc/source/dsintro.rst

+12
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,17 @@ For example, using the earlier example data, we could do:
885885
wp.minor_axis
886886
wp.minor_xs('C')
887887
888+
Squeezing
889+
~~~~~~~~~
890+
891+
Another way to change the dimensionality of an object is to ``squeeze`` a 1-len object, similar to ``wp['Item1']``
892+
893+
.. ipython:: python
894+
895+
wp.reindex(items=['Item1']).squeeze()
896+
wp.reindex(items=['Item1'],minor=['B']).squeeze()
897+
898+
888899
Conversion to DataFrame
889900
~~~~~~~~~~~~~~~~~~~~~~~
890901

@@ -900,6 +911,7 @@ method:
900911
minor_axis=['a', 'b', 'c', 'd'])
901912
panel.to_frame()
902913
914+
903915
Panel4D (Experimental)
904916
----------------------
905917

doc/source/v0.10.2.txt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.. _whatsnew_0102:
2+
3+
v0.10.2 (February ??, 2013)
4+
---------------------------
5+
6+
This is a minor release from 0.10.1 and includes many new features and
7+
enhancements along with a large number of bug fixes. There are also a number of
8+
important API changes that long-time pandas users should pay close attention
9+
to.
10+
11+
New features
12+
~~~~~~~~~~~~
13+
14+
``Squeeze`` to possibly remove length 1 dimensions from an object.
15+
16+
.. ipython:: python
17+
18+
p = Panel(randn(3,4,4),items=['ItemA','ItemB','ItemC'],
19+
major_axis=date_range('20010102',periods=4),
20+
minor_axis=['A','B','C','D'])
21+
p
22+
p.reindex(items=['ItemA']).squeeze()
23+
p.reindex(items=['ItemA'],minor=['B']).squeeze()
24+
25+
26+
See the `full release notes
27+
<https://github.com/pydata/pandas/blob/master/RELEASE.rst>`__ or issue tracker
28+
on GitHub for a complete list.
29+

pandas/core/generic.py

+7
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,13 @@ def pop(self, item):
591591
del self[item]
592592
return result
593593

594+
def squeeze(self):
595+
""" squeeze length 1 dimensions """
596+
try:
597+
return self.ix[tuple([ slice(None) if len(a) > 1 else a[0] for a in self.axes ])]
598+
except:
599+
return self
600+
594601
def _expand_axes(self, key):
595602
new_axes = []
596603
for k, ax in zip(key, self.axes):

pandas/tests/test_ndframe.py

+27
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,33 @@ def test_astype(self):
2929
casted = self.ndf.astype(np.int32)
3030
self.assert_(casted.values.dtype == np.int32)
3131

32+
def test_squeeze(self):
33+
# noop
34+
for s in [ t.makeFloatSeries(), t.makeStringSeries(), t.makeObjectSeries() ]:
35+
t.assert_series_equal(s.squeeze(),s)
36+
for df in [ t.makeTimeDataFrame() ]:
37+
t.assert_frame_equal(df.squeeze(),df)
38+
for p in [ t.makePanel() ]:
39+
t.assert_panel_equal(p.squeeze(),p)
40+
for p4d in [ t.makePanel4D() ]:
41+
t.assert_panel4d_equal(p4d.squeeze(),p4d)
42+
43+
# squeezing
44+
df = t.makeTimeDataFrame().reindex(columns=['A'])
45+
t.assert_series_equal(df.squeeze(),df['A'])
46+
47+
p = t.makePanel().reindex(items=['ItemA'])
48+
t.assert_frame_equal(p.squeeze(),p['ItemA'])
49+
50+
p = t.makePanel().reindex(items=['ItemA'],minor_axis=['A'])
51+
t.assert_series_equal(p.squeeze(),p.ix['ItemA',:,'A'])
52+
53+
p4d = t.makePanel4D().reindex(labels=['label1'])
54+
t.assert_panel_equal(p4d.squeeze(),p4d['label1'])
55+
56+
p4d = t.makePanel4D().reindex(labels=['label1'],items=['ItemA'])
57+
t.assert_frame_equal(p4d.squeeze(),p4d.ix['label1','ItemA'])
58+
3259
if __name__ == '__main__':
3360
import nose
3461
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)