BUG: pd.Series/DataFrame.truncate() drops multiindex names · Issue #34564 · pandas-dev/pandas (original) (raw)
- I have checked that this issue has not already been reported.
- I have confirmed this bug exists on the latest version of pandas.
- (optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
create multiindex with names L1/L2
mi = pd.MultiIndex.from_product([[1, 2, 3, 4], ['A', 'B']], names=['L1', 'L2']) s1 = pd.Series(range(mi.shape[0]), index=mi)
truncate drops the names in the index of s2
s2 = s1.truncate(before=2, after=3)
Problem description
The current version seems to drop index level names. This was not the case in pandas version 0.23.4
Expected Output
s1
Out[1]:
L1 L2
1 A 0
B 1
2 A 2
B 3
3 A 4
B 5
4 A 6
B 7
dtype: int64
# In pandas 0.23.4
s2
Out[2]:
L1 L2
2 A 2
B 3
3 A 4
B 5
dtype: int64
# In pandas 1.0.3 (drops axis labels L1, L2 along truncation axis)
s2
Out[2]:
2 A 2
B 3
3 A 4
B 5
dtype: int64
Output of pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.6.final.0
python-bits : 64
OS : Linux
OS-release : 4.14.13-1.el7.elrepo.x86_64
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.0.3
numpy : 1.18.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.4.0.post20200518
Cython : 0.29.17
pytest : 5.4.2
hypothesis : None
sphinx : 3.0.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.13.0
pandas_datareader: None
bs4 : None
bottleneck : 1.3.2
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.1.3
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.2
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.17
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : None
numba : 0.49.1