Saturday, July 16, 2022

[FIXED] How to print the name of document that during the reading to pandas data frame displays the warning

Issue

I have this warning during the reading excel document to data frame:

*

Warning (from warnings module):
  File "C:\Users\Slobo\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\frame.py", line 6211
    sort=sort)
FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.
To accept the future behavior, pass 'sort=False'.
To retain the current behavior and silence the warning, pass 'sort=True'.

*

For reading all documents from a particular folder I use this:

`for f in glob.glob(dirname + "/*.xlsx")`:
    #where dirname is the path to the folder, and then for reading documents:
    pd.read_excel(f)

I want to print the name of the document which causes warning appearance during its reading to the data frame.

How to do that?


Solution

import warnings
warnings.filterwarnings("error")

for f in glob.glob(dirname + "/*.xlsx"):
    try:
       pd.read_excel(f)
    except Warning as w:
        print(f)


Answered By - Omkar Sabade
Answer Checked By - Pedro (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.