Install & Compatibility
Where this runs
tested against v2.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
build_error
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 13.7s · import 0.537s · 341MB
353MB installed
● package 353MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
DataFrameMapper
✓ from sklearn_pandas import DataFrameMapper
✗ from sklearn_pandas import DataFrameMapper
CategoricalImputer
✓ from sklearn_pandas import CategoricalImputer
cross_val_score
✓ from sklearn_pandas import cross_val_score
This quickstart demonstrates how to use `DataFrameMapper` to apply different scikit-learn transformers to specific columns of a pandas DataFrame. Categorical 'pet' column is binarized, 'children' is standardized, and 'salary' is kept as is. Setting `df_out=True` (requires pandas >= 1.0) ensures the output is a DataFrame rather than a NumPy array.
import pandas as pd
from sklearn_pandas import DataFrameMapper
from sklearn.preprocessing import LabelBinarizer, StandardScaler
data = pd.DataFrame({
'pet': ['cat', 'dog', 'dog', 'fish', 'cat', 'dog', 'cat', 'fish'],
'children': [4., 6, 3, 3, 2, 3, 5, 4],
'salary': [90., 24, 44, 27, 32, 59, 36, 27]
})
# Map DataFrame columns to Scikit-learn transformations
mapper = DataFrameMapper([
('pet', LabelBinarizer()),
(['children'], StandardScaler()),
('salary', None) # 'None' keeps the column without transformation
], df_out=True) # Set df_out=True to get a DataFrame output (requires pandas >= 1.0)
transformed_data = mapper.fit_transform(data.copy())
print(transformed_data.head())
print(transformed_data.columns)
Debug
Known issues
deprecated`NumericalTransformer` was deprecated in `v2.1.0` and is slated for removal in a future release. Users should migrate to native scikit-learn transformers or implement custom transformers.fixAvoid using `NumericalTransformer`. For common numerical transformations, use `sklearn.preprocessing` modules or custom `FunctionTransformer` instances.
affects: >=2.1.0
breakingFunctionalities like `CategoricalImputer`, `cross_val_score`, and `GridSearchCV` were removed in `sklearn-pandas v2.0.0`. Their equivalent features are now available directly within `scikit-learn`.fixReplace `CategoricalImputer` with `sklearn.impute.SimpleImputer(strategy='most_frequent')`. For cross-validation and grid search, use `sklearn.model_selection.cross_val_score` and `GridSearchCV` directly, as they now support pandas DataFrames.
affects: >=2.0.0
gotchaBy default, `DataFrameMapper.transform()` outputs a NumPy array, not a pandas DataFrame. This can lead to loss of column names and type information.fixTo receive a pandas DataFrame as output (if using pandas >= 1.0), initialize `DataFrameMapper` with `df_out=True`. Otherwise, manually convert the output NumPy array back to a DataFrame and re-add column names if needed.
affects: <2.2.0 (and default in >=2.2.0)
gotchaThe way a column is specified in `DataFrameMapper` (e.g., `'column_name'` vs. `['column_name']`) affects the shape of the array passed to the transformer (1D array vs. 2D array/column vector). Some scikit-learn transformers expect a 2D input.fixFor transformers expecting 2D input (e.g., `StandardScaler`), always pass column names as a list: `(['column_name'], Transformer())`. For transformers that can handle 1D input (e.g., `LabelBinarizer`), a string is often sufficient, but using a list ensures 2D input.
affects: All versions
gotchaWhile `sklearn-pandas` bridges DataFrame functionality, recent versions of `scikit-learn` (v1.2+) introduced native `set_config(transform_output="pandas")` for transformers. This may reduce the need for `sklearn-pandas` in certain `sklearn.pipeline` contexts, but `DataFrameMapper` still offers granular column-wise transformation definition.fixConsider `scikit-learn`'s native DataFrame output features and `ColumnTransformer` alongside `sklearn-pandas` for new projects or when refactoring, especially if a simpler pipeline without complex column selection logic is sufficient.
affects: scikit-learn >= 1.2
Upgrade
Version history
2.2.0latest on PyPI · released May 8, 2021
Audit
Dependencies
numpyrequiredFundamental numerical computing library for array operations.
pandasrequiredCore data structure for DataFrames used in mapping transformations.
scipyrequiredScientific computing library, often a dependency of scikit-learn.
scikit-learnrequiredMachine learning library providing the transformers and estimators.