>>> 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()
Comment From: jorisvandenbossche
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
Comment From: jorisvandenbossche
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
Comment From: fisheye36
@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
:
Comment From: fisheye36
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?).
Comment From: mroeschke
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