Skip to content

Commit 5b96979

Browse files
committed
Customization for pandas dataframe.
1 parent 2d32c46 commit 5b96979

25 files changed

+8624
-476
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/FinMrkt.iml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sonarlint/issuestore/index.pb

Whitespace-only changes.

.idea/vagrant.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/.DS_Store

0 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 5
6+
}

src/.ipynb_checkpoints/calulateRisk-checkpoint.ipynb

+1,097
Large diffs are not rendered by default.

src/.ipynb_checkpoints/dfMethod-checkpoint.ipynb

+1,645
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 5
6+
}

src/.ipynb_checkpoints/returnfit-checkpoint.ipynb

+288-233
Large diffs are not rendered by default.

src/__pycache__/base.cpython-37.pyc

3.68 KB
Binary file not shown.

src/base.py

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# !/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
__coverage__ = 0.0
4+
__author__ = "Bruce_H_Cottman"
5+
__license__ = "MIT License"
6+
import warnings
7+
8+
warnings.filterwarnings("ignore")
9+
10+
import math
11+
from pandas import to_datetime
12+
from statistics import stdev
13+
from pandas_datareader.data import DataReader
14+
15+
16+
### pasoDecorators class
17+
# adapted from pandas-flavor 11/13/2019
18+
from pandas.api.extensions import register_dataframe_accessor
19+
from functools import wraps
20+
21+
from pandas.core.dtypes.generic import ABCDataFrame
22+
def register_DataFrame_method(method):
23+
"""Register a function as a method attached to the Pandas DataFrame.
24+
Example
25+
-------
26+
for a function
27+
@pf.register_dataframe_method
28+
def row_by_value(df, col, value):
29+
return df[df[col] == value].squeeze()
30+
31+
for a class method
32+
@pf.register_dataframe_accessor('Aclass')
33+
class Aclass(object):
34+
35+
def __init__(self, data):
36+
self._data
37+
38+
def row_by_value(self, col, value):
39+
return self._data[self._data[col] == value].squeeze()
40+
"""
41+
42+
def inner(*args, **kwargs):
43+
class AccessorMethod(object):
44+
def __init__(self, pandas_obj):
45+
self._obj = pandas_obj
46+
47+
@wraps(method)
48+
def __call__(self, *args, **kwargs):
49+
return method(self._obj, *args, **kwargs)
50+
51+
register_dataframe_accessor(method.__name__)(AccessorMethod)
52+
return method
53+
54+
return inner()
55+
56+
57+
def FutureValue_(principle=0, deposit=0, withdrawal=0, return_=0, nperiod=1):
58+
if (principle + deposit + withdrawal) == 0:
59+
return 0
60+
if return_ <= 0:
61+
return principle + deposit + withdrawal # default is end of period
62+
amount = deposit - withdrawal
63+
cumamount = 0
64+
for n in range(1, nperiod + 1):
65+
# print( 'before cumamount',cumamount)
66+
cumamount = cumamount + deposit * (1 + return_) ** n
67+
# print( 'after cumamount',cumamount)
68+
return principle * (1 + return_ ** nperiod) + cumamount
69+
70+
71+
def nppy_(EFT_df):
72+
t_dayspy = 252
73+
t_weekspy = 53
74+
t_monthsspy = 12
75+
t_ypy = 1
76+
77+
nlen = len(EFT_df.index.day)
78+
ndays = ((EFT_df.index[-1] - EFT_df.index[0])).days + 1
79+
nweeks = ndays // 7
80+
nmonths = ndays // 30.5
81+
if ndays >= nlen:
82+
return t_dayspy
83+
elif ndays >= nlen:
84+
return t_monthsspy
85+
elif nlen >= nmonths:
86+
return t_monthsspy
87+
elif nlen >= nweeks:
88+
return t_weekspy
89+
else:
90+
return t_ypy
91+
92+
93+
def p_or_r_std(
94+
asset_value_type, EFT_df, column_name, ticker, rd_type, start_date, end_date
95+
):
96+
EFT_df = DataReader(ticker, rd_type, start=start_date, end=end_date)
97+
EFT_df.index = to_datetime(EFT_df.index, format="%Y-%m-%d")
98+
nppy = nppy_(EFT_df)
99+
if asset_value_type.lower() == "price":
100+
101+
ave_price = EFT_df[column_name].mean()
102+
return (ave_price * nppy, statistics.stdev(EFT_df[column_name]))
103+
else:
104+
EFT_df["return"] = (
105+
EFT_df[column_name] - EFT_df[column_name].shift(1)
106+
) / EFT_df[column_name].shift(1)
107+
108+
ave_return = (EFT_df["return"]).mean(skipna=True)
109+
110+
return (ave_return * nppy, EFT_df["return"].std(skipna=True))
111+
112+
113+
def return_fit(nperiod: int = 1, principle: float = 0.0, return_: float = 0.0) -> float:
114+
if (principle) == 0:
115+
return 0
116+
if return_ <= 0:
117+
return principle # default is end of period
118+
return principle * (1 + return_) ** nperiod
119+
120+
121+
def price_std(EFT_df, column_name, ticker, rd_type, start_date, end_date):
122+
123+
EFT_df = DataReader(ticker, rd_type, start=start_date, end=end_date)
124+
EFT_df.index = to_datetime(EFT_df.index, format="%Y-%m-%d")
125+
126+
return stdev(EFT_df[column_name])

src/base.pyx

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cpdef int count_primes(int limit):
2+
cdef int count = 0
3+
cdef int candidate_int
4+
cdef int factor
5+
for candidate_int in range(limit):
6+
if (candidate_int > 1):
7+
for factor in range(2, candidate_int):
8+
if (candidate_int % factor == 0):
9+
break
10+
else:
11+
count += 1
12+
return count

0 commit comments

Comments
 (0)