Skip to content

BUG: rolling().skew modifies original data column #39404

Closed
@jwyndham

Description

@jwyndham
  • [x ] I have checked that this issue has not already been reported.

  • [x ] I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

df = pd.DataFrame(np.random.random(100)*10, columns=['A'])
           A
0   5.076681
1   6.959102
2   9.989156
..       ...
97  3.475425
98  3.280561
99  6.966864

Apply skew to create new column and df['A'] is now modified unexpectedly

df['skew'] = df['A'].rolling(10).skew()
           A      skew
0   0.076681       NaN
1   1.959102       NaN
2   4.989156       NaN
..       ...       ...
97 -1.524575 -0.482644
98 -1.719439 -0.114206
99  1.966864 -0.414480

Problem description

rolling().skew modifies original data column. This is an unexpected and problematic behaviour since it does not occur with other rolling window methods like mean(), std() and `'kurt'. A coder would reasonably expect that these methods can be applied in the same way, therefore it is likely to cause issues that are hard to debug.

It can be worked around by using using copy, but again, this is a deviation from the usage of other similar methods: df['A'].copy().rolling(10).skew()

Expected Output

           A      skew
0   5.076681       NaN
1   6.959102       NaN
2   9.989156       NaN
..       ...       ...
97  3.475425 -0.482644
98  3.280561 -0.114206
99  6.966864 -0.414480

           A
0   5.076681
1   6.959102
2   9.989156
..       ...
97  3.475425
98  3.280561
99  6.966864

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 3e89b4c
python : 3.8.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Australia.1252

pandas : 1.2.0
numpy : 1.19.5
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
setuptools : 40.8.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : 2.7.2
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDuplicate ReportDuplicate issue or pull requestWindowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions