Skip to content

Commit

Permalink
Merge pull request #1084 from effigies/fix/pandas_deprecation
Browse files Browse the repository at this point in the history
fix: Create indices with object dtype
  • Loading branch information
effigies authored Sep 6, 2024
2 parents 03a1af5 + 31f0441 commit bc93815
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions bids/modeling/transformations/munge.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _transform(self, var, constraint='none', ref_level=None, sep='.'):
variableClass = var.__class__

levels = np.sort(data['amplitude'].unique())
new_cols = pd.get_dummies(data['amplitude'], drop_first=False)[levels]
new_cols = pd.get_dummies(data['amplitude'], drop_first=False, dtype=float)[levels]

if len(levels) > 1 and constraint in ('drop_one', 'mean_zero'):
if ref_level is None:
Expand All @@ -156,7 +156,7 @@ def _transform(self, var, constraint='none', ref_level=None, sep='.'):
continue
name = ''.join([var.name, sep, str(lev)])
lev_data = data.copy()
lev_data['amplitude'] = new_cols[lev].astype(float)
lev_data['amplitude'] = new_cols[lev]
args = [name, lev_data, var.source]
if hasattr(var, 'run_info'):
args.insert(2, var.run_info)
Expand Down
9 changes: 8 additions & 1 deletion bids/variables/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,14 @@ def to_df(
ent_cols = list(all_cols - {"condition", "amplitude", "onset", "duration"})

if format == "long":
df = df.reset_index(drop=True).fillna(fillna)
with warnings.catch_warnings():
# This change in behavior doesn't affect our usage, so ignore warnings
# without setting a global config for Pandas.
# Short story: fillna used to downcast object to float64, but if it isn't
# already float64, we have non-float objects in the column, so we've never
# downcasted.
warnings.filterwarnings("ignore", message='no_silent_downcasting', category=FutureWarning)
df = df.reset_index(drop=True).fillna(fillna)
else:
# Rows in wide format can only be defined by combinations of level entities
# plus (for run-level variables) onset and duration.
Expand Down
5 changes: 3 additions & 2 deletions bids/variables/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,9 @@ def _load_time_variables(layout, dataset=None, columns=None, scan_length=None,
columns={'amplitude': 'amplitude_'})
warnings.warn(msg)

_data = _data.replace('n/a', np.nan) # Replace BIDS' n/a
_data = _data.apply(pd.to_numeric, errors='ignore')
# Pandas already converts 'n/a' to NaN. Leaving this comment
# because we used to do it manually here.
# We also converted to numeric, but this is now irrelevant.

_cols = columns or list(set(_data.columns.tolist()) -
{'onset', 'duration'})
Expand Down
2 changes: 1 addition & 1 deletion bids/variables/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def _build_entity_index(self, run_info, sampling_rate, match_vol=False):

def _create_index(all_keys, all_reps, all_ents):
all_keys = np.array(sorted(all_keys))
df = pd.DataFrame(np.zeros((sum(all_reps), len(all_keys))), columns=all_keys)
df = pd.DataFrame(np.zeros((sum(all_reps), len(all_keys)), dtype=object), columns=all_keys)

prev_ix = 0
for i, reps in enumerate(all_reps):
Expand Down

0 comments on commit bc93815

Please sign in to comment.