Skip to content

Commit

Permalink
Backport PR #49080 on branch 1.5.x (REGR: midx.values resetting freq …
Browse files Browse the repository at this point in the history
…of underyling index) (#49094)

Backport PR #49080: REGR: midx.values resetting freq of underyling index

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl authored Oct 14, 2022
1 parent c58f205 commit 27717a2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrame.loc` raising ``FutureWarning`` when setting an empty :class:`DataFrame` (:issue:`48480`)
- Fixed regression in :meth:`DataFrame.describe` raising ``TypeError`` when result contains ``NA`` (:issue:`48778`)
- Fixed regression in :meth:`DataFrame.plot` ignoring invalid ``colormap`` for ``kind="scatter"`` (:issue:`48726`)
- Fixed regression in :meth:`MultiIndex.values`` resetting ``freq`` attribute of underlying :class:`Index` object (:issue:`49054`)
- Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`)
- Fixed regression causing an ``AttributeError`` during warning emitted if the provided table name in :meth:`DataFrame.to_sql` and the table name actually used in the database do not match (:issue:`48733`)
- Fixed regression in :func:`to_datetime` when ``arg`` was a date string with nanosecond and ``format`` contained ``%f`` would raise a ``ValueError`` (:issue:`48767`)
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,21 +723,23 @@ def _values(self) -> np.ndarray:
vals = cast("CategoricalIndex", vals)
vals = vals._data._internal_get_values()

if isinstance(vals, ABCDatetimeIndex):
is_dti = isinstance(vals, ABCDatetimeIndex)

if is_dti:
# TODO: this can be removed after Timestamp.freq is removed
# The astype(object) below does not remove the freq from
# the underlying Timestamps so we remove it here to match
# the behavior of self._get_level_values
vals = vals.copy()
vals.freq = None
vals = algos.take_nd(vals, codes, fill_value=index._na_value)

if isinstance(vals.dtype, ExtensionDtype) or isinstance(
vals, (ABCDatetimeIndex, ABCTimedeltaIndex)
):
vals = vals.astype(object)

vals = np.array(vals, copy=False)
vals = algos.take_nd(vals, codes, fill_value=index._na_value)
if not is_dti:
vals = algos.take_nd(vals, codes, fill_value=index._na_value)
values.append(vals)

arr = lib.fast_zip(values)
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexes/multi/test_get_level_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,14 @@ def test_get_level_values_when_periods():
[idx._get_level_values(level) for level in range(idx.nlevels)]
)
assert all(x.is_monotonic_increasing for x in idx2.levels)


def test_values_loses_freq_of_underlying_index():
# GH#49054
idx = pd.DatetimeIndex(date_range("20200101", periods=3, freq="BM"))
expected = idx.copy(deep=True)
idx2 = Index([1, 2, 3])
midx = MultiIndex(levels=[idx, idx2], codes=[[0, 1, 2], [0, 1, 2]])
midx.values
assert idx.freq is not None
tm.assert_index_equal(idx, expected)

0 comments on commit 27717a2

Please sign in to comment.