Skip to content

Commit

Permalink
FIX: Make pd import local in DataFrameTransformer (#1017)
Browse files Browse the repository at this point in the history
DataFrameTransformer requires pandas but we don't want to have a
dependency on pandas. Therefore, the pandas import should be local.

Back when I wrote this, I thought it would be sufficient to import
pandas at a class level, but that is incorrect. Now, pandas is important
inside the method bodies.

For most skorch users, this should make no difference. But it will
allow skorch users who do use something from helpers.py to avoid having
to install pandas.
  • Loading branch information
BenjaminBossan authored Sep 7, 2023
1 parent 35cbaa7 commit 64ce295
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions skorch/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,6 @@ class DataFrameTransformer(BaseEstimator, TransformerMixin):
contains 1 column.
"""
import pandas as pd

def __init__(
self,
treat_int_as_categorical=False,
Expand Down Expand Up @@ -399,6 +397,8 @@ def _check_dtypes(self, df):
If a wrong dtype is found.
"""
import pandas as pd

if 'X' in df:
raise ValueError(
"DataFrame contains a column named 'X', which clashes "
Expand All @@ -408,7 +408,7 @@ def _check_dtypes(self, df):
wrong_dtypes = []

for col, dtype in zip(df, df.dtypes):
if isinstance(dtype, self.pd.api.types.CategoricalDtype):
if isinstance(dtype, pd.api.types.CategoricalDtype):
continue
if np.issubdtype(dtype, np.integer):
continue
Expand Down Expand Up @@ -447,6 +447,8 @@ def transform(self, df):
respective column names as keys.
"""
import pandas as pd

self._check_dtypes(df)

X_dict = {}
Expand All @@ -455,7 +457,7 @@ def transform(self, df):
for col, dtype in zip(df, df.dtypes):
X_col = df[col]

if isinstance(dtype, self.pd.api.types.CategoricalDtype):
if isinstance(dtype, pd.api.types.CategoricalDtype):
x = X_col.cat.codes.values
if self.int_dtype is not None:
x = x.astype(self.int_dtype)
Expand Down

0 comments on commit 64ce295

Please sign in to comment.