-  
 -   Notifications  
You must be signed in to change notification settings  - Fork 19.2k
 
Description
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