Below is one way to do it. The idea is to:
- Determine the cutoff date: Calculate today's date (or the date of interest) minus 365 days.
- Identify the last modification for each task: Group by the task ID, sorting by modification date (assuming you have a column for that).
- Find tasks that meet the criteria: In the aggregated result, check for tasks whose last modification date is older than the cutoff and whose status is closed.
- Filter out those tasks from the original DataFrame.
Below is a sample code snippet that demonstrates the process. Adjust the column names ('task_id', 'modification_date', and 'status') as needed to match your DataFrame.
import pandas as pd
# Example DataFrame with task history
data = {
'task_id': [1, 1, 2, 2, 3, 3],
'modification_date': [
'2021-01-01', '2021-06-01', # Task 1 modifications
'2022-02-15', '2022-03-01', # Task 2 modifications
'2020-05-10', '2020-12-10' # Task 3 modifications
],
'status': [
'open', 'closed',
'open', 'closed',
'closed', 'closed'
]
}
df = pd.DataFrame(data)
# Ensure modification_date is a datetime type
df['modification_date'] = pd.to_datetime(df['modification_date'])
# Define the cutoff date as today minus 365 days
cutoff_date = pd.Timestamp.today() - pd.Timedelta(days=365)
print("Cutoff date:", cutoff_date)
# We need the last (most recent) modification for each task.
# Sorting by modification_date ensures that the last row per group is the most recent.
df_sorted = df.sort_values('modification_date')
# Get the last entry per task
last_entries = df_sorted.groupby('task_id').last().reset_index()
# Identify tasks that are closed and haven't been modified in more than 365 days
tasks_to_remove = last_entries[
(last_entries['status'].str.lower() == 'closed') &
(last_entries['modification_date'] < cutoff_date)
]['task_id']
print("Tasks to remove:", tasks_to_remove.tolist())
# Remove all rows related to these tasks from the original DataFrame
df_filtered = df[~df['task_id'].isin(tasks_to_remove)]
print("\nFiltered DataFrame:")
print(df_filtered)
Explanation
-
Convert Dates:
The modification dates are converted to datetime objects to allow proper comparisons.
-
Determining the Cutoff Date:
The variable cutoff_date is set to the current date minus 365 days. Any task whose most recent modification is before this date qualifies (if its last status is closed).
-
Finding the Last Modification per Task:
The DataFrame is first sorted by modification_date so that when we group by task_id and pick the last row, we get the most recent modification for each task.
-
Filtering the Tasks:
The code creates a mask: tasks whose last modification date is older than cutoff_date and whose status is 'closed'. It gathers these task IDs into tasks_to_remove.
-
Excluding Rows:
Finally, the original DataFrame is filtered to remove all rows belonging to these tasks.
This approach ensures that all history rows for any qualifying task are removed from your DataFrame. Adjust the column names and possibly date calculations if your data or requirements differ.