Description
-
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.
This bug is happening just in the pandas' version 1.2.0. The indexes are disappearing in the following condition:
- Using MultiIndex;
- Using an empty dataframe;
- GroupBy the empty dataframe with multi indexes;
- Aggregate using the
list
method.
Code Sample
Here is a code example:
import pandas as pd
# Create a mock dataframe
df = pd.DataFrame(data=[[1, 2, 1],[1, 2, 0]], columns=['a', 'b', 'c'])
# filter it to be an empty dataframe
df = df[df['c']<0]
# group to be multiIndexes and aggregate the groupby using the list method
df = df.groupby(['a','b']).agg(d=('c', list))
print(df.index)
The output of this piece of code is:
Index([], dtype='object')
and it should be:
MultiIndex([], names=['a', 'b'])
Problem description
This bug is happening only in the pandas' version 1.2.0, I've tried using versions 1.1.4 and 1.1.5, and the bug does not appear.
It is a very specific error that happens just when using MultiIndex, empty DataFrame, groupby, and aggregate using list
method.
In this case, the indexes names will disappear, and the index will no longer be MultiIndex
but Index
.
Expected Output
The output should be an empty dataframe multi indexed with the indexes names preserved.
Output of pd.show_versions()
Follows the pd.show_versions()
python-bits : 64
OS : Linux
OS-release : 5.4.0-53-generic
Version : #59-Ubuntu SMP Wed Oct 21 09:38:44 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : pt_BR.UTF-8
pandas : 1.2.0
numpy : 1.18.5
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
setuptools : 51.0.0
Cython : 0.29.14
pytest : 6.2.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 0.8.5
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : None
odfpy : None
openpyxl : 3.0.5
pandas_gbq : None
pyarrow : 2.0.0
pyxlsb : None
s3fs : 0.4.2
scipy : 1.6.0
sqlalchemy : 1.3.22
tables : None
tabulate : 0.8.7
xarray : None
xlrd : None
xlwt : None
numba : 0.52.0