2

Relatively new to Python, having challenges handling a Pandas exception in PyCharm Professional. I'm trying to read in a CSV using Pandas:

import pandas as pd
df = pd.read_csv('file.csv', index_col=False)

and I get this message:

pydev_umd:1: DtypeWarning: Columns (56) have mixed types. Specify dtype option on import of set low_memory = False

I know why the warning arises. The CSV is an output from another program that will, very rarely, omit columns from the interior of CSV's last row, causing subsequent columns to shift and screw up the dtypes. I can't change the other program's behavior, so my thought was to add error-handling in Python when the DtypeWarning occurs.

I don't have much experience with exceptions, but after seeing a similar question here, I tried:

try:
    df = pd.read_csv('file.csv', index_col=False)
except pd.errors.DtypeWarning:
    print("Handling Dtype warning!")

It doesn't seem to catch the warning; I still get the same df and the same message:

pydev_umd:1: DtypeWarning: Columns (56) have mixed types. Specify dtype option on import of set low_memory = False

I'm at a loss. I'd appreciate any suggestions what to try next.

Nick
  • 21
  • 1
  • Does this answer your question? [Pandas read\_csv low\_memory and dtype options](https://stackoverflow.com/questions/24251219/pandas-read-csv-low-memory-and-dtype-options) – Mr_and_Mrs_D Dec 09 '20 at 16:44

1 Answers1

1

I was encountered the same problem. As you can see in the source code of pandas.errors.DtypeWarning, it is inherited from Warning class. In python, warnings will not be catched. I checked this blog post and found that we can use

import pandas as pd
import warnings
warnings.simplefilter('error', pd.errors.DtypeWarning)

to treat DtypeWarning as exception. Then, try ... except ... can work.