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?
Asked
Active
Viewed 744 times
1 Answers
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