Repository: mie-lab/trackintel

Issue:
Bug in location_identifier for pre_filter=False and non-serial index
The function [location_identifier](https://github.com/mie-lab/trackintel/blob/master/trackintel/analysis/location_identification.py#L7) assumes that an index is serial e.g., incremental from 0 to n. It throws an error in fthe index is not serial and sorts the dataframe (may not be intended) if the index is serial but not in order.

The problem is that for `pre_filter=False` [here](https://github.com/mie-lab/trackintel/blob/master/trackintel/analysis/location_identification.py#L65) a boolean series with the same length as the staypoint dataframe and serial index is created. 

From the geopandas documentation for what can be input to `loc` for indexing: 
_An alignable boolean Series. The index of the key will be aligned before masking._ [source](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html)

A fix could be to either use the original index when creating `f` in line [65](https://github.com/mie-lab/trackintel/blob/master/trackintel/analysis/location_identification.py#L65), to use a boolean array without index or to use iloc instead of loc.




Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/mie-lab/trackintel/pull/408?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mie-lab) Report
> Merging [#408](https://codecov.io/gh/mie-lab/trackintel/pull/408?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mie-lab) (9bd20bb) into [master](https://codecov.io/gh/mie-lab/trackintel/commit/5dee60a7378fc04ebbdbc92d1000fb106a945af8?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mie-lab) (5dee60a) will **increase** coverage by `0.28%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##           master     #408      +/-   ##
==========================================
+ Coverage   96.71%   96.99%   +0.28%     
==========================================
  Files          39       39              
  Lines        1888     1865      -23     
  Branches      283      275       -8     
==========================================
- Hits         1826     1809      -17     
+ Misses         27       24       -3     
+ Partials       35       32       -3     
```


| [Impacted Files](https://codecov.io/gh/mie-lab/trackintel/pull/408?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mie-lab) | Coverage Δ | |
|---|---|---|
| [trackintel/analysis/location\_identification.py](https://codecov.io/gh/mie-lab/trackintel/pull/408/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mie-lab#diff-dHJhY2tpbnRlbC9hbmFseXNpcy9sb2NhdGlvbl9pZGVudGlmaWNhdGlvbi5weQ==) | `99.06% <100.00%> (ø)` | |
| [trackintel/geogr/distances.py](https://codecov.io/gh/mie-lab/trackintel/pull/408/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mie-lab#diff-dHJhY2tpbnRlbC9nZW9nci9kaXN0YW5jZXMucHk=) | `94.56% <0.00%> (+1.36%)` | :arrow_up: |
| [trackintel/preprocessing/util.py](https://codecov.io/gh/mie-lab/trackintel/pull/408/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mie-lab#diff-dHJhY2tpbnRlbC9wcmVwcm9jZXNzaW5nL3V0aWwucHk=) | `100.00% <0.00%> (+12.50%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/mie-lab/trackintel/pull/408?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mie-lab).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mie-lab)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/mie-lab/trackintel/pull/408?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mie-lab). Last update [5dee60a...9bd20bb](https://codecov.io/gh/mie-lab/trackintel/pull/408?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mie-lab). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mie-lab).


Patch:
diff --git a/trackintel/analysis/location_identification.py b/trackintel/analysis/location_identification.py
index 33e691c..6682e4a 100644
--- a/trackintel/analysis/location_identification.py
+++ b/trackintel/analysis/location_identification.py
@@ -62,7 +62,7 @@ def location_identifier(staypoints, method="FREQ", pre_filter=True, **pre_filter
     if pre_filter:
         f = pre_filter_locations(sp, **pre_filter_kwargs)
     else:
-        f = pd.Series(np.full(len(sp.index), True))
+        f = pd.Series(np.full(len(sp.index), True), index=sp.index)
 
     if method == "FREQ":
         method_val = freq_method(sp[f], "home", "work")


Test patch:
diff --git a/tests/analysis/test_location_identification.py b/tests/analysis/test_location_identification.py
index a256ca5..af0917c 100644
--- a/tests/analysis/test_location_identification.py
+++ b/tests/analysis/test_location_identification.py
@@ -253,6 +253,15 @@ class TestLocation_Identifier:
         osna = osna_method(example_osna)
         assert_geodataframe_equal(li, osna)
 
+    def test_pre_filter_index(self, example_freq):
+        """Test if pre_filter=False works with non-serial index"""
+        # issue-#403
+        example_freq.index = *reversed(example_freq.index[1:]), example_freq.index[0]
+        example_freq.index += 100  # move it so that there is no overlap to a range index
+        li = location_identifier(example_freq, method="FREQ", pre_filter=False)
+        fr = freq_method(example_freq)
+        assert_geodataframe_equal(li, fr)
+
 
 @pytest.fixture
 def example_osna():