I have a pandas dataframe consisting of 13 columns of daily stock returns for certain stocks. I want to calculate the geometric mean of each column but some have zeros in the column as those businesses materialized on the stock market at different times.
I know numpy's arithmetic mean will ignore NaNs. Is there some way to calculate the geometric mean and ignore zeros at the same time?
sample df:
import pandas as pd
dictA = {'AAPL': [.02, -.001, .05, .43], 'ABC':[.03, -.02, -.05, 0], 'DEF': [.045, 0, -.10, .63]}
df = pd.DataFrame(dictA)
The geometric mean for AAPL would be .02 * -.001 * .05 * .43**(1/N) where N is the number of observations.
Is there some sort of slick code that can calculate the geometric mean while ignoring zeros?