Skip to content

BUG: Enable changing dtype of column where column names are not unique #27256

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

Closed
wants to merge 1 commit 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
1 change: 0 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,6 @@ def can_do_equal_len():
for i, idx in enumerate(indexer)
if i != info_axis
)
and item_labels.is_unique
):
self.obj[item_labels[indexer[info_axis]]] = value
return
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,12 @@ def value_getitem(placement):
blknos = self._blknos[loc]
blklocs = self._blklocs[loc].copy()

if not self.axes[0].is_unique:
Copy link
Contributor

Choose a reason for hiding this comment

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

really don't want to add anything in internals; why is this not possible to be done in pandas/core/indexing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jreback agreed, thanks for feedback! This is work in progress. I'll try to fix it in pandas/core/indexing.

if not isinstance(blknos, np.ndarray):
blknos = [blknos]
if not isinstance(blklocs, np.ndarray):
blklocs = [blklocs]

unfit_mgr_locs = []
unfit_val_locs = []
removed_blknos = []
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,17 @@ def test_iloc_setitem_dups(self):
df.iloc[[1, 0], [0, 1]] = df.iloc[[1, 0], [0, 1]].reset_index(drop=True)
tm.assert_frame_equal(df, expected)

def test_iloc_change_dtype_dups(self):
# GH 22035 and GH 24798

df = DataFrame([['0', 1, '2']], columns=['A', 'B', 'B'])
df.iloc[:, 0] = df.iloc[:, 0].astype(int)
assert df.dtypes.tolist() == ['int', 'int', 'O']

df = DataFrame([['0', '1', '2']], columns=['A', 'B', 'B'])
df.iloc[:, 0] = df.iloc[:, 0].astype(int)
assert df.dtypes.tolist() == ['int', 'O', 'O']

def test_iloc_getitem_frame(self):
df = DataFrame(
np.random.randn(10, 4), index=range(0, 20, 2), columns=range(0, 8, 2)
Expand Down