title: How can only the latest values in time windows of a pandas DataFrame be selected?
body: <p>I have a DataFrame that should be split into time windows of 15 minutes, perhaps by labeling them using a new column that has a unique identifier for each window. Then I want to keep only the latest measurements of each time window. So, if there are two measurements of "b" within a time window, only the latest one is kept, while the earlier ones are removed.</p>
  
  <p>So, let's say we have the following data:</p>
  
  <pre><code>|**datetime**    |**measurement**|**value**|
  |----------------|---------------|---------|
  |2018-07-18T0642Z|"a"            |5347     |
  |2018-07-18T0641Z|"b"            |8345     |
  |2018-07-18T0640Z|"c"            |1256     |
  |2018-07-18T0630Z|"b"            |7485     |
  |2018-07-18T0629Z|"a"            |2536     |
  |2018-07-18T0629Z|"b"            |5943     |
  </code></pre>
  
  <p>As a DataFrame it is this:</p>
  
  <pre><code>import pandas as pd
  df = pd.DataFrame(
           [
               ["2018-07-18T0642Z", "a", 5347],
               ["2018-07-18T0641Z", "b", 8345],
               ["2018-07-18T0640Z", "c", 1256],
               ["2018-07-18T0630Z", "b", 7485],
               ["2018-07-18T0629Z", "a", 2536],
               ["2018-07-18T0629Z", "b", 5943],
           ],
           columns = [
               "datetime",
               "measurement",
               "value"
           ]
      )
  df["datetime"] = pd.to_datetime(df["datetime"])
  df.index       = df["datetime"]
  del df["datetime"]
  </code></pre>
  
  <p>With a column added to label the time windows, it becomes the following:</p>
  
  <pre><code>|**datetime**    |**measurement**|**value**|**time_window**|
  |----------------|---------------|---------|---------------|
  |2018-07-18T0642Z|"a"            |5347     |2              |
  |2018-07-18T0641Z|"b"            |8345     |2              |
  |2018-07-18T0640Z|"c"            |1256     |2              |
  |----------------|---------------|---------|---------------|
  |2018-07-18T0630Z|"b"            |7485     |1              |
  |2018-07-18T0629Z|"a"            |2536     |1              |
  |2018-07-18T0629Z|"b"            |5943     |1              |
  </code></pre>
  
  <p>Now, only the most recent measurements in each window are kept, meaning that the first "b" measurement of time window 1 is removed:</p>
  
  <pre><code>|**datetime**    |**measurement**|**value**|**time_window**|
  |----------------|---------------|---------|---------------|
  |2018-07-18T0642Z|"a"            |5347     |2              |
  |2018-07-18T0641Z|"b"            |8345     |2              |
  |2018-07-18T0640Z|"c"            |1256     |2              |
  |----------------|---------------|---------|---------------|
  |2018-07-18T0630Z|"b"            |7485     |1              |
  |2018-07-18T0629Z|"a"            |2536     |1              |
  </code></pre>
  
  <p>How could this be achieved in Pandas?</p>