Amartin
Amartin

Reputation: 337

filtering a list of lists of dates in python

I have the following list of list:

[[Timestamp('2018-04-07 00:00:00'), Timestamp('2018-07-07 00:00:00'),Timestamp('2020-04-07 00:00:00')], [Timestamp('2018-04-07 00:00:00'),Timestamp('2018-10-07 00:00:00'),Timestamp('2018-12-07 00:00:00'),Timestamp('2021-04-07 00:00:00'),Timestamp('2022-04-07 00:00:00'),Timestamp('2023-04-07 00:00:00')]

And my question is if it is possible filter the list of lists by another date. For example: If I choose this date:

datetime.datetime(2019, 1, 26, 0, 0)

Is there a way to filter the list of lists to just taking those dates that are higher than the date I choose and also the last one before my date? For example for the first list of the lists I want to keep the values:

[Timestamp('2018-07-07 00:00:00'),Timestamp('2020-04-07 00:00:00')]

And for the second list of the list of lists:

[Timestamp('2018-12-07 00:00:00'),Timestamp('2021-04-07 00:00:00'),Timestamp('2022-04-07 00:00:00'),Timestamp('2023-04-07 00:00:00')]   

Upvotes: 2

Views: 2013

Answers (1)

Teemu
Teemu

Reputation: 286

Something like this?

import datetime
from pandas import Timestamp


# List of Timestamps
list_of_dates = [[Timestamp('2018-04-07 00:00:00'), Timestamp('2018-07-07 00:00:00'), Timestamp('2020-04-07 00:00:00')], [Timestamp('2018-04-07 00:00:00'), Timestamp('2018-10-07 00:00:00'), Timestamp('2018-12-07 00:00:00'), Timestamp('2021-04-07 00:00:00'), Timestamp('2022-04-07 00:00:00'), Timestamp('2023-04-07 00:00:00')]]

# Target date that we use as filter
target_date = datetime.datetime(2019, 1, 26, 0, 0)

def filter_dates(date_list, target_date):
  """Filter given timestamps according to target date.

  Keep last date before target date and all future dates after target date."""

  # Initialise return list
  filtered_dates = []

  # Iterate over list of lists
  for dates in date_list:

    # Use list comprehension to filter dates that are on either sides of the target date
    dates_before_target_date = [date for date in dates if date < target_date]
    dates_after_target_date = [date for date in dates if date > target_date]

    # Keep last date before the target date and all future dates
    filtered_dates.append([dates_before_target_date[-1]] + dates_after_target_date)

  return filtered_dates

filtered_dates = filter_dates(list_of_dates, target_date)
print(filtered_dates)

This produces

[
    [Timestamp('2018-07-07 00:00:00'), Timestamp('2020-04-07 00:00:00')],
    [Timestamp('2018-12-07 00:00:00'), Timestamp('2021-04-07 00:00:00'), Timestamp('2022-04-07 00:00:00'), Timestamp('2023-04-07 00:00:00')]
]

Upvotes: 1

Related Questions