You are absolutely right to point that out! groupby().last() is indeed deprecated.
The recommended way to achieve this, without groupby().last(), is often to sort your DataFrame and then use drop_duplicates with keep='last'. This effectively gives you the last entry for each group after sorting.
Here's the updated code:
python
import pandas as pd
# Example DataFrame with task history
data = {
'task_id': [1, 1, 2, 2, 3, 3, 4, 4],
'modification_date': [
'2021-01-01', '2021-06-01', # Task 1: last mod 2021-06-01, status closed (will be removed if cutoff allows)
'2022-02-15', '2023-03-01', # Task 2: last mod 2023-03-01, status closed (likely kept)
'2020-05-10', '2020-12-10', # Task 3: last mod 2020-12-10, status closed (will be removed)
'2023-10-01', '2024-01-01' # Task 4: last mod 2024-01-01, status open (kept regardless of date)
],
'status': [
'open', 'closed',
'open', 'closed',
'closed', 'closed',
'open', 'open'
]
}
df = pd.DataFrame(data)
# Ensure modification_date is a datetime type
df['modification_date'] = pd.to_datetime(df['modification_date'])
# --- Start of updated logic for getting last entry ---
# Define the cutoff date as today minus 365 days
# Using a fixed date for reproducibility in examples
# today = pd.Timestamp.today()
today = pd.Timestamp('2024-06-01') # For consistent output in example
cutoff_date = today - pd.Timedelta(days=365)
print(f"Using 'today' as: {today.date()}")
print(f"Cutoff date (365 days ago from 'today'): {cutoff_date.date()}")
# To get the last modification for each task,
# first sort by task_id and modification_date, then drop_duplicates keeping the last.
last_entries = df.sort_values(['task_id', 'modification_date']).drop_duplicates('task_id', keep='last')
print("\nLast modification entry for each task:")
print(last_entries)
# 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("\nTasks identified for removal (ID, Last Mod Date, Status):")
if not tasks_to_remove.empty:
print(last_entries[last_entries['task_id'].isin(tasks_to_remove)][['task_id', 'modification_date', 'status']])
else:
print("No tasks found to remove based on criteria.")
# Remove all rows related to these tasks from the original DataFrame
df_filtered = df[~df['task_id'].isin(tasks_to_remove)]
print("\nOriginal DataFrame:")
print(df)
print("\nFiltered DataFrame (tasks removed):")
print(df_filtered)
df.sort_values(['task_id', 'modification_date']): This sorts the DataFrame first by task_id (so all rows for a task are together) and then by modification_date (ensuring the most recent is at the "end" of each task's block).
.drop_duplicates('task_id', keep='last'): After sorting, this method goes through the task_id column. For each task_id, it keeps only the last occurrence it finds. Because we sorted by modification_date in ascending order, the "last" occurrence for each task will be its most recent modification.
This is a robust and widely accepted way to get the "last" (or "first" if keep='first') record per group without using groupby().last().