Beispiel #1
0
    def transform(self, X, **transform_args):
        check_is_fitted(self, 'features_to_transform_')

        if isinstance(X, pd.DataFrame):
            X = X.copy()
            X.loc[:, self.features_to_transform_] = self.trans(
                X.loc[:, self.features_to_transform_])
            return X
        elif np.ndim(X) == 1:
            return self.trans(X) if self.features_to_transform_ else X
        elif isinstance(X, np.ndarray):
            if self.features_to_transform_.any():
                X = X.copy()
                X = X.astype('float')
                mask = np.tile(self.features_to_transform_, (X.shape[0], 1))
                np.putmask(X, mask,
                           self.trans(X[:, self.features_to_transform_]))
            return X
        elif not self.features_to_transform_:
            # if we wouldn't otherwise have known what to do, we can pass
            # through X if transformation was not necessary anyways
            return X
        else:
            raise TypeError(
                "Couldn't apply transformer on features in {}.".format(
                    get_arr_desc(X)))
Beispiel #2
0
    def transform(self, X, **transform_kwargs):
        msg = "Couldn't convert object {} to named 1d DataFrame."
        if isinstance(X, pd.Index):
            return X.to_series().to_frame(name=self.name)
        elif isinstance(X, pd.Series):
            return X.to_frame(name=self.name)
        elif isinstance(X, pd.DataFrame):
            if X.shape[1] == 1:
                X = X.copy()
                X.columns = [self.name]
                return X
            else:
                raise ValueError(msg.format(get_arr_desc(X)))
        elif isinstance(X, np.ndarray):
            if X.ndim == 1:
                return pd.DataFrame(data=X.reshape(-1, 1), columns=[self.name])
            elif X.ndim == 2 and X.shape[1] == 1:
                return pd.DataFrame(data=X, columns=[self.name])
            else:
                raise ValueError(msg.format(get_arr_desc(X)))

        raise TypeError(msg.format(get_arr_desc(X)))
Beispiel #3
0
def test_get_arr_desc_object():
    obj = object()
    expected = 'object <no shape>'
    actual = get_arr_desc(obj)
    assert actual == expected
Beispiel #4
0
def test_get_arr_desc_frame():
    df = pd.DataFrame()
    expected = 'DataFrame (0, 0)'
    actual = get_arr_desc(df)
    assert actual == expected
Beispiel #5
0
def test_get_arr_desc_array():
    a = np.ones((2, 2))
    expected = 'ndarray (2, 2)'
    actual = get_arr_desc(a)
    assert actual == expected