Open
Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
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
### Example 1: Tail ###
# Note: non-unique values in index
df = pd.DataFrame({"A": ["a", "b", "c"], "B": range(3)}, index=[1, 2, 2])
# Works
pd.concat([df.drop("B", axis=1), df["B"].tail()], axis=1)
# Fails
df2 = df.tail(2)
pd.concat([df.drop("B", axis=1), df2["B"].tail()], axis=1)
### Example 2: Explode ###
# Works
df = pd.DataFrame({"A": ["a", "b"], "B": [[0], [1, 2]]})
pd.concat([df.drop("B", axis=1), df["B"].explode()], axis=1)
# Fails
df = pd.DataFrame({"A": ["a", "b"], "B": [[0], [1, 2]]}, index=[0, 1])
pd.concat([df.drop("B", axis=1), df.explode("B")], axis=1)
Which both fail with:
raise InvalidIndexError(self._requires_unique_msg)
pandas.errors.InvalidIndexError: Reindexing only valid with uniquely valued Index objects
### Issue Description
The concat function seems to behave different based on different scenarios. Trying to troubleshoot I found two examples where I had unexpected errors from the concat function. The error returned is that Reindexing only valid with uniquely valued Index objects, but both examples, to me, appear to have counter parts that do work with non-unique indexes.
This may be related to https://github.com/pandas-dev/pandas/issues/51646 which has a similar issue.
### Expected Behavior
The behaivor I expected was for concat to work in a similar fashion to the "Works" version, even with the non-unique index, like a SQL left join or full-outer join.
### Installed Versions
INSTALLED VERSIONS
------------------
commit : 5dd6efc209f5a47aadc9813def6bb29695a14653
python : 3.11.0.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.0-32-generic
Version : #33~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Jan 30 17:03:34 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.1.0.dev0+82.g5dd6efc209
numpy : 1.24.1
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 65.5.0
pip : 23.0
Cython : None
pytest : 7.2.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.0.8
lxml.etree : 4.9.2
html5lib : None
pymysql : None
psycopg2 : 2.9.5
jinja2 : 3.1.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.3
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.0
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : 1.4.4
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : None
qtpy : None
pyqt5 : None