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
In [1]: import pandas as pd
In [2]: pdf = pd.DataFrame(
...: {
...: "b": [1.0, 3.0, pd.NA],
...: "a": [1, 4, 3],
...: 1: ["a", "b", "c"],
...: "e": [3, 1, 4],
...: "d": [1, 2, 8],
...: }
...: )
In [3]: pdf
Out[3]:
b a 1 e d
0 1.0 1 a 3 1
1 3.0 4 b 1 2
2 <NA> 3 c 4 8
In [4]: df = pdf.set_index(["b", "a", 1])
In [5]: df.sort_index(axis='columns', ignore_index=True)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[5], line 1
----> 1 df.sort_index(axis='columns', ignore_index=True)
File /nvme/0/pgali/envs/cudfdev/lib/python3.10/site-packages/pandas/core/frame.py:7134, in DataFrame.sort_index(self, axis, level, ascending, inplace, kind, na_position, sort_remaining, ignore_index, key)
7037 def sort_index(
7038 self,
7039 *,
(...)
7048 key: IndexKeyFunc | None = None,
7049 ) -> DataFrame | None:
7050 """
7051 Sort object by labels (along an axis).
7052
(...)
7132 d 4
7133 """
-> 7134 return super().sort_index(
7135 axis=axis,
7136 level=level,
7137 ascending=ascending,
7138 inplace=inplace,
7139 kind=kind,
7140 na_position=na_position,
7141 sort_remaining=sort_remaining,
7142 ignore_index=ignore_index,
7143 key=key,
7144 )
File /nvme/0/pgali/envs/cudfdev/lib/python3.10/site-packages/pandas/core/generic.py:5257, in NDFrame.sort_index(self, axis, level, ascending, inplace, kind, na_position, sort_remaining, ignore_index, key)
5255 if ignore_index:
5256 axis = 1 if isinstance(self, ABCDataFrame) else 0
-> 5257 new_data.set_axis(axis, default_index(len(indexer)))
5259 result = self._constructor_from_mgr(new_data, axes=new_data.axes)
5261 if inplace:
File /nvme/0/pgali/envs/cudfdev/lib/python3.10/site-packages/pandas/core/internals/managers.py:227, in BaseBlockManager.set_axis(self, axis, new_labels)
225 def set_axis(self, axis: AxisInt, new_labels: Index) -> None:
226 # Caller is responsible for ensuring we have an Index object.
--> 227 self._validate_set_axis(axis, new_labels)
228 self.axes[axis] = new_labels
File /nvme/0/pgali/envs/cudfdev/lib/python3.10/site-packages/pandas/core/internals/base.py:85, in DataManager._validate_set_axis(self, axis, new_labels)
82 pass
84 elif new_len != old_len:
---> 85 raise ValueError(
86 f"Length mismatch: Expected axis has {old_len} elements, new "
87 f"values have {new_len} elements"
88 )
ValueError: Length mismatch: Expected axis has 3 elements, new values have 2 elements
Issue Description
When we try to sort a dataframe by columns
and pass ignore_index=True
, there is a failure.
Expected Behavior
I expect it to not fail and return a dataframe with existing columns/dropped columns with labels 0, 1, 2...n-1.
Installed Versions
In [6]: pd.show_versions()
INSTALLED VERSIONS
commit : a671b5a
python : 3.10.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-88-generic
Version : #98-Ubuntu SMP Mon Oct 2 15:18:56 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.1.4
numpy : 1.24.4
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.3.1
Cython : 3.0.6
pytest : 7.4.3
hypothesis : 6.91.0
sphinx : 7.2.6
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.18.1
pandas_datareader : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : 2023.12.1
gcsfs : None
matplotlib : None
numba : 0.57.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 14.0.1
pyreadstat : None
pyxlsb : None
s3fs : 2023.12.1
scipy : 1.11.4
sqlalchemy : 2.0.23
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None