Vaiaro
Vaiaro

Reputation: 1008

Python Pandas – How to supress PerformanceWarning?

How I can supress a PerformanceWarning in pandas?

I've already tried warnings.simplefilter(action='ignore', category=PerformanceWarning), but it gives me a NameError: name 'PerformanceWarning' is not defined

Upvotes: 31

Views: 26457

Answers (3)

The other other Alan
The other other Alan

Reputation: 1908

Although @FiloCara 's answer is reliable, it did not work for me until I put the warning-generating code in a context:

with warnings.catch_warnings():
    warnings.simplefilter("ignore", category=pd.errors.PerformanceWarning)
    # code that generates warnings ...

Upvotes: 0

Bryce Chamberlain
Bryce Chamberlain

Reputation: 535

I found the underlying performance issue actually was a problem on large data. I fixed it by creating my loop columns first and then adding them all at once.

Here is some pseudo code that would have helped me:

df = ..your DataFrame..

# build the columns you need to append. 
newdata = {}
for newcol in newcols:
    newdata[newcol] = ..calculate newcol..

# append them all at once with a clean index:
df = pd.concat([df.reset_index(drop = True), pd.DataFrame(newdata)], axis = 1)

Upvotes: 2

FiloCara
FiloCara

Reputation: 806

PerformanceWarning is not a built-in warning class so you can't call it directly in category argument. You can try the following code:

import pandas as pd
import warnings

warnings.simplefilter(action='ignore', category=pd.errors.PerformanceWarning)

I have no idea how to reproduce the PerformanceWarning but i tested a similar approach to the "SettingWithCopyWarning" pandas warning and it worked. Let me know if it works.

Upvotes: 63

Related Questions