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

fix: Create indices with object dtype #1084

Merged
merged 4 commits into from
Sep 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why did we have this in the first place but is no longer necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

get_dummies() creates bools by default. This brought it back to float when reinserting into the dataframe. Instead, I make them floats at the beginning, since there's nothing that depends on them being bools above.

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
Loading