string#
- skrub.selectors.string()[source]#
Select columns that have a string data type.
In pandas, object columns containing strings are also selected.
See also
categoricalSelect categorical columns (explicit categories, not arbitrary strings).
objectSelect object dtype columns (broader, may include mixed types).
filterUse for custom text-based selection criteria.
Notes
Warning
The behavior of string columns may change depending on the pandas version:
Before pandas 3.0: String columns may have the ‘object’ dtype
From pandas 3.0 onwards: String columns have only the ‘string’ dtype
This selector handles both cases, selecting string columns regardless of pandas version. Object columns containing mixed types (e.g., strings and numbers) are not selected.
Examples
>>> from skrub import selectors as s >>> import pandas as pd >>> df = pd.DataFrame( ... dict( ... object_string=pd.Series(['A', 'B']), ... object=pd.Series(['A', 10]), ... string=pd.Series(['A', 'B']).convert_dtypes(), ... categorical=pd.Series(['A', 'B'], dtype="category"), ... ) ... ) >>> df object_string object string categorical 0 A A A A 1 B 10 B B
Select all string columns (note: mixed-type object columns are excluded):
>>> s.select(df, s.string()) object_string string 0 A A 1 B B
Combine with categorical() to select all text-like columns:
>>> s.select(df, s.string() | s.categorical()) object_string string categorical 0 A A A 1 B B B