Skip to content

Commit bc93815

Browse files
authored
Merge pull request #1084 from effigies/fix/pandas_deprecation
fix: Create indices with object dtype
2 parents 03a1af5 + 31f0441 commit bc93815

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

bids/modeling/transformations/munge.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def _transform(self, var, constraint='none', ref_level=None, sep='.'):
140140
variableClass = var.__class__
141141

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

145145
if len(levels) > 1 and constraint in ('drop_one', 'mean_zero'):
146146
if ref_level is None:
@@ -156,7 +156,7 @@ def _transform(self, var, constraint='none', ref_level=None, sep='.'):
156156
continue
157157
name = ''.join([var.name, sep, str(lev)])
158158
lev_data = data.copy()
159-
lev_data['amplitude'] = new_cols[lev].astype(float)
159+
lev_data['amplitude'] = new_cols[lev]
160160
args = [name, lev_data, var.source]
161161
if hasattr(var, 'run_info'):
162162
args.insert(2, var.run_info)

bids/variables/collections.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,14 @@ def to_df(
158158
ent_cols = list(all_cols - {"condition", "amplitude", "onset", "duration"})
159159

160160
if format == "long":
161-
df = df.reset_index(drop=True).fillna(fillna)
161+
with warnings.catch_warnings():
162+
# This change in behavior doesn't affect our usage, so ignore warnings
163+
# without setting a global config for Pandas.
164+
# Short story: fillna used to downcast object to float64, but if it isn't
165+
# already float64, we have non-float objects in the column, so we've never
166+
# downcasted.
167+
warnings.filterwarnings("ignore", message='no_silent_downcasting', category=FutureWarning)
168+
df = df.reset_index(drop=True).fillna(fillna)
162169
else:
163170
# Rows in wide format can only be defined by combinations of level entities
164171
# plus (for run-level variables) onset and duration.

bids/variables/io.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ def _load_time_variables(layout, dataset=None, columns=None, scan_length=None,
272272
columns={'amplitude': 'amplitude_'})
273273
warnings.warn(msg)
274274

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

278279
_cols = columns or list(set(_data.columns.tolist()) -
279280
{'onset', 'duration'})

bids/variables/variables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ def _build_entity_index(self, run_info, sampling_rate, match_vol=False):
526526

527527
def _create_index(all_keys, all_reps, all_ents):
528528
all_keys = np.array(sorted(all_keys))
529-
df = pd.DataFrame(np.zeros((sum(all_reps), len(all_keys))), columns=all_keys)
529+
df = pd.DataFrame(np.zeros((sum(all_reps), len(all_keys)), dtype=object), columns=all_keys)
530530

531531
prev_ix = 0
532532
for i, reps in enumerate(all_reps):

0 commit comments

Comments
 (0)