Report which pandas text columns save memory when converted to category
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write `category_memory_savings(df: pd.DataFrame) -> pd.DataFrame`. For each object or string column, it compares the column's current deep memory with its deep memory after `astype('category')`, both measured with `Series.memory_usage(index=False, deep=True)`. Report only the columns that save memory.
The output must meet these requirements:
- Consider only object and string columns (exclude numeric ones). Keep a column only when `current_bytes - category_bytes > 0`.
- The columns are exactly `['column', 'nunique', 'unique_ratio', 'current_bytes', 'category_bytes', 'pct_saved']`. `column` is `object`; `nunique`, `current_bytes` and `category_bytes` are `int64`.
- `unique_ratio` is `nunique / len(df)` (`float64`, 3 decimal places). `pct_saved` is `100 * (current_bytes - category_bytes) / current_bytes` (`float64`, 2 decimal places).
- Sort by `pct_saved` descending, then `column` ascending, then `reset_index(drop=True)`.
- If no column qualifies, return an empty frame with the same columns.
```hint Where the savings come from
Compare what a categorical stores once per distinct value with what it stores once per row. Then ask what happens to a column where almost every value is distinct.
```
```hint The empty case
Filtering can remove every row. Make sure the frame you return still has all six columns, in order, with the right dtypes.
```
### Constraints & Assumptions
- Text columns are those with `object` dtype or the pandas `string` dtype. Numeric columns are never candidates.
- The function reports a what-if comparison. It must not modify `df` or return a converted frame.
### What a Strong Answer Covers
- Selects only object and string columns and measures both versions with the same deep `memory_usage` call.
- Keeps a column only when its categorical copy is strictly smaller.
- Computes `unique_ratio` and `pct_saved` with the right denominators and rounding, and casts all six columns to the required dtypes.
- Sorts by `pct_saved` descending and `column` ascending, resets the index, and returns an empty frame with the six columns when nothing qualifies.
- Explains why category encoding shrinks low-cardinality text and can grow a column that is nearly unique.
### Follow-up Questions
- Why does a high-cardinality column often use more memory as `category`?
- How would you scale this audit when the frame itself won't fit in RAM?
- Does `memory_usage(deep=True)` always reflect the RAM an object column really occupies?
- Apart from memory, what behaves differently once a column is `category`?
Overview: Write a pandas function that measures the deep memory of each text column before and after converting it to the category dtype and reports only the columns that shrink, ranked by percent saved. It tests how category encoding depends on cardinality and how to return an exact schema with a deterministic sort order.