The
KeyError
is because you are using the
boolean
value
True
as an index into the
date_col_search
list in line 15. So change your code from line 13 on to the following:
for i in range(date_col_search):
if date_col_search[i] is True:
print(formatted_dates)
The commented line is incorrect; you cannot convert a boolean value to a datetime. You need to be addressing the column value of the current row. So you probably need an outer loop to iterate through all the rows of the series.
[edit]
Try the following code. Note that the parameter passed in to
date_fun
is a pandas
table, not a file path.
def date_fun(table):
lst_to_ser=pd.Series(table.columns.values)
date_col_search= lst_to_ser.str.contains(pat = 'date')
for index, row in table.iterrows():
for i in range(date_col_search.size):
if date_col_search[i] == True:
dt = pd.to_datetime(row.iloc[i], errors='coerce')
print(index, dt)
You can uncomment the print statements to see what happens at each part of the code.
[/edit]