Repository: kutaslab/fitgrid

Issue:
BUG: usage of `groupby.get_group(0)` is bad
`groupby.get_group(0)` gets the group with name `0`, not the first group.

Possible fix is to replace with 
```python
first_group_name = list(groupby.groups)[0]
first_group = groupby.get_group(first_group_name)
```

Discussion:
codecov-io: # [Codecov](https://codecov.io/gh/kutaslab/fitgrid/pull/39?src=pr&el=h1) Report
> Merging [#39](https://codecov.io/gh/kutaslab/fitgrid/pull/39?src=pr&el=desc) into [master](https://codecov.io/gh/kutaslab/fitgrid/commit/06e5803882f11412adf99d32c57f04381d581ffa?src=pr&el=desc) will **increase** coverage by `0.19%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/kutaslab/fitgrid/pull/39/graphs/tree.svg?width=650&token=d9xVBW7gSn&height=150&src=pr)](https://codecov.io/gh/kutaslab/fitgrid/pull/39?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #39      +/-   ##
==========================================
+ Coverage   86.33%   86.52%   +0.19%     
==========================================
  Files           8        8              
  Lines         278      282       +4     
==========================================
+ Hits          240      244       +4     
  Misses         38       38
```


| [Impacted Files](https://codecov.io/gh/kutaslab/fitgrid/pull/39?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [fitgrid/epochs.py](https://codecov.io/gh/kutaslab/fitgrid/pull/39/diff?src=pr&el=tree#diff-Zml0Z3JpZC9lcG9jaHMucHk=) | `79.12% <100%> (ø)` | :arrow_up: |
| [fitgrid/tools.py](https://codecov.io/gh/kutaslab/fitgrid/pull/39/diff?src=pr&el=tree#diff-Zml0Z3JpZC90b29scy5weQ==) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/kutaslab/fitgrid/pull/39?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/kutaslab/fitgrid/pull/39?src=pr&el=footer). Last update [06e5803...107b849](https://codecov.io/gh/kutaslab/fitgrid/pull/39?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/fitgrid/epochs.py b/fitgrid/epochs.py
index d1f5b86..899298a 100644
--- a/fitgrid/epochs.py
+++ b/fitgrid/epochs.py
@@ -87,7 +87,7 @@ class Epochs:
         self.channels = channels
         self.table = table
         self._snapshots = snapshots
-        self._epoch_index = snapshots.get_group(0).index.copy()
+        self._epoch_index = tools.get_first_group(snapshots).index.copy()
 
     def _validate_LHS(self, LHS):
 
diff --git a/fitgrid/tools.py b/fitgrid/tools.py
index 114907f..a75d482 100644
--- a/fitgrid/tools.py
+++ b/fitgrid/tools.py
@@ -25,3 +25,10 @@ def get_index_duplicates_table(df, level):
         msg += '{0:<20} {1}\n'.format(value, ', '.join(locations))
 
     return msg
+
+
+def get_first_group(groupby):
+
+    first_group_name = list(groupby.groups)[0]
+    first_group = groupby.get_group(first_group_name)
+    return first_group


Test patch:
diff --git a/tests/test_fitgrid.py b/tests/test_fitgrid.py
index 43cb7e0..f14a93a 100644
--- a/tests/test_fitgrid.py
+++ b/tests/test_fitgrid.py
@@ -2,6 +2,7 @@ import pytest
 import numpy as np
 from .context import fitgrid
 from fitgrid.errors import FitGridError
+from fitgrid import tools
 import matplotlib
 
 matplotlib.use('Agg')
@@ -51,7 +52,8 @@ def test__residuals_have_long_form_and_correct_index():
         RHS='categorical + continuous',
     )
 
-    single_epoch = epochs._snapshots.get_group(0)
+    single_epoch = tools.get_first_group(epochs._snapshots)
+
     assert single_epoch.index.equals(grid.resid.index.levels[1])
 
 
@@ -69,7 +71,7 @@ def test__epoch_id_substitution():
     epochs = fitgrid.epochs_from_dataframe(data, channels)
 
     # remember epoch_index
-    epoch_index = epochs._snapshots.get_group(0).index
+    epoch_index = tools.get_first_group(epochs._snapshots).index
     assert (epoch_index == unusual_index).all()
 
     # take just two channels for speed