Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for issue 732 Yearly pivots causes error with pandas 2.x #772

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions pandas_ta/overlap/pivots.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
from numpy import NaN, greater, zeros_like
from numba import njit
from pandas import DataFrame, Series, Timedelta, infer_freq, to_datetime
from pandas_ta._typing import Array, DictLike
from pandas import DataFrame, Series, infer_freq
from pandas_ta._typing import DictLike
from pandas_ta.utils import np_non_zero_range, v_datetime_ordered, v_series, v_str

import pandas as pd

@njit
def pivot_camarilla(high, low, close):
Expand Down Expand Up @@ -231,14 +231,25 @@ def pivots(
df[f"{_props}_R1"], df[f"{_props}_R2"] = r1, r2
df[f"{_props}_R3"], df[f"{_props}_R4"] = r3, r4

df.index = df.index + Timedelta(1, anchor.lower())
# Support for Pandas v1.4.x and v2.2.x
td_mapping = {
'Y': 'years',
'YE': 'years',
'M': 'months',
'ME': 'months',
'D': 'days',
}
time_unit = td_mapping.get(anchor.upper(), None)
if time_unit:
time_delta = pd.DateOffset(**{time_unit: 1})
df.index = df.index + time_delta
else:
print(f"[!] Unsupported time anchor {anchor}.")

if freq is not anchor:
df = df.reindex(dt_index, method="ffill")
df = df.iloc[:,4:]

# hm = df.loc[lambda x: (x.index.hour == 23) & (x.index.minute == 59)].iloc[0].name
# df.loc[:hm] = NaN

if method in ["demark", "fibonacci"]:
df.drop(columns=[x for x in df.columns if all(df[x].isna())], inplace=True)

Expand Down
Loading