Financial Data Science Financial Performance Analysis: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 22: Line 22:
         return int(s[1:].replace(',',''))
         return int(s[1:].replace(',',''))
     elif math.isnan(s):
     elif math.isnan(s):
         return 0
         return s # we will interpolate later
    else:
 
        return -1
# extract a specific time series ('Fidelity Self', 'Fidelity Managed', etc.)
fid_slf = df['Fidelity Self'].apply(dollar_to_int)
# resample and interpolate
fid_slf = fid_slf.resample('D').interpolate()


# extract a specific time series and plot it
fidelity_self = df['Fidelity Self'].apply(dollar_to_int)
fidelity_self = fidelity_self[fidelity_self != 0]
fidelity_managed = df['Fidelity Managed'].apply(dollar_to_int)
fidelity_managed = fidelity_managed[fidelity_managed != 0]


# get the SP&500
# get the SP&500

Revision as of 03:21, 21 October 2023

Internal

Overview

import math
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ptick
from fredapi import Fred

# load the DataFrame
df = pd.read_csv("./finances.csv", parse_dates=["Date"])

# make it a time series DataFrame
df = df.set_index('Date')

# declare the function that converts the dollar amount
def dollar_to_int(s):
    if isinstance(s, str):
        return int(s[1:].replace(',',''))
    elif math.isnan(s):
        return s # we will interpolate later

# extract a specific time series ('Fidelity Self', 'Fidelity Managed', etc.)
fid_slf = df['Fidelity Self'].apply(dollar_to_int)
# resample and interpolate
fid_slf = fid_slf.resample('D').interpolate()


# get the SP&500
fred = Fred(api_key='...')
sp500 = fred.get_series(series_id="SP500")

# graph
fig, ax = plt.subplots()
fig.autofmt_xdate()
ax.set_ylabel("amount")
ax.yaxis.set_major_formatter(mt.FormatStrFormatter('% 1.2f')) 
ax.plot(fidelity_self, lw=0.5)
ax.plot(fidelity_managed, lw=0.5)
plt.show()