Skip to content

BUG: df.clip does not handle inverted lower/upper bounds consistently  #52147

Open
@jkew

Description

@jkew

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

import pandas as pd
import numpy as np
data = {'A': [-2, -1, 0, 1, 2], 'B': [-2, 1, 0, 1, 2]}
df = pd.DataFrame(data)
df
	A	B
0	-2	-2
1	-1	1
2	0	0
3	1	1
4	2	2
df.clip(lower=10, upper=[1,1])
A	B
0	10	10
1	10	10
2	10	10
3	10	10
4	1	1

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignError ReportingIncorrect or improved errors from pandasNumeric OperationsArithmetic, Comparison, and Logical operations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions