Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.1.rst
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
Expand Up @@ -734,21 +734,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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the same thing happen to TimedeltaIndex?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the underlying TimeDelta objects don't have the freq, so as type(object) a couple of lines below this return the correct objects without resetting the freq

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