Home > Enterprise >  Python pandas trying to get Cummin but get a zero value
Python pandas trying to get Cummin but get a zero value

Time:09-15

I want to use cummin but it doesnt work, It shows zero, cummax work well.

my code:

df = pd.DataFrame(df, columns=['symbol', 'timestamp', 'open', 'high', 'low', 'close', 'volume'])
df["min_Close"] = df.close.shift(fill_value=0).cummin()
df["max_Close"] = df.close.shift(fill_value=0).cummax()
print(df)

original df:

      symbol      timestamp   open   high    low  close   volume
0   AAVEUSDT  1663186500000  81.70  81.90  81.65  81.80   405.54
1   AAVEUSDT  1663186800000  81.80  81.85  81.70  81.75   233.11
2   AAVEUSDT  1663187100000  81.75  82.05  81.75  81.75   346.30
3   AAVEUSDT  1663187400000  81.75  81.90  81.60  81.90   126.11
4   AAVEUSDT  1663187700000  81.90  81.95  81.70  81.75   260.90
5   AAVEUSDT  1663188000000  81.75  81.75  81.60  81.65   274.26

my result :

      symbol      timestamp   open   high    low  close   volume  min_Close  max_Close
0   AAVEUSDT  1663186500000  81.70  81.90  81.65  81.80   405.54        0.0       0.00
1   AAVEUSDT  1663186800000  81.80  81.85  81.70  81.75   233.11        0.0      81.80
2   AAVEUSDT  1663187100000  81.75  82.05  81.75  81.75   346.30        0.0      81.80
3   AAVEUSDT  1663187400000  81.75  81.90  81.60  81.90   126.11        0.0      81.80
4   AAVEUSDT  1663187700000  81.90  81.95  81.70  81.75   260.90        0.0      81.90
5   AAVEUSDT  1663188000000  81.75  81.75  81.60  81.65   274.26        0.0      81.90

CodePudding user response:

It is expected, because after shift first value is 0, so after cummin is here always 0 (until negative value):

print (df.close.shift(fill_value=0))
0     0.00
1    81.80
2    81.75
3    81.75
4    81.90
5    81.75
Name: close, dtype: float64

Remove fill_value=0 for NaN instead 0 for first value in Series.shift, last if necessary replace first missing value by Series.fillna:

print (df.close.shift())
0      NaN
1    81.80
2    81.75
3    81.75
4    81.90
5    81.75
Name: close, dtype: float64

df["min_Close"] = df.close.shift().cummin().fillna(0)
print (df)
     symbol      timestamp   open   high    low  close  volume  min_Close
0  AAVEUSDT  1663186500000  81.70  81.90  81.65  81.80  405.54       0.00
1  AAVEUSDT  1663186800000  81.80  81.85  81.70  81.75  233.11      81.80
2  AAVEUSDT  1663187100000  81.75  82.05  81.75  81.75  346.30      81.75
3  AAVEUSDT  1663187400000  81.75  81.90  81.60  81.90  126.11      81.75
4  AAVEUSDT  1663187700000  81.90  81.95  81.70  81.75  260.90      81.75
5  AAVEUSDT  1663188000000  81.75  81.75  81.60  81.65  274.26      81.75
  • Related