Description
Code Sample
import pandas as pd
pd.show_versions()
p_index = pd.PeriodIndex(["20181101 1100", "20181101 1200", "20181102 1300", "20181102 1400"], name='datetime', freq="B")
mi_series = pd.DataFrame([["A", "B", 1.0],
["A", "C", 2.0],
["Z", "Q", 3.0],
["W", "F", 4.0]], index=p_index, columns=["ONE", "TWO", "VALUES"])
mi_series = mi_series.set_index(["ONE", "TWO"], append=True)["VALUES"]
# mi_series:
# datetime ONE TWO
# 2018-11-01 A B 1.0
# C 2.0
# 2018-11-02 Z Q 3.0
# W F 4.0
# Name: VALUES, dtype: float64
mi_series_2levels = mi_series.reset_index("ONE", drop=True)
# Should return a single value:
mi_series.loc[(p_index[0], "A", "B")]
# Expected: 1.0
# Actual:
# ONE TWO
# A B 1.0
# C 2.0
# Name: VALUES, dtype: float64
# Workaround:
assert mi_series.loc[p_index[0]].loc[("A", "B")] == 1.0
# If the MultiIndex has only two levels, it works as expected:
assert mi_series_2levels.loc[(p_index[0], "B")] == 1.0
# Here is probably why:
from pandas.core.tools.datetimes import parse_time_string
mi_key = (pd.Period("2018-11-01", freq="B"), "1", "1")
# This returns a nonsensical tuple:
parse_time_string(mi_key)
# And this returns True when it probably shouldn't:
mi_key in mi_series.index.levels[0]
Problem description
For a Series with a MultiIndex that 1) has exactly 3 levels, and 2) the first level is a PeriodIndex (see examples above), indexing by a triple (series.loc[(first, second, third)]
) behaves the same as series.loc[first]
, i.e. the return value is another series with a 2-level MultiIndex, whereas I expect a scalar value from the series.
If the first level is not a PeriodIndex, or the MultiIndex has any number of levels other than 3, the problem does not occur.
Output of pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 2.7.13.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-514.10.2.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8
pandas: 0.23.4
pytest: 3.1.0
pip: 8.1.1
setuptools: 28.8.0
Cython: None
numpy: 1.15.4
scipy: None
pyarrow: None
xarray: None
IPython: 5.3.0
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None