Skip to content

BUG: Selecting columns from a DataFrame affects display of warnings #57054

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

Open
2 of 3 tasks
EBurtonRod opened this issue Jan 24, 2024 · 2 comments
Open
2 of 3 tasks

BUG: Selecting columns from a DataFrame affects display of warnings #57054

EBurtonRod opened this issue Jan 24, 2024 · 2 comments
Labels
Bug Upstream issue Issue related to pandas dependency Warnings Warnings that appear or should be added to pandas

Comments

@EBurtonRod
Copy link

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 warnings
import pandas as pd

df = pd.DataFrame({"A": [0, 1], "B": [2, 3], "C": [4, 5]})

for _ in range(3):
    # Normal behaviour (warning will display once).
    warnings.warn("First warning.")

for _ in range(3):
    warnings.warn("Second warning.")

    # Select two columns from the DataFrame.
    # This line seems to affect the behaviour of the second
    # warning - it will display on all 3 iterations of the loop.
    df = df[["A", "B"]]

Issue Description

Selecting columns from a DataFrame appears to affect the normal behaviour of Python warnings.

Where a warning is raised repeatedly by a given line within a loop, the default behaviour is that the warning will only be displayed the first time it is raised. This is what happens in the first loop above.

However, in the second loop, the line which filters the columns of the DataFrame (df = df[["A", "B"]]) seems to affect this behaviour, as this time the warning is displayed on each iteration of the loop, rather than just once. Interestingly, this behaviour occurs regardless of whether this filtering line appears before or after the second warn() statement.

The following is the output produced:

..\repeated_warnings.py:8: UserWarning: First warning.
  warnings.warn("First warning.")
..\repeated_warnings.py:11: UserWarning: Second warning.
  warnings.warn("Second warning.")
..\repeated_warnings.py:11: UserWarning: Second warning.
  warnings.warn("Second warning.")
..\repeated_warnings.py:11: UserWarning: Second warning.
  warnings.warn("Second warning.")

I've experimented with putting warnings.resetwarnings() at the start of the script and then examining the contents of the warnings.filters list immediately prior to the two warn() statements - I thought perhaps Pandas might be adding filters to the filters list. However, in each case the filters list seems to be empty, so that doesn't seem to be the problem.

This may be in some way related to Issue #55801, although that issue relates to creation of a DataFrame, rather than selecting columns from it.

Expected Behavior

I would expect each warning to display only once:

..\repeated_warnings.py:8: UserWarning: First warning.
  warnings.warn("First warning.")
..\repeated_warnings.py:11: UserWarning: Second warning.
  warnings.warn("Second warning.")

Installed Versions

INSTALLED VERSIONS

commit : f538741
python : 3.12.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 154 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Ireland.1252

pandas : 2.2.0
numpy : 1.26.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 69.0.3
pip : 23.3.2
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
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None

@EBurtonRod EBurtonRod added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 24, 2024
@rhshadrach
Copy link
Member

Thanks for the report. I believe this is due to python/cpython#73858. The example hits this line:

with warnings.catch_warnings():

As commented there, that catch_warnings can be removed once NumPy 1.24 is the minimum version. However this will continue to be a general issue until the Python bug is fixed.

@rhshadrach
Copy link
Member

If we're waiting for a fix to come from CPython, then this is a duplicate of #55801, but if we're wanting to do something in pandas, it's not. Leaving this open for the time being.

@rhshadrach rhshadrach added Warnings Warnings that appear or should be added to pandas Upstream issue Issue related to pandas dependency and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Upstream issue Issue related to pandas dependency Warnings Warnings that appear or should be added to pandas
Projects
None yet
Development

No branches or pull requests

2 participants