Skip to content

[SPARK-42574][CONNECT][PYTHON] Fix toPandas to handle duplicated column names - #40170

Closed
ueshin wants to merge 2 commits into
apache:masterfrom
ueshin:issues/SPARK-42574/toPandas
Closed

[SPARK-42574][CONNECT][PYTHON] Fix toPandas to handle duplicated column names#40170
ueshin wants to merge 2 commits into
apache:masterfrom
ueshin:issues/SPARK-42574/toPandas

Conversation

@ueshin

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

Fixes DataFrame.toPandas to handle duplicated column names.

Why are the changes needed?

Currently

spark.sql("select 1 v, 1 v").toPandas()

fails with the error:

Traceback (mostrecentcalllast):
...
File".../python/pyspark/sql/connect/dataframe.py", line1335, intoPandasreturnself._session.client.to_pandas(query)
File".../python/pyspark/sql/connect/client.py", line548, into_pandaspdf=table.to_pandas()
File"pyarrow/array.pxi", line830, inpyarrow.lib._PandasConvertible.to_pandasFile"pyarrow/table.pxi", line3908, inpyarrow.lib.Table._to_pandasFile"/.../lib/python3.9/site-packages/pyarrow/pandas_compat.py", line819, intable_to_blockmanagercolumns=_deserialize_column_index(table, all_columns, column_indexes)
File"/.../lib/python3.9/site-packages/pyarrow/pandas_compat.py", line938, in_deserialize_column_indexcolumns=_flatten_single_level_multiindex(columns)
File"/.../lib/python3.9/site-packages/pyarrow/pandas_compat.py", line1186, in_flatten_single_level_multiindexraiseValueError('Found non-unique column index')
ValueError: Foundnon-uniquecolumnindex

Simliar to #28210.

Does this PR introduce any user-facing change?

Duplicated column names will be available when calling toPandas().

How was this patch tested?

Enabled related tests.

@HyukjinKwon

Copy link
Copy Markdown
Member

Merged to master and branch-3.4.

HyukjinKwon pushed a commit that referenced this pull request Feb 27, 2023
…mn names
### What changes were proposed in this pull request?
Fixes `DataFrame.toPandas` to handle duplicated column names.
### Why are the changes needed?
Currently
```py
spark.sql("select 1 v, 1 v").toPandas()
```
fails with the error:
```py
Traceback (most recent call last):
...
File ".../python/pyspark/sql/connect/dataframe.py", line 1335, in toPandas
return self._session.client.to_pandas(query)
File ".../python/pyspark/sql/connect/client.py", line 548, in to_pandas
pdf = table.to_pandas()
File "pyarrow/array.pxi", line 830, in pyarrow.lib._PandasConvertible.to_pandas
File "pyarrow/table.pxi", line 3908, in pyarrow.lib.Table._to_pandas
File "/.../lib/python3.9/site-packages/pyarrow/pandas_compat.py", line 819, in table_to_blockmanager
columns = _deserialize_column_index(table, all_columns, column_indexes)
File "/.../lib/python3.9/site-packages/pyarrow/pandas_compat.py", line 938, in _deserialize_column_index
columns = _flatten_single_level_multiindex(columns)
File "/.../lib/python3.9/site-packages/pyarrow/pandas_compat.py", line 1186, in _flatten_single_level_multiindex
raise ValueError('Found non-unique column index')
ValueError: Found non-unique column index
```
Simliar to #28210.
### Does this PR introduce _any_ user-facing change?
Duplicated column names will be available when calling `toPandas()`.
### How was this patch tested?
Enabled related tests.
Closes#40170 from ueshin/issues/SPARK-42574/toPandas.
Authored-by: Takuya UESHIN <ueshin@databricks.com>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
(cherry picked from commit 89cf490)
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
@zhengruifeng

Copy link
Copy Markdown
Contributor

late LGTM, thanks!

snmvaughan pushed a commit to snmvaughan/spark that referenced this pull request Jun 20, 2023
…mn names
### What changes were proposed in this pull request?
Fixes `DataFrame.toPandas` to handle duplicated column names.
### Why are the changes needed?
Currently
```py
spark.sql("select 1 v, 1 v").toPandas()
```
fails with the error:
```py
Traceback (most recent call last):
...
File ".../python/pyspark/sql/connect/dataframe.py", line 1335, in toPandas
return self._session.client.to_pandas(query)
File ".../python/pyspark/sql/connect/client.py", line 548, in to_pandas
pdf = table.to_pandas()
File "pyarrow/array.pxi", line 830, in pyarrow.lib._PandasConvertible.to_pandas
File "pyarrow/table.pxi", line 3908, in pyarrow.lib.Table._to_pandas
File "/.../lib/python3.9/site-packages/pyarrow/pandas_compat.py", line 819, in table_to_blockmanager
columns = _deserialize_column_index(table, all_columns, column_indexes)
File "/.../lib/python3.9/site-packages/pyarrow/pandas_compat.py", line 938, in _deserialize_column_index
columns = _flatten_single_level_multiindex(columns)
File "/.../lib/python3.9/site-packages/pyarrow/pandas_compat.py", line 1186, in _flatten_single_level_multiindex
raise ValueError('Found non-unique column index')
ValueError: Found non-unique column index
```
Simliar to apache#28210.
### Does this PR introduce _any_ user-facing change?
Duplicated column names will be available when calling `toPandas()`.
### How was this patch tested?
Enabled related tests.
Closesapache#40170 from ueshin/issues/SPARK-42574/toPandas.
Authored-by: Takuya UESHIN <ueshin@databricks.com>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
(cherry picked from commit 89cf490)
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
@ueshin
ueshin deleted the issues/SPARK-42574/toPandas branch August 3, 2026 22:59
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@ueshin@HyukjinKwon@zhengruifeng