Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] I have confirmed this bug exists on the latest version of pandas.

  • [ ] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

df = pd.DataFrame()
df = df.reindex(columns=list(df.columns) + ['A'])
df = df.reindex(columns=list(df.columns) + ['A'])
print(df.columns)
# Output: Index(['A', 'A'], dtype='object')

# ------------------------------------------------

df = pd.DataFrame()
df.insert(0, 'A', None)
df.insert(0, 'A', None) # <- Raises ValueError: cannot insert A, already exists

Issue Description

I am unsure if this behavior is intended or (a bug?), and would appreciate any insight on the matter. It seems strange that the pandas.DataFrame.reindex method allows in the Reproducible Example the creation of a duplicate column, while the pandas.DataFrame.insert method raises an error when we try to insert the same column (i.e, 'A') two times in a row.

Expected Behavior

I would expect pandas to raise an error when using pandas.DataFrame.reindex to conform a pandas.core.frame.DataFrame with a list of columns when at least one of those already exists in the dataframe.

In the documentation, we read :

drawing

Installed Versions

INSTALLED VERSIONS ------------------ python : 3.11.0.final.0 python-bits : 64 OS : Windows OS-release : 10 pandas : 1.5.3

Comment From: phofl

This behaves as intended, insert has an option to allow duplicates and the constructor allows you to create duplicated columns as well