1

Python often throws warning messages, especially when working with pandas. But no line reference is shown. Why is that and is there a way to get that info?

Veliko
  • 747
  • 1
  • 9
  • 25
  • Does this answer your question? [How to find the line that is generating a Pandas SettingWithCopyWarning?](https://stackoverflow.com/questions/30392099/how-to-find-the-line-that-is-generating-a-pandas-settingwithcopywarning) – Nelly Mincheva Jul 31 '20 at 08:14

1 Answers1

0

For a more general solution that is not pandas specific you can override the formatwarning method:

The printing of warning messages is done by calling showwarning(), which may be overridden; the default implementation of this function formats the message by calling formatwarning(), which is also available for use by custom implementations.

So it can look something like the following:

import warnings

def my_formatwarning(message, category, filename, lineno, line=None):
  print(message, category)
  # lineno is the line number you are looking for
  print('file:', filename, 'line number:', lineno) 
  ...

warnings.formatwarning = my_formatwarning

For the why part of the question I am not really sure what the answer is, I suppose so that the log is kept cleaner?

Nelly Mincheva
  • 380
  • 3
  • 8