NeuralAnalysis / PyalData

Repository for the Python implementation of the TrialData analysis library.
GNU General Public License v3.0
7 stars 9 forks source link

KeyValue errors from trying to refer to first value of trial_data column #125

Closed raeedcho closed 2 years ago

raeedcho commented 2 years ago

Some pieces of code in the package intend to refer to the first value of a column in the trial_data DataFrame using something like:

trial_data.bin_size[0] (example from line 99 of firing_rates.py)

This kind of reference breaks when the DataFrame doesn't have a row with index 0--which happens when you want to use another column as an index (e.g. trial_id) or when you subselect trials without resetting the index.

For generality, I think I would change these kinds of references to something like:

trial_data['bin_size'].values[0] or trial_data.loc[trial_data.index[0],'bin_size']

I prefer the first one most of the time for reading ease, but I think the second one avoids chained indexing, which might be preferred in cases where you're trying to assign a value to the element, rather than just accessing and viewing it.

bagibence commented 2 years ago

Nice catch, thanks! I also strongly prefer the first option of trial_data['bin_size'].values[0], let's go with that. Have you found any other occurrences?

raeedcho commented 2 years ago

I think I've seen it from time to time in other functions, but can't think of any places off the top of my head. I'll fix and open pull requests as I find them.