-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: df.clip does not handle inverted lower/upper bounds consistently #52147
Labels
API Design
Error Reporting
Incorrect or improved errors from pandas
Numeric Operations
Arithmetic, Comparison, and Logical operations
Comments
jkew
added
Bug
Needs Triage
Issue that has not been reviewed by a pandas team member
labels
Mar 23, 2023
take |
Ok, I tested clip method and it is quite a mess. >>> import pandas as pd
>>> import numpy as np
>>> data = {'A': range(10), 'B': range(10)}
>>> df = pd.DataFrame(data)
>>> df
A B
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9 When >>> expected = df.clip(lower=3, upper=7)
>>> expected
A B
0 3 3
1 3 3
2 3 3
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 7 7
9 7 7
>>> all(expected == df.clip(lower=3, upper=[7, 7]))
True
>>> all(expected == df.clip(lower=[3, 3], upper=7))
True When we swap >>> df.clip(lower=7, upper=[3, 3]) # 1
A B
0 7 7
1 7 7
2 7 7
3 7 7
4 3 3
5 3 3
6 3 3
7 3 3
8 3 3
9 3 3
>>> df.clip(lower=[7, 7], upper=3) # 2
A B
0 3 3
1 3 3
2 3 3
3 3 3
4 3 3
5 3 3
6 3 3
7 3 3
8 3 3
9 3 3
>>> df.clip(lower=7, upper=3) # 3
A B
0 3 3
1 3 3
2 3 3
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 7 7
9 7 7
In my opinion, there are two reasonable approaches:
I will start coding first one, to be consistent with numpy. If you have other idea, discussion is welcome. |
m-ganko
added a commit
to m-ganko/pandas
that referenced
this issue
Apr 2, 2023
* Before when lower > upper arguments were swapped, changed to be consistent with numpy, all equal to upper * test added
m-ganko
added a commit
to m-ganko/pandas
that referenced
this issue
Apr 2, 2023
* DataFrame.clip fixed for lower > upper with list-like arguments
m-ganko
added a commit
to m-ganko/pandas
that referenced
this issue
Apr 2, 2023
5 tasks
m-ganko
added a commit
to m-ganko/pandas
that referenced
this issue
Apr 2, 2023
lithomas1
added
API Design
Error Reporting
Incorrect or improved errors from pandas
Numeric Operations
Arithmetic, Comparison, and Logical operations
and removed
Bug
Needs Triage
Issue that has not been reviewed by a pandas team member
labels
May 8, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
API Design
Error Reporting
Incorrect or improved errors from pandas
Numeric Operations
Arithmetic, Comparison, and Logical operations
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
This is undocumented behavior; but when upper is < lower, and upper is not a scalar the resulting data frame is not consistently producing the same results. If lower is an array, and upper is a scalar the values are fixed at whatever the scalar upper is.
I'm not sure if pandas should simply throw an error when the bounds are inverted, or what - but some consistency would be helpful.
Expected Behavior
Either throw an error when upper < lower or have a consistent way to handle the result between scalar and list-like things.
Installed Versions
INSTALLED VERSIONS
commit : 2e218d1
python : 3.10.9.final.0
python-bits : 64
OS : Darwin
OS-release : 21.4.0
Version : Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58 PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.5.3
numpy : 1.24.2
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 67.6.0
pip : 23.0
Cython : None
pytest : 7.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.10.0
pandas_datareader: None
bs4 : 4.11.2
bottleneck : None
brotli :
fastparquet : None
fsspec : 2023.3.0
gcsfs : None
matplotlib : 3.7.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 10.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : 2.0.4
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : 0.19.0
tzdata : None
The text was updated successfully, but these errors were encountered: