Skip to content

Commit bf63e5c

Browse files
phoflnoatamir
authored andcommitted
REGR: midx.values resetting freq of underyling index (pandas-dev#49080)
1 parent 965c096 commit bf63e5c

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
@@ -734,21 +734,23 @@ def _values(self) -> np.ndarray:
734734
vals = cast("CategoricalIndex", vals)
735735
vals = vals._data._internal_get_values()
736736

737-
if isinstance(vals, ABCDatetimeIndex):
737+
is_dti = isinstance(vals, ABCDatetimeIndex)
738+
739+
if is_dti:
738740
# TODO: this can be removed after Timestamp.freq is removed
739741
# The astype(object) below does not remove the freq from
740742
# the underlying Timestamps so we remove it here to match
741743
# the behavior of self._get_level_values
742-
vals = vals.copy()
743-
vals.freq = None
744+
vals = algos.take_nd(vals, codes, fill_value=index._na_value)
744745

745746
if isinstance(vals.dtype, ExtensionDtype) or isinstance(
746747
vals, (ABCDatetimeIndex, ABCTimedeltaIndex)
747748
):
748749
vals = vals.astype(object)
749750

750751
vals = np.array(vals, copy=False)
751-
vals = algos.take_nd(vals, codes, fill_value=index._na_value)
752+
if not is_dti:
753+
vals = algos.take_nd(vals, codes, fill_value=index._na_value)
752754
values.append(vals)
753755

754756
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)