From 4e29a602f7db457235846588c0d6fb98b5972ec8 Mon Sep 17 00:00:00 2001 From: ly Date: Mon, 16 Jan 2017 11:36:57 +0800 Subject: [PATCH] Fix index out of bounds issue when fillna --- datacleaner/datacleaner.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/datacleaner/datacleaner.py b/datacleaner/datacleaner.py index 910963f..e19592f 100644 --- a/datacleaner/datacleaner.py +++ b/datacleaner/datacleaner.py @@ -76,7 +76,13 @@ def autoclean(input_dataframe, drop_nans=False, copy=False, encoder=None, try: input_dataframe[column].fillna(input_dataframe[column].median(), inplace=True) except TypeError: - input_dataframe[column].fillna(input_dataframe[column].mode()[0], inplace=True) + most_frequent = input_dataframe[column].mode() + if len(most_frequent)>0: + input_dataframe[column].fillna(input_dataframe[column].mode()[0], inplace=True) + else: + input_dataframe[column].fillna(method='bfill', inplace=True) + input_dataframe[column].fillna(method='ffill', inplace=True) + # Encode all strings with numerical equivalents if str(input_dataframe[column].values.dtype) == 'object':