I am working with a rather large dataset. After applying the resample command in combination with the conversion method "prod" (multiplication), I realized that my NaN values were changed to 1, which is not what I intended. To give an example what happened:
# build random dataframe with one column containing NaN
import pandas as pd
import numpy as np
index = pd.date_range('1/1/2000', periods=7, freq='d')
df = pd.DataFrame(index = index, columns = ["Score 1", "Score 2", "Score 3"])
df["Score 1"] = np.random.randint(1,20,size=7)
df["Score 2"] = np.random.randint(1,20,size=7)
df["Score 3"] = [1, 2, 3, np.NaN, np.NaN, np.NaN, np.NaN]
print(df)
Score 1 Score 2 Score 3
2000-01-01 6 7 1.0
2000-01-02 2 15 2.0
2000-01-03 8 19 3.0
2000-01-04 14 19 NaN
2000-01-05 17 8 NaN
2000-01-06 15 6 NaN
2000-01-07 12 18 NaN
Now lets say I want to resample my Dataframe from a daily to a 3-day Frequency with using the "prod" conversion method. I do so by:
df.resample("3d").agg("prod")
print(df)
Score 1 Score 2 Score 3
2000-01-01 96 1995 6.0
2000-01-04 3570 2052 1.0
2000-01-07 12 18 1.0
Looking at the column "Score 3", my NaN values suddenly changed to 1, which is a surprise for me. This means that when multiplying NaN with each other, I would get =1. Does anyone why exactly a multiplication of NaN's equals one and what I could do to keep the NaN value in case it is multiplicated with itself?
Thanks in advance, any help is highly appreciated