>>> 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

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:

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

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