Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] I have confirmed this bug exists on the latest version of pandas.
-
[x] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
- Create a series with
Index
and a MultiIndex to use for reindexing later
>>> series = pd.Series(
... [26.7300, 24.2550],
... index=pd.Index([81, 82], name='a')
... )
>>> series
a
81 26.730
82 24.255
dtype: float64
>>> series.index
Index([81, 82], dtype='int64', name='a')
>>> other_index = pd.MultiIndex(
... levels=[
... pd.Index([81, 82], name='a'),
... pd.Index([np.nan], name='b'),
... pd.Index([
... '2018-06-01', '2018-07-01'
... ], name='c')
... ],
... codes=[
... [0, 0, 1, 1],
... [0, 0, 0, 0],
... [0, 1, 0, 1]
... ],
... names=['a', 'b', 'c']
... )
>>> other_index
MultiIndex([(81, nan, '2018-06-01'),
(81, nan, '2018-07-01'),
(82, nan, '2018-06-01'),
(82, nan, '2018-07-01')],
names=['a', 'b', 'c'])
reindex
toMultiIndex
(other_index
) which expandsseries.index
by two more levels.- unfortunately the
reindex
sets all values of the original series to NaN which can be fixed by turningseries.index
into a 1-levelMultiIndex
first
>>> series.reindex(other_index) # this removes all values of the series
a b c
81 NaN 2018-06-01 NaN
2018-07-01 NaN
82 NaN 2018-06-01 NaN
2018-07-01 NaN
dtype: float64
- apply
to_mi(...)
to turn theseries.index
into a 1-levelMultiIndex
- rerun
reindex
on the newseries
withMultiIndex
and the values are maintained/filled as expected
>>> def to_mi(series):
... if isinstance(series.index, pd.MultiIndex):
... series_mi = series.index
... else:
... level_names = [series.index.name]
... level_values = [series.index]
... series_mi = pd.MultiIndex.from_arrays(level_values, names=level_names)
... series_with_mi = pd.Series(series.values, index=series_mi, name=series.name)
... return series_with_mi
...
>>> series_mi = to_mi(series)
>>> series_mi
a
81 26.730
82 24.255
dtype: float64
>>> series_mi.index
MultiIndex([(81,),
(82,)],
names=['a'])
>>> series_mi.reindex(other_index)
a b c
81 NaN 2018-06-01 26.730
2018-07-01 26.730
82 NaN 2018-06-01 24.255
2018-07-01 24.255
dtype: float64
Issue Description
In the above case, series.reindex(multi_index)
will turn the series values to NaN when the series has a single Index
. However when the series index is converted to a 1-level MultiIndex
prior to the reindex
, the values are maintained and filled as expected.
In my opinion it shouldn't matter if a 1-level MultiIndex
or an Index
is used for a reindex
- the outcomes should be the same.
As a further discussion point (here or elsewhere), this issue (and others) also begs the question why a distinction between Index
and MultiIndex
is necessary (I suspect there are historic reasons). I would imagine that many issues (and code) would go away if MultiIndex
was used exclusively (even for 1-dimensional indices).
Expected Behavior
The missing levels in series_mi
(compared to other_index
) are added and the values of the partial index from the original series are used to fill the places of the added indices.
>>> series_mi.reindex(other_index)
a b c
81 NaN 2018-06-01 26.730 # from index <81> of `series` (`series_mi`)
2018-07-01 26.730 # from index <81> of `series` (`series_mi`)
82 NaN 2018-06-01 24.255 # from index <82> of `series` (`series_mi`)
2018-07-01 24.255 # from index <82> of `series` (`series_mi`)
dtype: float64
Installed Versions
Comment From: rhshadrach
Thanks for the report! Is it possible to shorten your example? Making it as short as possible to demonstrate the behavior would certainly be appreciated.
Comment From: ssche
Thanks for the feedback. I managed to shorten the example and provided additional comments.
Comment From: micheleuap
Adding to this because I've seen this situation before.
This is about the fact when calling reindex
, it makes a difference if the calling frame has a regular index, or an "identical" single-level multi-index.
series = pd.Series([1, 2], index=pd.Index(["x", "y"], name="lvl1"))
# the same series as above, with the index converted to multi-index
series_mi = series.copy()
series_mi.index = pd.MultiIndex.from_frame(series.index.to_frame())
# a multi-index with a level not present in the index of the series
idx = pd.MultiIndex.from_product([["x", "y"], ["m", "n"]], names=["lvl1", "lvl2"])
# this returns nans
series.reindex(idx)
# this returns a series with values [1,1,2,2]
series_mi.reindex(idx)
Comment From: rhshadrach
Thanks @ssche - finally got some time to take a look into this. I believe this can currently be accomplished with the level
argument.
series = pd.Series(
[26.7300, 24.2550],
index=pd.Index([81, 82], name='a')
)
other_index = pd.MultiIndex(
levels=[
pd.Index([81, 82], name='a'),
pd.Index([np.nan], name='b'),
pd.Index([
'2018-06-01', '2018-07-01'
], name='c')
],
codes=[
[0, 0, 1, 1],
[0, 0, 0, 0],
[0, 1, 0, 1]
],
names=['a', 'b', 'c']
)
series.reindex(other_index, level="a")
# a b c
# 81 NaN 2018-06-01 26.730
# 2018-07-01 26.730
# 82 NaN 2018-06-01 24.255
# 2018-07-01 24.255
# dtype: float64
But it makes sense to me to do this automatically if it's feasible. PRs are welcome!
Comment From: ssche
From
https://pandas.pydata.org/docs/reference/api/pandas.Series.reindex.html#pandas.Series.reindex, it seems that level=None
(the default) is currently undefined as input:
level int or name Broadcast across a level, matching Index values on the passed MultiIndex level.
Could we repurpose level=None
as kind of "autodetect" to find overlapping levels? Is level=None
used for other use-cases already?
Comment From: rhshadrach
I think so. I do not see the current behavior as being a useful op, so I think we can do this without deprecation (but in a major release only).
Comment From: Roline-Stapny
take I am planning to add a check in series.reindex(..) method to set level value automatically if series index name is present in the MultiIndex level