Skip to content

Commit a82d779

Browse files
villasvjschendel
authored andcommitted
DOC: update NDFrame.squeeze docstring (#20269)
1 parent fc46fb7 commit a82d779

File tree

1 file changed

+94
-4
lines changed

1 file changed

+94
-4
lines changed

pandas/core/generic.py

+94-4
Original file line numberDiff line numberDiff line change
@@ -768,18 +768,108 @@ def pop(self, item):
768768

769769
def squeeze(self, axis=None):
770770
"""
771-
Squeeze length 1 dimensions.
771+
Squeeze 1 dimensional axis objects into scalars.
772+
773+
Series or DataFrames with a single element are squeezed to a scalar.
774+
DataFrames with a single column or a single row are squeezed to a
775+
Series. Otherwise the object is unchanged.
776+
777+
This method is most useful when you don't know if your
778+
object is a Series or DataFrame, but you do know it has just a single
779+
column. In that case you can safely call `squeeze` to ensure you have a
780+
Series.
772781
773782
Parameters
774783
----------
775-
axis : None, integer or string axis name, optional
776-
The axis to squeeze if 1-sized.
784+
axis : axis : {0 or ‘index’, 1 or ‘columns’, None}, default None
785+
A specific axis to squeeze. By default, all length-1 axes are
786+
squeezed.
777787
778788
.. versionadded:: 0.20.0
779789
780790
Returns
781791
-------
782-
scalar if 1-sized, else original object
792+
DataFrame, Series, or scalar
793+
The projection after squeezing `axis` or all the axes.
794+
795+
See Also
796+
--------
797+
Series.iloc : Integer-location based indexing for selecting scalars
798+
DataFrame.iloc : Integer-location based indexing for selecting Series
799+
Series.to_frame : Inverse of DataFrame.squeeze for a
800+
single-column DataFrame.
801+
802+
Examples
803+
--------
804+
>>> primes = pd.Series([2, 3, 5, 7])
805+
806+
Slicing might produce a Series with a single value:
807+
808+
>>> even_primes = primes[primes % 2 == 0]
809+
>>> even_primes
810+
0 2
811+
dtype: int64
812+
813+
>>> even_primes.squeeze()
814+
2
815+
816+
Squeezing objects with more than one value in every axis does nothing:
817+
818+
>>> odd_primes = primes[primes % 2 == 1]
819+
>>> odd_primes
820+
1 3
821+
2 5
822+
3 7
823+
dtype: int64
824+
825+
>>> odd_primes.squeeze()
826+
1 3
827+
2 5
828+
3 7
829+
dtype: int64
830+
831+
Squeezing is even more effective when used with DataFrames.
832+
833+
>>> df = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])
834+
>>> df
835+
a b
836+
0 1 2
837+
1 3 4
838+
839+
Slicing a single column will produce a DataFrame with the columns
840+
having only one value:
841+
842+
>>> df_a = df[['a']]
843+
>>> df_a
844+
a
845+
0 1
846+
1 3
847+
848+
So the columns can be squeezed down, resulting in a Series:
849+
850+
>>> df_a.squeeze('columns')
851+
0 1
852+
1 3
853+
Name: a, dtype: int64
854+
855+
Slicing a single row from a single column will produce a single
856+
scalar DataFrame:
857+
858+
>>> df_0a = df.loc[df.index < 1, ['a']]
859+
>>> df_0a
860+
a
861+
0 1
862+
863+
Squeezing the rows produces a single scalar Series:
864+
865+
>>> df_0a.squeeze('rows')
866+
a 1
867+
Name: 0, dtype: int64
868+
869+
Squeezing all axes wil project directly into a scalar:
870+
871+
>>> df_0a.squeeze()
872+
1
783873
"""
784874
axis = (self._AXIS_NAMES if axis is None else
785875
(self._get_axis_number(axis),))

0 commit comments

Comments
 (0)