Skip to content

Instantly share code, notes, and snippets.

@Nempickaxe
Last active November 16, 2023 20:08
Show Gist options
  • Save Nempickaxe/5203c642fbccb8205cf4285b170f4581 to your computer and use it in GitHub Desktop.
Save Nempickaxe/5203c642fbccb8205cf4285b170f4581 to your computer and use it in GitHub Desktop.
Create a new column with the minimum difference (in days) between today's date and future dates.
import pandas as pd
from datetime import datetime
def min_date_difference(df, date_columns):
"""
Create a new column with the minimum difference (in days) between today's date and future dates.
Parameters:
- df: pandas DataFrame
- date_columns: list of str, column names containing datetime values
Returns:
- pandas DataFrame with a new column 'min_date_difference' in days
"""
today = datetime.today()
# Initialize the new column with None
df['min_date_difference'] = None
# Iterate through specified date columns
for column in date_columns:
future_dates = pd.to_datetime(df[column])
# Filter only future dates
future_dates = future_dates[future_dates > today]
if not future_dates.empty:
differences = (future_dates - today).dt.days
# Update 'min_date_difference' column with the minimum positive difference in days
min_positive_difference = differences[differences > 0].min()
if not pd.isnull(min_positive_difference):
idx_min_difference = differences.idxmin()
df.at[idx_min_difference, 'min_date_difference'] = min_positive_difference
return df
# Example usage
data = {'date1': ['2022-01-01', '2023-02-01', '2023-03-01'],
'date2': ['2022-04-01', '2024-05-01', '2023-06-01'],
'date3': ['2022-07-01', '2023-08-01', '2023-09-01']}
df = pd.DataFrame(data)
date_columns = ['date1', 'date2', 'date3']
df = min_date_difference(df, date_columns)
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment