Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(python): raise error when pandas df's some index name duplicates some column name #16023

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions py-polars/polars/_utils/construction/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,11 @@ def pandas_to_pydf(
) -> PyDataFrame:
"""Construct a PyDataFrame from a pandas DataFrame."""
convert_index = include_index and not _pandas_has_default_index(data)
if convert_index and set(data.index.names).intersection(data.columns):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Polars converts non-string column names to string

I think a more generic solution is needed, to handle #16025 too

msg = (
"cannot create DataFrame with some index name duplicating some column name"
)
raise ValueError(msg)
piri-p marked this conversation as resolved.
Show resolved Hide resolved
if not convert_index and all(
is_simple_numpy_backed_pandas_series(data[col]) for col in data.columns
):
Expand Down
68 changes: 68 additions & 0 deletions py-polars/tests/unit/interop/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,74 @@ def test_from_pandas_duplicated_columns() -> None:
pl.from_pandas(df)


def test_from_pandas_exclude_index() -> None:
data = pd.DataFrame({"a": [1, 2], "b": [3, 4]}, index=pd.Index([5, 6], name="c"))
df = pl.from_pandas(data, include_index=False)
assert df.columns == ["a", "b"]
assert df.rows() == [(1, 3), (2, 4)]


def test_from_pandas_include_index() -> None:
data = pd.DataFrame({"a": [1, 2], "b": [3, 4]}, index=pd.Index([5, 6], name="c"))
df = pl.from_pandas(data, include_index=True)
assert df.columns == ["c", "a", "b"]
assert df.rows() == [(5, 1, 3), (6, 2, 4)]


def test_from_pandas_exclude_dup_index() -> None:
data = pd.DataFrame({"a": [1, 2], "b": [3, 4]}, index=pd.Index([5, 6], name="a"))
df = pl.from_pandas(data, include_index=False)
assert df.columns == ["a", "b"]
assert df.rows() == [(1, 3), (2, 4)]


def test_from_pandas_include_dup_index() -> None:
data = pd.DataFrame({"a": [1, 2], "b": [3, 4]}, index=pd.Index([5, 6], name="a"))

with pytest.raises(ValueError):
pl.from_pandas(data, include_index=True)


def test_from_pandas_exclude_multi_index() -> None:
data = pd.DataFrame(
{"a": [1, 2], "b": [3, 4]},
index=pd.MultiIndex.from_arrays([(5, 6), (7, 8)], names=["c", "d"]),
)
df = pl.from_pandas(data, include_index=False)
assert df.columns == ["a", "b"]
assert df.rows() == [(1, 3), (2, 4)]


def test_from_pandas_include_multi_index() -> None:
data = pd.DataFrame(
{"a": [1, 2], "b": [3, 4]},
index=pd.MultiIndex.from_arrays([(5, 6), (7, 8)], names=["c", "d"]),
)
df = pl.from_pandas(data, include_index=True)
assert df.columns == ["c", "d", "a", "b"]
assert df.rows() == [(5, 7, 1, 3), (6, 8, 2, 4)]


def test_from_pandas_exclude_dup_multi_index() -> None:
data = pd.DataFrame(
{"a": [1, 2], "b": [3, 4]},
index=pd.MultiIndex.from_arrays([(5, 6), (7, 8)], names=["b", "c"]),
)
df = pl.from_pandas(data, include_index=False)
assert df.columns == ["a", "b"]
assert df.rows() == [(1, 3), (2, 4)]


def test_from_pandas_include_dup_multi_index() -> None:
data = pd.DataFrame(
{"a": [1, 2], "b": [3, 4]},
index=pd.MultiIndex.from_arrays([(5, 6), (7, 8)], names=["b", "c"]),
)

with pytest.raises(ValueError):
pl.from_pandas(data, include_index=True)


def test_arrow_list_roundtrip() -> None:
# https://github.com/pola-rs/polars/issues/1064
tbl = pa.table({"a": [1], "b": [[1, 2]]})
Expand Down
Loading