0

When I read_csv the following df, it raises the warning:

              TIME                       col_A     col_B    
0        yyyy-MM-dd"T"HH:mm:ss.fffzzz      t         m         
1       2022-12-10T00:00:00.000-06:00  0.000     0.000
2       2022-12-10T00:00:05.000-06:00  0.000     0.000
> DtypeWarning: Columns (1,2,3) have mixed types. Specify dtype option
> on import or set low_memory=False. csv_file =
> pd.read_csv(self.surf_path + '/' + file)

I understand the warning, when I have these sorts of csv files (with units on the zeroth index) I have to manually drop the index 0 in order to be able to process further. I would like to write a function, which catches this warning and drops the zeroth index if necessary. See example of code I have in mind:

def read_file():
  for file in os.listdir(file_path):
    if file.endswith('.csv'):
      try:
        csv_file = pd.read_csv(self.surf_path + '/' + file)
      except DtypeWarning:
        csv_file = pd.read_csv(self.surf_path + '/' + file)
        csv_file.drop(index=csv_file.index[0], axis=0, inplace=True)
        csv_file.reset_index(drop=True, inplace=True)
  return csv_file

I know in python I can catch only exception in this way, not the warnings, but I couldn't fiigure out how to catch the warning for my case from other posts.

serdar_bay
  • 271
  • 1
  • 7
  • Duplicate of [*Catch Pandas DtypeWarning in Pycharm*](https://stackoverflow.com/q/62141996/10824407). – Olvin Roght Dec 15 '22 at 12:51
  • I saw that post. There is only one answer there, it didn't work for me. Also, I read the blog suggested but it didn't show how to incorporate this warning handling as an exception. – serdar_bay Dec 15 '22 at 13:00
  • Maybe you don't need to capture this warning but check first column `dtype`? `if csv_file.dtypes[0].kind == "O"` – Olvin Roght Dec 15 '22 at 13:11
  • Interesting alternative, I will try that. Thanks – serdar_bay Dec 15 '22 at 13:26
  • Unfortunately, all of the dtypes are object – serdar_bay Dec 15 '22 at 13:32
  • Check: 1) [*Find mixed types in Pandas columns*](https://stackoverflow.com/q/29376026/10824407); 2) [*Is there an efficient method of checking whether a column has mixed dtypes?*](https://stackoverflow.com/q/53746004/10824407); 3) [*Pandas: how to identify columns with dtype object but mixed-type items?*](https://stackoverflow.com/q/47326350/10824407); 4) [*Pandas: Coloumn with mixed datatype; how to find the exceptions*](https://stackoverflow.com/q/47660384/10824407). – Olvin Roght Dec 15 '22 at 16:08

0 Answers0