Skip to content

Commit da79229

Browse files
antocommimwouts
andauthored
solve bug caused by df with duplicated columns name (#89)
* solve bug caused by df with duplicated col name * solve bug caused by df with duplicated col name * Version 1.1.3 (#1) * Prepare version 1.1.3 * Use 'j' for the column index and add a comment Co-authored-by: Marc Wouts <[email protected]>
1 parent ec388a7 commit da79229

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

docs/changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
ITables ChangeLog
22
=================
33

4+
1.1.3 (2022-08-11)
5+
------------------
6+
7+
**Fixed**
8+
- Tables with duplicate column names are now supported, thanks to Antonio Commisso's fix ([#89](https://github.com/mwouts/jupytext/issues/89))
9+
10+
411
1.1.2 (2022-06-30)
512
------------------
613

itables/javascript.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,21 @@ def init_notebook_mode(
104104
def _formatted_values(df):
105105
"""Return the table content as a list of lists for DataTables"""
106106
formatted_df = df.copy()
107-
for col in formatted_df:
108-
x = formatted_df[col]
107+
# We iterate over columns using an index rather than the column name
108+
# to avoid an issue in case of duplicate column names #89
109+
for j, col in enumerate(formatted_df):
110+
x = formatted_df.iloc[:, j]
109111
if x.dtype.kind in ["b", "i", "s"]:
110112
continue
111113

112114
if x.dtype.kind == "O":
113-
formatted_df[col] = formatted_df[col].astype(str)
115+
formatted_df.iloc[:, j] = formatted_df.iloc[:, j].astype(str)
114116
continue
115117

116-
formatted_df[col] = np.array(fmt.format_array(x.values, None))
118+
formatted_df.iloc[:, j] = np.array(fmt.format_array(x.values, None))
117119
if x.dtype.kind == "f":
118120
try:
119-
formatted_df[col] = formatted_df[col].astype(float)
121+
formatted_df.iloc[:, j] = formatted_df.iloc[:, j].astype(float)
120122
except ValueError:
121123
pass
122124

itables/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""ITables' version number"""
22

3-
__version__ = "1.1.2"
3+
__version__ = "1.1.3"

tests/test_sample_dfs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pandas as pd
12
import pytest
23

34
from itables import show
@@ -39,3 +40,9 @@ def test_show_test_dfs(df_name, df):
3940
@pytest.mark.parametrize("series_name,series", get_dict_of_test_series().items())
4041
def test_show_test_series(series_name, series):
4142
show(series)
43+
44+
45+
def test_show_df_with_duplicate_column_names():
46+
df = pd.DataFrame({"a": [0], "b": [0.0], "c": ["str"]})
47+
df.columns = ["duplicated_name"] * 3
48+
show(df)

0 commit comments

Comments
 (0)