Skip to content

Commit

Permalink
added pandas .ffill() as precursor to pct_change() in .value_to_ret()…
Browse files Browse the repository at this point in the history
… method.
  • Loading branch information
karrmagadgeteer2 committed Sep 26, 2024
1 parent 89c3d0a commit e8c301d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion openseries/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def value_to_ret(self: Self) -> Self:
returns.iloc[0] = 0
new_labels = [ValueType.RTRN] * self.item_count
arrays = [self.tsdf.columns.get_level_values(0), new_labels]
returns.columns = MultiIndex.from_arrays(arrays)
returns.columns = MultiIndex.from_arrays(arrays=arrays)
self.tsdf = returns.copy()
return self

Expand Down
16 changes: 8 additions & 8 deletions openseries/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ def from_fixed_rate(
- cast(DatetimeIndex, d_range)[:-1]
],
)
# noinspection PyTypeChecker
arr = list(cumprod(insert(1 + deltas * rate / 365, 0, 1.0)))
dates = [d.strftime("%Y-%m-%d") for d in cast(DatetimeIndex, d_range)]

Expand Down Expand Up @@ -434,15 +435,12 @@ def value_to_ret(self: Self) -> Self:
The returns of the values in the series
"""
self.tsdf = self.tsdf.pct_change()
self.tsdf.iloc[0] = 0
returns = self.tsdf.ffill().pct_change()
returns.iloc[0] = 0
self.valuetype = ValueType.RTRN
self.tsdf.columns = MultiIndex.from_arrays(
[
[self.label],
[self.valuetype],
],
)
arrays = [[self.label], [self.valuetype]]
returns.columns = MultiIndex.from_arrays(arrays=arrays)
self.tsdf = returns.copy()
return self

def value_to_diff(self: Self, periods: int = 1) -> Self:
Expand Down Expand Up @@ -520,6 +518,7 @@ def from_1d_rate_to_cumret(
arr = array(self.values) / divider

deltas = array([i.days for i in self.tsdf.index[1:] - self.tsdf.index[:-1]])
# noinspection PyTypeChecker
arr = cumprod(insert(1.0 + deltas * arr[:-1] / days_in_year, 0, 1.0))

self.dates = [d.strftime("%Y-%m-%d") for d in self.tsdf.index]
Expand Down Expand Up @@ -818,6 +817,7 @@ def timeseries_chain(

dates.extend([x.strftime("%Y-%m-%d") for x in new.tsdf.index])

# noinspection PyUnresolvedReferences
if back.__class__.__subclasscheck__(
OpenTimeSeries,
):
Expand Down

0 comments on commit e8c301d

Please sign in to comment.