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.