Skip to content

Pandas breaks warnings module when printing DataFrame #31978

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

Closed
fisheye36 opened this issue Feb 14, 2020 · 5 comments
Closed

Pandas breaks warnings module when printing DataFrame #31978

fisheye36 opened this issue Feb 14, 2020 · 5 comments
Labels
Bug Regression Functionality that used to work in a prior pandas version Warnings Warnings that appear or should be added to pandas

Comments

@fisheye36
Copy link

>>> import warnings
>>> warnings.warn('asd')
<input>:1: UserWarning: asd
>>> warnings.warn('asd')
>>> warnings.warn('asd')
>>> import pandas as pd
>>> df = pd.DataFrame([1])
>>> warnings.warn('asd')
<input>:1: UserWarning: asd
>>> warnings.warn('asd')
>>> df
   0
0  1
>>> warnings.warn('asd')
<input>:1: UserWarning: asd
>>> warnings.warn('asd')
<input>:1: UserWarning: asd
>>> warnings.warn('asd')
<input>:1: UserWarning: asd
>>> warnings.warn('asd')
<input>:1: UserWarning: asd

Problem description

As you can see from the interactive Python listed above, when DataFrame is printed, suddenly all calls to warnings.warn() are being reported, instead of being suppressed when identical warning is issued.

The problem does not occur on Pandas version 0.25.3.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1250
pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.2.7
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : 1.2.7
numba : None

@fisheye36 fisheye36 changed the title Pandas breaks warnings module when printing DataFrame Pandas breaks warnings module when printing DataFrame Feb 14, 2020
@MarcoGorelli MarcoGorelli added the Regression Functionality that used to work in a prior pandas version label Feb 14, 2020
@jorisvandenbossche
Copy link
Member

Can you show the result of pd.show_versions() for both cases (for pandas 1.0 and pandas 0.25.3)? To check that there are no other differences between both environments

@jorisvandenbossche
Copy link
Member

BTW, I can't reproduce this with pandas master on Python 3.7, and also not with pandas 1.0.0 on Python 3.8

@fisheye36
Copy link
Author

@jorisvandenbossche, here is the same output for 0.25.3:

>>> import warnings
>>> import pandas as pd
>>> warnings.warn('asd')
<input>:1: UserWarning: asd
>>> warnings.warn('asd')
>>> warnings.warn('asd')
>>> df = pd.DataFrame([1])
>>> warnings.warn('asd')
>>> warnings.warn('asd')
>>> warnings.warn('asd')
>>> df
   0
0  1
>>> warnings.warn('asd')
>>> warnings.warn('asd')
>>> warnings.warn('asd')

Output of pd.show_versions() for 0.25.3:

INSTALLED VERSIONS

commit : None
python : 3.8.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1250

pandas : 0.25.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.1.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
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

@fisheye36
Copy link
Author

A little update.

First of all, all the tests were being done using PyCharm's Python Console. When I wrote a script file and then run it, the behavior is a bit different. Here is the script:

import sys
import warnings
from time import sleep

import pandas as pd


def do_warn():
    sleep(1)
    warnings.warn('asd')
    sys.stdout.flush()


print('Printing warning 3 times', flush=True)
do_warn()
do_warn()
do_warn()

print('Creating DataFrame and then printing warning 3 times', flush=True)
df = pd.DataFrame([1])
do_warn()
do_warn()
do_warn()

print('Printing DataFrame and then printing warning 3 times', flush=True)
print(df)
do_warn()
do_warn()
do_warn()

print('Printing DataFrame AGAIN and then printing warning 3 times', flush=True)
print(df)
do_warn()
do_warn()
do_warn()

print('Printing DataFrame AGAIN and then printing warning 3 times', flush=True)
print(df)
do_warn()
do_warn()
do_warn()

I use sleep() to align stdout and stderr, otherwise stderr was being printed after all stdout messages (which is strange, I thought flush=True should fix that but that is not the point of this issue). Here is the output:

C:\Users\fisheye\Documents\Projects\pandastest\venv\Scripts\python.exe C:/Users/fisheye/Documents/Projects/pandastest/src/test.py
Printing warning 3 times
C:/Users/fisheye/Documents/Projects/pandastest/src/test.py:10: UserWarning: asd
  warnings.warn('asd')
Creating DataFrame and then printing warning 3 times
Printing DataFrame and then printing warning 3 times
   0
0  1
C:/Users/fisheye/Documents/Projects/pandastest/src/test.py:10: UserWarning: asd
  warnings.warn('asd')
Printing DataFrame AGAIN and then printing warning 3 times
   0
0  1
C:/Users/fisheye/Documents/Projects/pandastest/src/test.py:10: UserWarning: asd
  warnings.warn('asd')
Printing DataFrame AGAIN and then printing warning 3 times
   0
0  1
C:/Users/fisheye/Documents/Projects/pandastest/src/test.py:10: UserWarning: asd
  warnings.warn('asd')

Process finished with exit code 0

It seems like printing DataFrame somehow resets warnings altogether. In case of PyCharm's Python Console, after just one print it keeps resetting warnings every time, maybe due some internal printing of that DataFrame somewhere in the background (a list of variables maybe?).

@mroeschke mroeschke added Bug Warnings Warnings that appear or should be added to pandas labels May 11, 2020
@mroeschke
Copy link
Member

Seems like this may be a PyCharm issue potentially since it's not reproducible with other set ups so closing. Can reopen if it appears again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Regression Functionality that used to work in a prior pandas version Warnings Warnings that appear or should be added to pandas
Projects
None yet
Development

No branches or pull requests

4 participants