Detect Mixed-Type Object Columns in a pandas DataFrame
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write `mixed_type_report(df)` that returns a DataFrame with exactly one row per `object`-dtype column of `df`, flagging the columns that hold values of more than one type. Columns of any other dtype are ignored.
The report must follow these rules:
- The columns are, in order, `['column', 'inferred_dtype', 'n_python_types', 'is_mixed']`, with dtypes `object`, `object`, `int64` and `bool`.
- `inferred_dtype` is `pandas.api.types.infer_dtype(df[col], skipna=True)`.
- `n_python_types` is the number of distinct `type(v).__name__` values among the non-null values of the column.
- `is_mixed` is True when `inferred_dtype` starts with `'mixed'`.
- Rows are sorted by `is_mixed` descending, then `column` ascending, followed by `reset_index(drop=True)`.
- Nothing may be coerced with `to_numeric`.
For example:
```python
df = pd.DataFrame({
"id": [1, 2, 3, 4],
"city": pd.Series(["NYC", None, "LA", "SF"], dtype=object),
"zip": pd.Series([10001, "94105", 60601, None], dtype=object),
"score": pd.Series([1, 2.5, 3, None], dtype=object),
})
```
```text
column inferred_dtype n_python_types is_mixed
0 score mixed-integer-float 2 True
1 zip mixed-integer 2 True
2 city string 1 False
```
`id` is `int64`, so it gets no row. `city` holds only strings once the null is skipped, `zip` mixes integers with a string, and `score` mixes integers with a float.
```hint Labels that mean mixed
Look at the full list of strings `infer_dtype` can return. More than one of them describes a mixed column.
```
```hint Nulls and output dtypes
A null value has a Python type of its own. Also check which dtypes your result frame gets when there are no object columns at all, or when a column of the report holds only plain strings.
```
### Clarifying Questions to Ask
- Which pandas version runs the tests? From pandas 3.0, a column of plain strings defaults to the `str` dtype, so it is no longer `object` and would not appear in the report.
- Are the column labels unique, and are they all strings, so that sorting on `column` is well defined?
- What should the report look like when `df` has no object columns?
### What a Strong Answer Covers
- Selecting only `object`-dtype columns and skipping every other dtype, including categoricals and the pandas string dtype.
- Using `infer_dtype(..., skipna=True)` for the label and counting distinct type names over non-null values only.
- Flagging `'mixed-integer'` and `'mixed-integer-float'` as well as the bare `'mixed'`.
- Returning the exact column order, dtypes and sort order, including when there are no object columns.
- Explaining when the type count and the `infer_dtype` flag can disagree.
### Follow-up Questions
- How would you extend the report to suggest a safe target dtype per column?
- How would this scale on a very wide frame with millions of rows?
- When can `n_python_types` be above 1 while `is_mixed` is False, or the reverse, and which signal would you act on?
Overview: Write a pandas function that reports, for every object-dtype column, the inferred dtype, the number of distinct Python value types and a mixed-type flag. It tests pandas type inference, null handling and producing an exact output schema with the right dtypes and sort order.