10

What are all the exceptions that can be thrown by pd.read_csv()?

In the example below I am capturing some exception types explicitly and using a generic Exception to catch the others, but what are the others exactly?

Reviewing the documentation for pandas read_csv() I can't see a complete list of exceptions thrown.

In a more general case, what is the recommended practice to determine all of the types of exceptions that can be thrown by any call/library?

import pandas as pd

try:
    df = pd.read_csv("myfile.csv")
except FileNotFoundError:
    print("File not found.")
except pd.errors.EmptyDataError:
    print("No data")
except pd.errors.ParserError:
    print("Parse error")
except Exception:
    print("Some other exception")
MattG
  • 4,495
  • 4
  • 26
  • 44
  • 2
    I think it's important to ask yourself why you want to do this and whether it adds value to whatever you're doing. – cs95 Oct 11 '20 at 09:28
  • You can look here: https://github.com/pandas-dev/pandas/blob/master/pandas/io/parsers.py#L549-L717 , but I'm not sure why you would want to to do this. – David Erickson Oct 11 '20 at 09:29
  • 1
    I see some highly ranked answers for other questions such as this one which recommend against capturing generic exceptions: https://stackoverflow.com/a/9824050/833960. They seem to imply that I should try to determine what exceptions could be thrown, I guess that is what I am asking. – MattG Oct 11 '20 at 09:44
  • My use case – a pipeline, where you can import CSV file, select columns, modify values, and create visualisations, export it to another format like xlsx. Let's say a user will send an excel file but with `.csv` extension. I would like to check if this is an eligible file to process considering content, encodings, separators, etc. Without the knowledge about the error, I would be forced to show a user very generic message. – pdaawr Jun 01 '21 at 10:14
  • @DavidErickson you should use `Copy permalink` when linking to files from VCS repositories. The link is dead now. Here is the current link that will show the `to_csv` version at the time of posting unless the repo is cleaned: https://github.com/pandas-dev/pandas/blob/00af20a22c6c64ad2d8f48345beaede0e946d630/pandas/io/formats/format.py#L1056 – int_ua Jun 21 '21 at 15:36

3 Answers3

1

You can see all the exceptions in the following file:

Python > 3.8 > lib > python > site-packages > pandas > errors > __init__.py

BTW, the exceptions are:

  • IntCastingNaNError
  • NullFrequencyError
  • PerformanceWarning
  • UnsupportedFunctionCall
  • ParserError
  • DtypeWarning
  • EmptyDataError
  • ParserWarning
  • MergeError
  • AccessorRegistrationWarning
  • AbstractMethodError
yesidays
  • 41
  • 2
0

A complete list and explanation can be found in the pandas source code

Desi Pilla
  • 464
  • 6
  • 12
-7

This is a way to catch all exceptions:

import sys

try:
    int("test") # creates a ValueError
except BaseException as e:
    print('The exception: {}'.format(e))

If you really want to find out the possible exceptions of read_csv you can look at the source code

Rick
  • 187
  • 2
  • 8