Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silencing warnings in Pandas

When running:

mydf = pd.read_csv(p_file, sep=',', error_bad_lines=False, index_col=False)

I get many lines like the following:

...
Skipping line 77432: expected 15 fields, saw 16
Skipping line 77497: expected 15 fields, saw 16
Skipping line 77528: expected 15 fields, saw 16
Skipping line 77560: expected 15 fields, saw 16
Skipping line 77625: expected 15 fields, saw 16
Skipping line 77656: expected 15 fields, saw 16
...

How can I silence these warnings? How can I find the list of warning classes in Pandas?

like image 756
Amelio Vazquez-Reina Avatar asked Feb 18 '14 22:02

Amelio Vazquez-Reina


People also ask

How do I suppress a warning in Python?

import warnings warnings. filterwarnings('ignore') print('The script still runs. ') To suppress any warnings in the script, import the warnings library, and pass the filterwarnings() function the argument ignore .

How do I stop pandas warnings SettingWithCopyWarning?

Generally, to avoid a SettingWithCopyWarning in Pandas, you should do the following: Avoid chained assignments that combine two or more indexing operations like df["z"][mask] = 0 and df. loc[mask]["z"] = 0 . Apply single assignments with just one indexing operation like df.

Can you ignore SettingWithCopyWarning?

SettingWithCopyWarning is a warning which means that your code may still be functional. However, it's important not to ignore it but instead, understand why it has been raised in the first place. This way it will be much easier for you to adjust your code accordingly so that the warning is no longer raised.


1 Answers

Looks like read_csv has a warn_bad_lines kwarg, so you should be able to do the following:

mydf = pd.read_csv(p_file, sep=',', error_bad_lines=False, index_col=False, warn_bad_lines=False)

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html#pandas.read_csv

"If error_bad_lines is False, and warn_bad_lines is True, a warning for each “bad line” will > be output. (Only valid with C parser)."

like image 73
Bij Avatar answered Oct 05 '22 19:10

Bij