{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#Importing necessary libraries\n", "from dotenv import load_dotenv\n", "from datetime import datetime, timedelta\n", "import requests\n", "import os\n", "import time\n", "import pandas as pd \n", "from textblob import TextBlob" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#Defining a function to process news articles\n", "def process_news_articles(news_articles):\n", " # Convert list of dictionaries to DataFrame\n", " df = pd.DataFrame(news_articles)\n", "\n", " # Drop rows where the description is NaN\n", " df = df.dropna(subset=['description'])\n", "\n", " # Fill missing 'amp_url' and 'keywords' with specific placeholders\n", " df['amp_url'] = df['amp_url'].fillna('No URL provided')\n", " df['keywords'] = df['keywords'].fillna('No keywords')\n", "\n", " # Sentiment analysis on descriptions\n", " df['sentiment'] = df['description'].apply(lambda text: TextBlob(text).sentiment.polarity)\n", "\n", " # Convert 'published_utc' to datetime and extract date and time\n", " df['published_utc'] = pd.to_datetime(df['published_utc'])\n", " df['date'] = df['published_utc'].dt.date\n", " df['time'] = df['published_utc'].dt.time\n", "\n", " # Dropping unnecessary columns\n", " df.drop(['published_utc'], axis=1, inplace=True)\n", " # set date to index\n", " df = df.set_index(\"date\")\n", " df.reset_index(inplace=True)\n", " df.index = pd.to_datetime(df.index)\n", " df = df.groupby(['date', 'ticker'])['sentiment'].mean().reset_index()\n", "\n", " return df" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "#Defining a function for the exponential moving average\n", "\n", "def exponential_moving_average(df, window):\n", " # Calculate EMA on the 'sentiment' column\n", " df[f'exp_mean_{window}_days'] = df['sentiment'].ewm(span=window, adjust=False).mean()\n", " return df" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }