Skip to content

Commit

Permalink
FIX: Revert
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Dec 20, 2024
1 parent ae14a5e commit 94b8195
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
4 changes: 3 additions & 1 deletion mne/time_frequency/tests/test_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ def test_unaggregated_spectrum_to_data_frame(raw, long_format, method, output):
pytest.importorskip("pandas")
from pandas.testing import assert_frame_equal

from mne.utils.dataframe import _inplace

# aggregated spectrum → dataframe
orig_df = raw.compute_psd(method=method).to_data_frame(long_format=long_format)
# unaggregated welch or complex multitaper →
Expand All @@ -327,7 +329,7 @@ def test_unaggregated_spectrum_to_data_frame(raw, long_format, method, output):
orig_df = orig_df.loc[subset(orig_df["freq"])]
# sort orig_df, because at present we can't actually prevent pandas from
# sorting at the agg step *sigh*
orig_df.sort_values(by=grouping_cols, ignore_index=True)
_inplace(orig_df, "sort_values", by=grouping_cols, ignore_index=True)
# aggregate
df = df.drop(columns=drop_cols)
gb = df.groupby(grouping_cols, as_index=False, observed=False)
Expand Down
29 changes: 25 additions & 4 deletions mne/utils/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.

from inspect import signature

import numpy as np

from ..defaults import _handle_default
from ._logging import logger, verbose
from .check import check_version


@verbose
Expand Down Expand Up @@ -47,6 +50,24 @@ def _convert_times(times, time_format, meas_date=None, first_time=0):
return times


def _inplace(df, method, **kwargs):
# Handle transition: inplace=True (pandas <1.5) → copy=False (>=1.5)
# and 3.0 warning:
# E DeprecationWarning: The copy keyword is deprecated and will be removed in a
# future version. Copy-on-Write is active in pandas since 3.0 which utilizes a
# lazy copy mechanism that defers copies until necessary. Use .copy() to make
# an eager copy if necessary.
_meth = getattr(df, method) # used for set_index() and rename()

if check_version("pandas", "3.0"):
return _meth(**kwargs)
elif "copy" in signature(_meth).parameters:
return _meth(**kwargs, copy=False)
else:
_meth(**kwargs, inplace=True)
return df


@verbose
def _build_data_frame(
inst,
Expand Down Expand Up @@ -74,16 +95,16 @@ def _build_data_frame(
df.insert(i, k, v)
# build Index
if long_format:
df.set_index(keys=default_index)
df = _inplace(df, "set_index", keys=default_index)
df.columns.name = col_kind
elif index is not None:
df.set_index(keys=index)
df = _inplace(df, "set_index", keys=index)
if set(index) == set(default_index):
df.columns.name = col_kind
# long format
if long_format:
df = df.stack().reset_index()
df.rename(columns={0: "value"})
df = _inplace(df, "rename", columns={0: "value"})
# add column for channel types (as appropriate)
ch_map = (
None
Expand All @@ -101,7 +122,7 @@ def _build_data_frame(
df.insert(col_index, "ch_type", ch_type)
# restore index
if index is not None:
df.set_index(keys=index)
df = _inplace(df, "set_index", keys=index)
# convert channel/vertex/ch_type columns to factors
to_factor = [
c for c in df.columns.tolist() if c not in ("freq", "time", "value")
Expand Down

0 comments on commit 94b8195

Please sign in to comment.