Skip to content

Commit 8d952a1

Browse files
committed
ENH: provide squeeze method for removing 1-len dimensions, close (GH pandas-dev#2544)
1 parent 3ba3119 commit 8d952a1

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed

RELEASE.rst

+13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ Where to get it
2222
* Binary installers on PyPI: http://pypi.python.org/pypi/pandas
2323
* Documentation: http://pandas.pydata.org
2424

25+
**New features**
26+
27+
- added ``squeeze`` method that will reduce dimensionality of the object by squeezing any len-1 dimensions
28+
29+
pandas 0.10.2
30+
=============
31+
32+
**Release date:** 2013-02-??
33+
34+
**New features**
35+
36+
- Add ``squeeze`` function to reduce dimensionality of 1-len objects
37+
2538
pandas 0.10.1
2639
=============
2740

doc/source/dsintro.rst

+12
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,17 @@ For example, using the earlier example data, we could do:
819819
wp.minor_axis
820820
wp.minor_xs('C')
821821
822+
Squeezing
823+
~~~~~~~~~
824+
825+
Another way to change the dimensionality of an object is to ``squeeze`` a 1-len object, similar to ``wp['Item1']``
826+
827+
.. ipython:: python
828+
829+
wp.reindex(items=['Item1']).squeeze()
830+
wp.reindex(items=['Item1'],minor=['B']).squeeze()
831+
832+
822833
Conversion to DataFrame
823834
~~~~~~~~~~~~~~~~~~~~~~~
824835

@@ -834,6 +845,7 @@ method:
834845
minor_axis=['a', 'b', 'c', 'd'])
835846
panel.to_frame()
836847
848+
837849
Panel4D (Experimental)
838850
----------------------
839851

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+

doc/source/whatsnew.rst

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ What's New
1616

1717
These are new features and improvements of note in each release.
1818

19+
.. include:: v0.10.2.txt
20+
1921
.. include:: v0.10.1.txt
2022

2123
.. include:: v0.10.0.txt

pandas/core/generic.py

+7
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,13 @@ def pop(self, item):
587587
del self[item]
588588
return result
589589

590+
def squeeze(self):
591+
""" squeeze length 1 dimensions """
592+
try:
593+
return self.ix[tuple([ slice(None) if len(a) > 1 else a[0] for a in self.axes ])]
594+
except:
595+
return self
596+
590597
def _expand_axes(self, key):
591598
new_axes = []
592599
for k, ax in zip(key, self.axes):

pandas/tests/test_ndframe.py

+27
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,33 @@ def test_astype(self):
2626
casted = self.ndf.astype(int)
2727
self.assert_(casted.values.dtype == np.int64)
2828

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

0 commit comments

Comments
 (0)