df.columns is a list where pandas gives an Index
- Dominant language
- Mojo
- Stars
- 1
- Forks
- 0
- PR merge metrics
- PR metrics pending
Description
Measured against pandas 3.0.5.
```python
type(pd.DataFrame({"x": [1]}).columns) #
type(fp.DataFrame({"x": [1]}).columns) #
```
The values agree and the type does not. `list(df.columns)` and `for name in df.columns` and `df.columns[0]` and `"x" in df.columns` all read the same on both, which is why this has not bitten yet, and the five conformance cases it fails are the ones that ask what the object is rather than what is in it.
What breaks on a list is everything that treats the columns as an index, which is what they are in pandas: `df.columns.tolist()`, `df.columns.name`, `df.columns.dtype`, `df.columns.equals(other.columns)`, `df.columns.get_loc("x")`, and the set operations `df.columns.intersection(other.columns)` and `.difference(...)` that are the usual way to write a column selection that does not care about order.
There is already an `Index` type on the Python side and `s.index` returns one, so the shape of the fix is to have `DataFrame.columns` return one too rather than to build anything new. Two things need deciding first.
The first is whether a frame and the index it hands back are the same object across two reads. pandas returns the same object every time, so `df.columns is df.columns` is True there and a caller can hold one and compare it by identity. firepanda copies on the way out today, which is what `s.index` does as well, so this decision covers both and should be made once.
The second is that an `Index` of column names holds text and the `Index` a series carries holds row labels of whatever type the frame is indexed by. If those are the same type in the core then nothing here is special, and if they are not then this is the place that finds out.
Part of #154, and small enough to do before the rest of it.
Contributor guide
Research direction
Inspect the DataFrame.columns implementation and the existing Index type, then compare them with the Series index property. Check the five conformance cases mentioned in the issue, including Index methods and identity across repeated reads. Done means DataFrame.columns returns an Index with pandas-compatible behavior and the shared identity decision is covered for both columns and series indexes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- pandas
- Domain
- data-engineering
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100