-
-
Notifications
You must be signed in to change notification settings - Fork 18k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: rolling window with center=True, min_periods=1
is not symmetric at edges
#59252
Comments
take |
Hi , could you try with If this is not the case you are talking about, could you please give me some more explanation on what is the expected output? |
If you don't have |
@jack-walp - for the first data point are you wanting the window size to be 1, the 2nd the window size to be 3, etc, until 21 is hit and then the window size is 21? |
@rhshadrach yes, that's correct. The key thing is that the window is symmetric about the evaluation point. |
Expanding the window size is not how the rolling window in pandas is supposed to operate by default, so is not a bug. This can be accomplished by Custom window rolling. Here is an example for this case (I've only checked it on odd window sizes): window_size = 21
ser = pd.Series(np.arange(100))
from pandas.api.indexers import BaseIndexer
class CustomIndexer(BaseIndexer):
def get_window_bounds(self, num_values, min_periods, center, closed, step):
start = np.empty(num_values, dtype=np.int64)
end = np.empty(num_values, dtype=np.int64)
for i in range(num_values):
to_end = min(i, num_values - i - 1, (self.window_size - 1) // 2)
if i > to_end:
start[i] = i - to_end
else:
start[i] = 0
if num_values - i > to_end:
end[i] = i + to_end + 1
else:
end[i] = num_values
return start, end Closing. |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
The
np.arange
gives a simple linear trend that should not be affected by the rolling mean filter. However at the edges the mean filter pulls values more towards the centre than expected, causing kinks in the curve. It looks like at the edge of the data Null values creep into the window and these are ignored by the mean filter. Because Null values only creep into one side of the window the effective centre value gets offset.Expected Behavior
I would expect both sides of the window to be shrunk so that the point under examination is the centre of the live data.
Installed Versions
INSTALLED VERSIONS
commit : bdc79c1
python : 3.12.2.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-1160.36.2.el7.x86_64
Version : #1 SMP Wed Jul 21 11:57:15 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.8.2
setuptools : 69.1.1
pip : 24.0
Cython : 3.0.10
pytest : None
hypothesis : None
sphinx : 7.2.6
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.22.1
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.3
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : 2.4.1
pyqt5 : None
The text was updated successfully, but these errors were encountered: