import pandas as pd
from pandas_datareader import data as pdr
import numpy as np
import datetime

end = datetime.date.today()
begin=end-pd.DateOffset(365*10)
st=begin.strftime('%Y-%m-%d')
ed=end.strftime('%Y-%m-%d')


data = pdr.get_data_yahoo("AAPL",st,ed)

def bollinger_strat(data, window, no_of_std):
    rolling_mean = data['Close'].rolling(window).mean()
    rolling_std = data['Close'].rolling(window).std()

    df['Bollinger High'] = rolling_mean + (rolling_std * no_of_std)
    df['Bollinger Low'] = rolling_mean - (rolling_std * no_of_std)     

bollinger_strat(data,20,2)
Answer from Abhishek Kulkarni on Stack Overflow
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.3 › reference › api › pandas.core.window.rolling.Rolling.std.html
pandas.core.window.rolling.Rolling.std — pandas 2.3.3 documentation
The default ddof of 1 used in Series.std() is different than the default ddof of 0 in numpy.std(). A minimum of one period is required for the rolling calculation.
🌐
Statology
statology.org › home › how to calculate a rolling standard deviation in pandas
How to Calculate a Rolling Standard Deviation in Pandas
April 19, 2024 - The easiest way to calculate a rolling standard deviation in pandas is by using the Rolling.std() function, which uses the following basic syntax:
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.api.typing.Rolling.std.html
pandas.api.typing.Rolling.std — pandas 3.0.4 documentation
The default ddof of 1 used in Series.std() is different than the default ddof of 0 in numpy.std(). A minimum of one period is required for the rolling calculation.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.23 › generated › pandas.core.window.Rolling.std.html
pandas.core.window.Rolling.std — pandas 0.23.1 documentation
Extending Pandas · Release Notes · Enter search terms or a module, class or function name. Rolling.std(ddof=1, *args, **kwargs)[source]¶ · Calculate rolling standard deviation. Normalized by N-1 by default. This can be changed using the ddof argument. See also · Series.rolling ·
🌐
GitHub
github.com › pandas-dev › pandas › issues › 39872
BUG: pandas.core.window.rolling.Rolling.std gives all-zero output for small numbers · Issue #39872 · pandas-dev/pandas
February 17, 2021 - It seems std() on a rolling window in pandas 1.2.2 rounds the numbers to exact 0.0 if the result would be < 1.0e-7.
Author: pandas-dev
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.2 › reference › api › pandas.core.window.rolling.Rolling.std.html
pandas.core.window.rolling.Rolling.std — pandas 2.2.3 documentation
The default ddof of 1 used in Series.std() is different than the default ddof of 0 in numpy.std(). A minimum of one period is required for the rolling calculation.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.rolling.html
pandas.DataFrame.rolling — pandas 3.0.6 documentation
Rolling sum with a window length of 2, using the Scipy 'gaussian' window type. std is required in the aggregation function.
🌐
Intelpython
intelpython.github.io › sdc-doc › latest › _api_ref › pandas.core.window.Rolling.std.html
pandas.core.window.Rolling.std — Intel® Scalable Dataframe Compiler 0.1 documentation
import pandas as pd from numba import njit @njit def df_rolling_std(): df = pd.DataFrame({'A': [4, 3, 5, 2, 6], 'B': [-4, -3, -5, -2, -6]}) out_df = df.rolling(3).std() # Expect DataFrame of # {'A': [NaN, NaN, 1.000000, 1.527525, 2.081666], # 'B': [NaN, NaN, 1.000000, 1.527525, 2.081666]} return out_df print(df_rolling_std()) $ python ./dataframe/rolling/dataframe_rolling_std.py A B 0 NaN NaN 1 NaN NaN 2 1.000000 1.000000 3 1.527525 1.527525 4 2.081666 2.081666 ·
Find elsewhere
🌐
GitHub
github.com › pandas-dev › pandas › issues › 46049
BUG: Pandas rolling std precision error · Issue #46049 · pandas-dev/pandas
February 18, 2022 - import pandas as pd data = pd.Series([-3.0, -3.0, -4.0, -5.0, -4.0, -3.0, -4.0, -4.0, -3.0, -4.0, -3.0, -4.0, -3.0, -2.0, -2.0, -3.0, -2.0, -2.0, -3.0, -3.0, -4.0, -4.0, -4.0, -4.0, -4.0, -5.0, -5.0, -5.0, -5.0, -5.0]) print(data.rolling(5).std().iloc[-1]) # <-- yields 1.2904784139758924e-08 instead of 0 print(data.tail(25).rolling(5).std().iloc[-1]) # <-- correctly yields 0 ·
Author: pandas-dev
🌐
Delft Stack
delftstack.com › home › howto › python pandas › pandas rolling standard deviation
How to Calculate the Rolling Standard Deviation in Pandas | Delft Stack
February 16, 2024 - Previously, and more likely in legacy statistical code, to calculate rolling standard deviation, you will see the use of the Pandas rolling_std() function, which was previously used to make said calculation.
🌐
Medium
medium.com › @whyamit101 › understanding-pandas-rolling-f8f6d6796c07
Understanding Pandas Rolling. If you think you need to spend $2,000… | by why amit | Medium
February 26, 2025 - The rolling mean computes the average of each window, while other functions like sum() compute the total sum, and std() calculates the standard deviation over each window. You can even create custom aggregation functions using apply() to tailor ...
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.0 › reference › api › pandas.core.window.rolling.Window.std.html
pandas.core.window.rolling.Window.std — pandas 2.0.3 documentation
Window.std(ddof=1, numeric_only=False, **kwargs)[source]# Calculate the rolling weighted window standard deviation. Parameters · numeric_onlybool, default False · Include only float, int, boolean columns. New in version 1.5.0. **kwargs · Keyword arguments to configure the SciPy weighted window type. Returns · Series or DataFrame · Return type is the same as the original object with np.float64 dtype. See also · pandas.Series.rolling ·
🌐
GitHub
github.com › pandas-dev › pandas › issues › 21786
.rolling().std() only returns NaN in Python3.7 · Issue #21786 · pandas-dev/pandas
July 7, 2018 - import pandas as pd d = {"col": [1, 23, 231, 231, 4, 353, 62, 3, 56, 43, 354, 43, 231, 21, 7]} df = pd.DataFrame(data=d) std = df["col"].std() df["mean5"] = df["col"].rolling(5).mean() df["std5"] = df["col"].rolling(5).std() print(std) p...
Author: pandas-dev
🌐
Codefinity
codefinity.com › courses › v2 › 63d06973-1096-4188-a086-950538bb8310 › 43c99e58-1e45-473c-bda6-5c8106bf2fd7 › 7372bec7-408a-4d7a-a65b-b521d0173927
Learn Rolling Statistics for Trend Detection | Section
123456789101112131415 import pandas as pd # Create a sample time series dataset data = { "date": pd.date_range(start="2024-01-01", periods=10, freq="D"), "value": [10, 12, 13, 15, 14, 16, 18, 17, 19, 20] } df = pd.DataFrame(data) df.set_index("date", inplace=True) # Calculate rolling mean and rolling standard deviation with a window of 3 days df["rolling_mean"] = df["value"].rolling(window=3).mean() df["rolling_std"] = df["value"].rolling(window=3).std() print(df)
🌐
GitHub
github.com › pandas-dev › pandas › issues › 47721
BUG: Rolling std() error · Issue #47721 · pandas-dev/pandas
July 14, 2022 - import pandas as pd data=[1,-1,0,1,3,2,-2,10000000000,1,2,0,-2,1,3,0,1] df=pd.DataFrame(data,columns=['data']) df.data.rolling(6).std()[-1:] 15 57.250852 df.data.tail(6).std() 1.6431676725154984 · When there is a large outliner in the data, then rolling().std() and tail().std() come to different results.
Author: pandas-dev
🌐
GitHub
github.com › pandas-dev › pandas › issues › 26597
Rolling standard deviation fails when used with win_type · Issue #26597 · pandas-dev/pandas
May 31, 2019 - import pandas as pd df = pd.DataFrame({'a': range(6)}) df['a'].rolling(3, win_type=None).agg(['mean', 'std'])
Author: pandas-dev