Examples
import numpy as np
import pandas as pd
primes = pd.Series([3, 4, 5, 7])
Slicing might produce a Series with a single value:
even_primes = primes[primes % 2 == 0]
even_primes
even_primes.squeeze()
Squeezing objects with more than one value in every axis does nothing:
odd_primes = primes[primes % 2 == 1]
odd_primes
odd_primes.squeeze()
Squeezing is even more effective when used with DataFrames.
df = pd.DataFrame([[2, 3], [4, 5]], columns=['x', 'y'])
df
Slicing a single column will produce a DataFrame with the columns having only one value:
df_a = df[['x']]
df_a
So the columns can be squeezed down, resulting in a Series:
df_a.squeeze('columns')
Slicing a single row from a single column will produce a single scalar DataFrame:
df_0a = df.loc[df.index < 1, ['x']]
df_0a
Squeezing the rows produces a single scalar Series:
df_0a.squeeze('rows')
Squeezing all axes will project directly into a scalar:
df_0a.squeeze()