Dataset Overview Panel Series: Column Types, Missingness, Duplicates and Memory in Pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement `def dataset_overview(df: pd.DataFrame) -> pd.Series`, the overview panel that an automated profiling report shows first: shape, column types, missingness, duplicates and memory. It returns a `float64` Series with `name=None` whose index is exactly these labels in this order:
```python
['n_rows', 'n_columns', 'n_numeric', 'n_categorical', 'n_datetime',
'missing_cells', 'missing_cells_pct', 'duplicate_rows', 'duplicate_rows_pct',
'memory_bytes']
```
The fields are defined as follows:
- `n_numeric` counts number-dtype columns, excluding `bool`. `n_categorical` counts `object`, `string` and `category` columns. `n_datetime` counts datetime-dtype columns.
- `missing_cells` is the total number of nulls, and `missing_cells_pct` = `100 * missing_cells / (n_rows * n_columns)`.
- `duplicate_rows` is the count of `df.duplicated()` with `keep='first'`, and `duplicate_rows_pct` = `100 * duplicate_rows / n_rows`.
- `memory_bytes` = `df.memory_usage(index=True, deep=True).sum()`.
```hint Selectors and predicates
pandas can classify columns with `select_dtypes` or with the `pd.api.types.is_*_dtype` functions. Check what each one does with `bool` and `timedelta64` columns before you count numeric ones.
```
```hint Percentages
Round only the two percentage fields, and decide what they should be when the frame has no rows.
```
### Constraints & Assumptions
- Both `_pct` fields are rounded to 2 decimal places. All values are coerced to float.
- The Series has `float64` dtype, `name=None` and the labels in the order above.
- The function reports on `df` and does not modify or clean it.
- The grader uses a fixed 12-row frame with mixed dtypes, scattered nulls and exactly 2 duplicate rows.
### Clarifying Questions to Ask
- Should `timedelta64` columns count toward `n_numeric`?
- Do timezone-aware datetime columns count toward `n_datetime`?
- What should the two percentages be for a frame with zero rows or zero columns?
### What a Strong Answer Covers
- A dtype classification that keeps `bool` out of the numeric count and says where edge dtypes such as timedelta and timezone-aware datetimes go.
- Total nulls over all cells and duplicates counted with `keep='first'`, each divided by the right denominator.
- Rounding only the two percentage fields, with a guard for an empty frame.
- Memory measured with `deep=True` and the index included.
- The exact label order, `float64` dtype and `name=None`.
### Follow-up Questions
- How would you make the column classification hold up for nullable and extension dtypes (for example `Int64` and `boolean`)?
- Why does `deep=True` matter for `memory_bytes`?
- Does `df.duplicated()` treat two rows with nulls in the same columns as duplicates, and what goes wrong if an object column holds lists?
Overview: Implement a pandas function that returns a ten-entry float64 overview of a DataFrame: shape, numeric, categorical and datetime column counts, missing cells, duplicate rows and deep memory usage. It tests dtype selection rules, duplicate detection, rounding and matching an exact output contract.