Farouk Fadly
Farouk Fadly

Reputation: 3

Error in imputer.fit when preprocessing data

Python 3.6.7 IPython 7.4.0 Navigator/Spyder

Hi, I'm getting TypeError: '(slice(None, None, None), slice(1, 3, None))' is an invalid key when using this code

#Importing Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#Importing the Dataset

dataset = pd.read_csv("Data.csv")

#Independant & Dependant Variables

X = dataset.iloc[: , :-1]
Y = dataset.iloc[: , 3]

#Missing Data

from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values = np.nan, strategy = "mean", 
                        fill_value=None, verbose=0, copy=True)
imputer = imputer.fit(X[: , 1:3])
X[:, 1:3] = imputer.transform(X[:, 1:3])

The error i get is:

from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values = np.nan, strategy = "mean", 
                        fill_value=None, verbose=0, copy=True)
imputer = imputer.fit(X[: , 1:3])
X[:, 1:3] = imputer.transform(X[:, 1:3])
Traceback (most recent call last):

  File "<ipython-input-4-a1bcee2e6448>", line 4, in <module>
    imputer = imputer.fit(X[: , 1:3])

  File "C:\Users\Farouk\Anaconda3\lib\site-packages\pandas\core\frame.py", 
line 2927, in __getitem__
    indexer = self.columns.get_loc(key)

  File "C:\Users\Farouk\Anaconda3\lib\site- 

packages\pandas\core\indexes\base.py", line 2657, in get_loc return self._engine.get_loc(key)

  File "pandas/_libs/index.pyx", line 108, in 
pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/index.pyx", line 110, in 
pandas._libs.index.IndexEngine.get_loc

TypeError: '(slice(None, None, None), slice(1, 3, None))' is an invalid 
key

Does anyone know why this is happening?

Upvotes: 0

Views: 510

Answers (1)

Quev
Quev

Reputation: 41

SimpleImputer's fit_transform fonction is expecting an narray not a pandas.DataFrame

X = dataset.iloc[: , :-1].values
Y = dataset.iloc[: , 3].values

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html

Upvotes: 1

Related Questions