By tim654321

2015-05-22 08:40:30 8 Comments

I have a large block of code that is, at some point somewhere, generating a setting with copy warning in pandas (this problem).

I know how to fix the problem, but I can't find what line number it is! Is there a way to back out the line number (apart from brute force methods like debug-stepping or putting in multiple prints)? The only output I get is the below, which doesn't go up the stack to my code:

C:\Anaconda3\lib\site-packages\pandas\core\ SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame  **kwargs)


@Julius 2018-01-18 17:23:30

Set pd.options.mode.chained_assignment = 'raise'

This will throw an exception pointing to the line which triggers SettingWithCopyError.

UPDATE: how to catch the error, and interrogate the stacktrace to get the actual offending lineno:

import pandas as pd
from inspect import currentframe, getframeinfo
from pandas.core.common import SettingWithCopyError

pd.options.mode.chained_assignment = 'raise'

df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})

df2 = df[df['a'] == 2]

    df2['b'] = 'foo'
except SettingWithCopyError:
    frameinfo = getframeinfo(currentframe())

@PiMathCLanguage 2018-10-03 08:33:46

Can the error be raised but the code runs further? I mean only to show the number of lines of code, where the warning(s) is/are happening.

@Julius 2018-10-03 18:16:13

Yes, you just need to catch the error. As for the line number you can either add it manually using logging/print statements, or extract it using inspect module. Please see the update in the answer.

@Jondiedoop 2019-04-29 12:52:22

If you want to change it back after finding the line, you could use pd.options.mode.chained_assignment = 'warn'

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] Adding new column to existing DataFrame in Python pandas

5 Answered Questions

[SOLVED] Catch multiple exceptions in one line (except block)

39 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

21 Answered Questions

[SOLVED] How to iterate over rows in a DataFrame in Pandas?

26 Answered Questions

[SOLVED] Renaming columns in pandas

16 Answered Questions

[SOLVED] "Large data" work flows using pandas

44 Answered Questions

[SOLVED] How do I merge two dictionaries in a single expression in Python?

15 Answered Questions

[SOLVED] How to deal with SettingWithCopyWarning in Pandas?

30 Answered Questions

[SOLVED] Finding the index of an item in a list

  • 2008-10-07 01:39:38
  • Eugene M
  • 3866151 View
  • 3162 Score
  • 30 Answer
  • Tags:   python list indexing

28 Answered Questions

[SOLVED] How to read a file line-by-line into a list?

Sponsored Content