Skip to content

Commit 05aaf61

Browse files
committed
convert non-string colnames to strings in interchange protocol
1 parent db11e25 commit 05aaf61

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Fixed regressions
2929

3030
Bug fixes
3131
~~~~~~~~~
32+
- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which wasn't converting columns names to strings (:issue:`55069`)
3233
- Fixed bug in :meth:`DataFrame.__getitem__` for empty :class:`DataFrame` with Copy-on-Write enabled (:issue:`57130`)
3334

3435
.. ---------------------------------------------------------------------------

pandas/core/interchange/dataframe.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self, df: DataFrame, allow_copy: bool = True) -> None:
3232
Constructor - an instance of this (private) class is returned from
3333
`pd.DataFrame.__dataframe__`.
3434
"""
35-
self._df = df
35+
self._df = df.rename(columns=str, copy=False)
3636
self._allow_copy = allow_copy
3737

3838
def __dataframe__(

pandas/tests/interchange/test_impl.py

+19
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,22 @@ def test_large_string():
392392
result = pd.api.interchange.from_dataframe(df.__dataframe__())
393393
expected = pd.DataFrame({"a": ["x"]}, dtype="object")
394394
tm.assert_frame_equal(result, expected)
395+
<<<<<<< HEAD
396+
=======
397+
398+
399+
@pytest.mark.parametrize("dtype", ["Int8", "Int8[pyarrow]"])
400+
def test_nullable_integers(dtype: str) -> None:
401+
pytest.importorskip("pyarrow")
402+
df = pd.DataFrame({"a": [1]}, dtype=dtype)
403+
expected = pd.DataFrame({"a": [1]}, dtype="int8")
404+
result = pd.api.interchange.from_dataframe(df.__dataframe__())
405+
tm.assert_frame_equal(result, expected)
406+
407+
408+
def test_non_str_names():
409+
# https://github.com/pandas-dev/pandas/issues/56701
410+
df = pd.Series([1, 2, 3], name=0).to_frame()
411+
names = df.__dataframe__().column_names()
412+
assert names == ["0"]
413+
>>>>>>> 2383e4b049 (wip)

0 commit comments

Comments
 (0)