LinkedinMonitor / analytics_data_processing.py
GuglielmoTor's picture
Update analytics_data_processing.py
58eb0f9 verified
raw
history blame
5.64 kB
import pandas as pd
from datetime import datetime, timedelta, time # Added time for min.time
import logging
# Configure logging for this module
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(module)s - %(message)s')
def filter_dataframe_by_date(df, date_column, start_date, end_date):
"""Filters a DataFrame by a date column within a given date range."""
if df is None or df.empty or not date_column:
logging.warning(f"Filter by date: DataFrame is None, empty, or no date_column provided. DF: {df is not None}, empty: {df.empty if df is not None else 'N/A'}, date_column: {date_column}")
return pd.DataFrame()
if date_column not in df.columns:
logging.warning(f"Filter by date: Date column '{date_column}' not found in DataFrame columns: {df.columns.tolist()}.")
return pd.DataFrame()
df_copy = df.copy() # Work on a copy to avoid SettingWithCopyWarning
try:
# Convert the DataFrame's date column to pandas datetime objects first
if not pd.api.types.is_datetime64_any_dtype(df_copy[date_column]):
df_copy[date_column] = pd.to_datetime(df_copy[date_column], errors='coerce')
# Normalize the DataFrame's date column to midnight (date part only)
df_copy[date_column] = df_copy[date_column].dt.normalize()
except Exception as e:
logging.error(f"Error converting or normalizing date column '{date_column}' to datetime: {e}")
return pd.DataFrame() # Return empty if conversion fails
df_filtered = df_copy.dropna(subset=[date_column])
if df_filtered.empty:
logging.info(f"Filter by date: DataFrame became empty after dropping NaNs in date column '{date_column}'.")
return pd.DataFrame()
# Convert start_date and end_date (which are expected to be datetime.datetime or None)
# to pandas Timestamps and normalize them for comparison
start_dt_obj = pd.to_datetime(start_date, errors='coerce').normalize() if start_date else None
end_dt_obj = pd.to_datetime(end_date, errors='coerce').normalize() if end_date else None
if start_dt_obj and end_dt_obj:
return df_filtered[(df_filtered[date_column] >= start_dt_obj) & (df_filtered[date_column] <= end_dt_obj)]
elif start_dt_obj:
return df_filtered[df_filtered[date_column] >= start_dt_obj]
elif end_dt_obj:
return df_filtered[df_filtered[date_column] <= end_dt_obj]
return df_filtered # No date filtering if neither start_date nor end_date is provided
def prepare_filtered_analytics_data(token_state_value, date_filter_option, custom_start_date, custom_end_date):
"""
Retrieves data from token_state, determines date range, filters posts and mentions.
Returns filtered_posts_df, filtered_mentions_df, follower_stats_df (unfiltered),
and the determined start_dt, end_dt for messaging.
"""
logging.info(f"Preparing filtered analytics data. Filter: {date_filter_option}, Custom Start: {custom_start_date}, Custom End: {custom_end_date}")
posts_df = token_state_value.get("bubble_posts_df", pd.DataFrame())
mentions_df = token_state_value.get("bubble_mentions_df", pd.DataFrame())
follower_stats_df = token_state_value.get("bubble_follower_stats_df", pd.DataFrame())
date_column_posts = token_state_value.get("config_date_col_posts", "published_at")
date_column_mentions = token_state_value.get("config_date_col_mentions", "date")
# Determine date range for filtering posts and mentions
# Normalize current time to midnight using datetime.replace
current_datetime_obj = datetime.now()
current_time_normalized = current_datetime_obj.replace(hour=0, minute=0, second=0, microsecond=0)
end_dt_filter = current_time_normalized
start_dt_filter = None
if date_filter_option == "Last 7 Days":
start_dt_filter = current_time_normalized - timedelta(days=6)
elif date_filter_option == "Last 30 Days":
start_dt_filter = current_time_normalized - timedelta(days=29)
elif date_filter_option == "Custom Range":
# custom_start_date and custom_end_date are strings from gr.DateTime(type="string")
# Convert to datetime objects and then normalize
start_dt_filter_temp = pd.to_datetime(custom_start_date, errors='coerce')
start_dt_filter = start_dt_filter_temp.replace(hour=0, minute=0, second=0, microsecond=0) if pd.notna(start_dt_filter_temp) else None
end_dt_filter_temp = pd.to_datetime(custom_end_date, errors='coerce')
# If custom_end_date is not provided or invalid, use current_time_normalized
end_dt_filter = end_dt_filter_temp.replace(hour=0, minute=0, second=0, microsecond=0) if pd.notna(end_dt_filter_temp) else current_time_normalized
# "All Time" means start_dt_filter remains None, end_dt_filter effectively means up to now.
logging.info(f"Date range for filtering: Start: {start_dt_filter}, End: {end_dt_filter}")
# Filter DataFrames
filtered_posts_data = pd.DataFrame()
if not posts_df.empty:
filtered_posts_data = filter_dataframe_by_date(posts_df, date_column_posts, start_dt_filter, end_dt_filter)
filtered_mentions_data = pd.DataFrame()
if not mentions_df.empty:
filtered_mentions_data = filter_dataframe_by_date(mentions_df, date_column_mentions, start_dt_filter, end_dt_filter)
logging.info(f"Processed - Filtered posts: {len(filtered_posts_data)} rows, Filtered Mentions: {len(filtered_mentions_data)} rows.")
return filtered_posts_data, filtered_mentions_data, follower_stats_df, start_dt_filter, end_dt_filter