Skip to content

Commit 27717a2

Browse files
meeseeksmachinephofl
andauthoredOct 14, 2022
Backport PR #49080 on branch 1.5.x (REGR: midx.values resetting freq of underyling index) (#49094)
Backport PR #49080: REGR: midx.values resetting freq of underyling index Co-authored-by: Patrick Hoefler <[email protected]>
1 parent c58f205 commit 27717a2

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed
 

‎doc/source/whatsnew/v1.5.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Fixed regressions
7676
- Fixed regression in :meth:`DataFrame.loc` raising ``FutureWarning`` when setting an empty :class:`DataFrame` (:issue:`48480`)
7777
- Fixed regression in :meth:`DataFrame.describe` raising ``TypeError`` when result contains ``NA`` (:issue:`48778`)
7878
- Fixed regression in :meth:`DataFrame.plot` ignoring invalid ``colormap`` for ``kind="scatter"`` (:issue:`48726`)
79+
- Fixed regression in :meth:`MultiIndex.values`` resetting ``freq`` attribute of underlying :class:`Index` object (:issue:`49054`)
7980
- Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`)
8081
- 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`)
8182
- Fixed regression in :func:`to_datetime` when ``arg`` was a date string with nanosecond and ``format`` contained ``%f`` would raise a ``ValueError`` (:issue:`48767`)

‎pandas/core/indexes/multi.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -723,21 +723,23 @@ def _values(self) -> np.ndarray:
723723
vals = cast("CategoricalIndex", vals)
724724
vals = vals._data._internal_get_values()
725725

726-
if isinstance(vals, ABCDatetimeIndex):
726+
is_dti = isinstance(vals, ABCDatetimeIndex)
727+
728+
if is_dti:
727729
# TODO: this can be removed after Timestamp.freq is removed
728730
# The astype(object) below does not remove the freq from
729731
# the underlying Timestamps so we remove it here to match
730732
# the behavior of self._get_level_values
731-
vals = vals.copy()
732-
vals.freq = None
733+
vals = algos.take_nd(vals, codes, fill_value=index._na_value)
733734

734735
if isinstance(vals.dtype, ExtensionDtype) or isinstance(
735736
vals, (ABCDatetimeIndex, ABCTimedeltaIndex)
736737
):
737738
vals = vals.astype(object)
738739

739740
vals = np.array(vals, copy=False)
740-
vals = algos.take_nd(vals, codes, fill_value=index._na_value)
741+
if not is_dti:
742+
vals = algos.take_nd(vals, codes, fill_value=index._na_value)
741743
values.append(vals)
742744

743745
arr = lib.fast_zip(values)

‎pandas/tests/indexes/multi/test_get_level_values.py

+11
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,14 @@ def test_get_level_values_when_periods():
112112
[idx._get_level_values(level) for level in range(idx.nlevels)]
113113
)
114114
assert all(x.is_monotonic_increasing for x in idx2.levels)
115+
116+
117+
def test_values_loses_freq_of_underlying_index():
118+
# GH#49054
119+
idx = pd.DatetimeIndex(date_range("20200101", periods=3, freq="BM"))
120+
expected = idx.copy(deep=True)
121+
idx2 = Index([1, 2, 3])
122+
midx = MultiIndex(levels=[idx, idx2], codes=[[0, 1, 2], [0, 1, 2]])
123+
midx.values
124+
assert idx.freq is not None
125+
tm.assert_index_equal(idx, expected)

0 commit comments

Comments
 (0)
Please sign in to comment.