FILE: docs/api/pandas.md¶
Pandas API Reference¶
Functions for cleaning, encoding, and profiling DataFrames.
auto_impute(df, strategy='smart', threshold=0.4, inplace=False, verbose=True)¶
Automatically handle missing values.
Parameters:
- df – pandas DataFrame
- strategy – 'smart' or dict mapping column names to strategies
- threshold – columns with null% > this are dropped (default 0.4)
- inplace – modify original DataFrame if True
- verbose – print report if True
Returns: Cleaned DataFrame (or None if inplace=True)
Example:
df_clean = dk.auto_impute(df)
df_clean = dk.auto_impute(df, strategy={'price': 'mean', 'notes': 'drop'})
fix_dtypes(df, schema=None, verbose=True)¶
Fix common dtype issues: dates, currency strings, booleans, categories.
Parameters:
- df – pandas DataFrame
- schema – optional dict mapping column names to target dtypes
- verbose – print changes if True
Returns: DataFrame with corrected dtypes
Example:
df = dk.fix_dtypes(df)
df = dk.fix_dtypes(df, schema={'date_col': 'datetime', 'price': 'float'})
clean_columns(df, style='snake_case')¶
Normalize column names to consistent format.
Parameters:
- df – pandas DataFrame
- style – 'snake_case', 'camelCase', or 'PascalCase'
Returns: DataFrame with cleaned column names
Example:
df = dk.clean_columns(df)
df = dk.clean_columns(df, style='camelCase')
dedup(df, subset=None, keep='last', reset_index=True, verbose=True)¶
Remove duplicate rows.
Parameters:
- df – pandas DataFrame
- subset – columns to consider (None = all columns)
- keep – 'first', 'last', or False (drop all)
- reset_index – reset index after deduplication
- verbose – print summary
Returns: DataFrame with duplicates removed
Example:
df = dk.dedup(df)
df = dk.dedup(df, subset=['user_id', 'date'], keep='first')
encode_categoricals(df, columns=None, method='auto', ordinal_map=None, drop_first=True, verbose=True)¶
Encode categorical columns.
Parameters:
- df – pandas DataFrame
- columns – list of columns to encode (None = all object/category)
- method – 'auto', 'onehot', 'label', 'ordinal'
- ordinal_map – dict for ordinal encoding: {'col': ['low','med','high']}
- drop_first – drop first dummy column (one-hot only)
- verbose – print report
Returns: DataFrame with encoded columns
Example:
df = dk.encode_categoricals(df)
df = dk.encode_categoricals(df, method='onehot', drop_first=True)
df = dk.encode_categoricals(df, method='ordinal', ordinal_map={'size': ['S','M','L']})
profile_df(df, include_samples=True, top_n=5, export_html=False, output_path='profile.html')¶
Generate a complete dataset profile.
Parameters:
- df – pandas DataFrame
- include_samples – show first 5 rows
- top_n – number of top values for categorical columns
- export_html – save HTML report
- output_path – path for HTML export
Returns: Dict with keys 'overview', 'columns', 'samples'
Example:
profile = dk.profile_df(df)
dk.profile_df(df, export_html=True, output_path='report.html')