本文整理汇总了Python中pandas.Series方法的典型用法代码示例。如果您正苦于以下问题:Python pandas.Series方法的具体用法?Python pandas.Series怎么用?Python pandas.Series使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas
的用法示例。
在下文中一共展示了pandas.Series方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: average_true_range
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def average_true_range(df, n):
"""
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
i = 0
TR_l = [0]
while i < df.index[-1]:
TR = max(df.loc[i + 1, 'High'], df.loc[i, 'Close']) - min(df.loc[i + 1, 'Low'], df.loc[i, 'Close'])
TR_l.append(TR)
i = i + 1
TR_s = pd.Series(TR_l)
ATR = pd.Series(TR_s.ewm(span=n, min_periods=n).mean(), name='ATR_' + str(n))
df = df.join(ATR)
return df
示例2: create_scats
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def create_scats(self, varieties):
self.scats = pd.DataFrame(columns=["x", "y", "color", "marker", "var"])
for i, var in enumerate(varieties):
self.legend.append(var)
(x_array, y_array) = self.get_arrays(varieties, var)
if len(x_array) <= 0: # no data to graph!
'''
I am creating a single "position" for an agent that cannot
be seen. This seems to fix the issue of colors being
missmatched in the occasion that a group has no agents.
'''
x_array = [-1]
y_array = [-1]
elif len(x_array) != len(y_array):
logging.debug("Array length mismatch in scatter plot")
return
color = get_color(varieties[var], i)
marker = get_marker(varieties[var], i)
scat = pd.DataFrame({"x": pd.Series(x_array),
"y": pd.Series(y_array),
"color": color,
"marker": marker,
"var": var})
self.scats = self.scats.append(scat, ignore_index=True,
sort=False)
示例3: get_topn_topm
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def get_topn_topm(self, s1, s2, n=10, m=3):
s1_sorted=s1.sort_values(ascending=False)
s1topn_index=s1_sorted.index[:n]
d=dict()
for i in s1topn_index:
d[i[:-3]]=s2[i[:-3]+'wn']
s=pd.Series(d)
s_sorted=s.sort_values(ascending=False)
l=len(s_sorted[s_sorted!=0])
if(l==0):
index=[]
for i in range(m):
index.append(s1topn_index[i][:-3])
return index
elif(l<m):
return s_sorted.index[:l]
else:
return s_sorted.index[:m]
示例4: ppsr
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def ppsr(df):
"""Calculate Pivot Points, Supports and Resistances for given data
:param df: pandas.DataFrame
:return: pandas.DataFrame
"""
PP = pd.Series((df['High'] + df['Low'] + df['Close']) / 3)
R1 = pd.Series(2 * PP - df['Low'])
S1 = pd.Series(2 * PP - df['High'])
R2 = pd.Series(PP + df['High'] - df['Low'])
S2 = pd.Series(PP - df['High'] + df['Low'])
R3 = pd.Series(df['High'] + 2 * (PP - df['Low']))
S3 = pd.Series(df['Low'] - 2 * (df['High'] - PP))
psr = {'PP': PP, 'R1': R1, 'S1': S1, 'R2': R2, 'S2': S2, 'R3': R3, 'S3': S3}
PSR = pd.DataFrame(psr)
df = df.join(PSR)
return df
示例5: trix
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def trix(df, n):
"""Calculate TRIX for given data.
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
EX1 = df['Close'].ewm(span=n, min_periods=n).mean()
EX2 = EX1.ewm(span=n, min_periods=n).mean()
EX3 = EX2.ewm(span=n, min_periods=n).mean()
i = 0
ROC_l = [np.nan]
while i + 1 <= df.index[-1]:
ROC = (EX3[i + 1] - EX3[i]) / EX3[i]
ROC_l.append(ROC)
i = i + 1
Trix = pd.Series(ROC_l, name='Trix_' + str(n))
df = df.join(Trix)
return df
示例6: vortex_indicator
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def vortex_indicator(df, n):
"""Calculate the Vortex Indicator for given data.
Vortex Indicator described here:
http://www.vortexindicator.com/VFX_VORTEX.PDF
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
i = 0
TR = [0]
while i < df.index[-1]:
Range = max(df.loc[i + 1, 'High'], df.loc[i, 'Close']) - min(df.loc[i + 1, 'Low'], df.loc[i, 'Close'])
TR.append(Range)
i = i + 1
i = 0
VM = [0]
while i < df.index[-1]:
Range = abs(df.loc[i + 1, 'High'] - df.loc[i, 'Low']) - abs(df.loc[i + 1, 'Low'] - df.loc[i, 'High'])
VM.append(Range)
i = i + 1
VI = pd.Series(pd.Series(VM).rolling(n).sum() / pd.Series(TR).rolling(n).sum(), name='Vortex_' + str(n))
df = df.join(VI)
return df
示例7: true_strength_index
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def true_strength_index(df, r, s):
"""Calculate True Strength Index (TSI) for given data.
:param df: pandas.DataFrame
:param r:
:param s:
:return: pandas.DataFrame
"""
M = pd.Series(df['Close'].diff(1))
aM = abs(M)
EMA1 = pd.Series(M.ewm(span=r, min_periods=r).mean())
aEMA1 = pd.Series(aM.ewm(span=r, min_periods=r).mean())
EMA2 = pd.Series(EMA1.ewm(span=s, min_periods=s).mean())
aEMA2 = pd.Series(aEMA1.ewm(span=s, min_periods=s).mean())
TSI = pd.Series(EMA2 / aEMA2, name='TSI_' + str(r) + '_' + str(s))
df = df.join(TSI)
return df
示例8: money_flow_index
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def money_flow_index(df, n):
"""Calculate Money Flow Index and Ratio for given data.
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
PP = (df['High'] + df['Low'] + df['Close']) / 3
i = 0
PosMF = [0]
while i < df.index[-1]:
if PP[i + 1] > PP[i]:
PosMF.append(PP[i + 1] * df.loc[i + 1, 'Volume'])
else:
PosMF.append(0)
i = i + 1
PosMF = pd.Series(PosMF)
TotMF = PP * df['Volume']
MFR = pd.Series(PosMF / TotMF)
MFI = pd.Series(MFR.rolling(n, min_periods=n).mean(), name='MFI_' + str(n))
df = df.join(MFI)
return df
示例9: on_balance_volume
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def on_balance_volume(df, n):
"""Calculate On-Balance Volume for given data.
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
i = 0
OBV = [0]
while i < df.index[-1]:
if df.loc[i + 1, 'Close'] - df.loc[i, 'Close'] > 0:
OBV.append(df.loc[i + 1, 'Volume'])
if df.loc[i + 1, 'Close'] - df.loc[i, 'Close'] == 0:
OBV.append(0)
if df.loc[i + 1, 'Close'] - df.loc[i, 'Close'] < 0:
OBV.append(-df.loc[i + 1, 'Volume'])
i = i + 1
OBV = pd.Series(OBV)
OBV_ma = pd.Series(OBV.rolling(n, min_periods=n).mean(), name='OBV_' + str(n))
df = df.join(OBV_ma)
return df
示例10: coppock_curve
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def coppock_curve(df, n):
"""Calculate Coppock Curve for given data.
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
M = df['Close'].diff(int(n * 11 / 10) - 1)
N = df['Close'].shift(int(n * 11 / 10) - 1)
ROC1 = M / N
M = df['Close'].diff(int(n * 14 / 10) - 1)
N = df['Close'].shift(int(n * 14 / 10) - 1)
ROC2 = M / N
Copp = pd.Series((ROC1 + ROC2).ewm(span=n, min_periods=n).mean(), name='Copp_' + str(n))
df = df.join(Copp)
return df
示例11: keltner_channel
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def keltner_channel(df, n):
"""Calculate Keltner Channel for given data.
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
KelChM = pd.Series(((df['High'] + df['Low'] + df['Close']) / 3).rolling(n, min_periods=n).mean(),
name='KelChM_' + str(n))
KelChU = pd.Series(((4 * df['High'] - 2 * df['Low'] + df['Close']) / 3).rolling(n, min_periods=n).mean(),
name='KelChU_' + str(n))
KelChD = pd.Series(((-2 * df['High'] + 4 * df['Low'] + df['Close']) / 3).rolling(n, min_periods=n).mean(),
name='KelChD_' + str(n))
df = df.join(KelChM)
df = df.join(KelChU)
df = df.join(KelChD)
return df
示例12: ultimate_oscillator
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def ultimate_oscillator(df):
"""Calculate Ultimate Oscillator for given data.
:param df: pandas.DataFrame
:return: pandas.DataFrame
"""
i = 0
TR_l = [0]
BP_l = [0]
while i < df.index[-1]:
TR = max(df.loc[i + 1, 'High'], df.loc[i, 'Close']) - min(df.loc[i + 1, 'Low'], df.loc[i, 'Close'])
TR_l.append(TR)
BP = df.loc[i + 1, 'Close'] - min(df.loc[i + 1, 'Low'], df.loc[i, 'Close'])
BP_l.append(BP)
i = i + 1
UltO = pd.Series((4 * pd.Series(BP_l).rolling(7).sum() / pd.Series(TR_l).rolling(7).sum()) + (
2 * pd.Series(BP_l).rolling(14).sum() / pd.Series(TR_l).rolling(14).sum()) + (
pd.Series(BP_l).rolling(28).sum() / pd.Series(TR_l).rolling(28).sum()),
name='Ultimate_Osc')
df = df.join(UltO)
return df
示例13: donchian_channel
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def donchian_channel(df, n):
"""Calculate donchian channel of given pandas data frame.
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
i = 0
dc_l = []
while i < n - 1:
dc_l.append(0)
i += 1
i = 0
while i + n - 1 < df.index[-1]:
dc = max(df['High'].ix[i:i + n - 1]) - min(df['Low'].ix[i:i + n - 1])
dc_l.append(dc)
i += 1
donchian_chan = pd.Series(dc_l, name='Donchian_' + str(n))
donchian_chan = donchian_chan.shift(n - 1)
return df.join(donchian_chan)
示例14: _get_target_encoder
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def _get_target_encoder(self, x, y):
"""Return a mapping from categories to average target values.
Args:
x (pandas.Series): a categorical column to encode.
y (pandas.Series): the target column
Returns:
(dict): mapping from categories to average target values
"""
assert len(x) == len(y)
# NaN cannot be used as a key for dict. So replace it with a random
# integer
mean_count = pd.DataFrame({y.name: y, x.name: x.fillna(NAN_INT)}).groupby(x.name)[y.name].agg(['mean', 'count'])
smoothing = 1 / (1 + np.exp(-(mean_count['count'] - self.min_samples) / self.smoothing))
mean_count[y.name] = self.target_mean * (1 - smoothing) + mean_count['mean'] * smoothing
return mean_count[y.name].to_dict()
示例15: fit
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Series [as 别名]
def fit(self, X, y):
"""Encode categorical columns into average target values.
Args:
X (pandas.DataFrame): categorical columns to encode
y (pandas.Series): the target column
Returns:
(pandas.DataFrame): encoded columns
"""
self.target_encoders = [None] * X.shape[1]
self.target_mean = y.mean()
for i, col in enumerate(X.columns):
if self.cv is None:
self.target_encoders[i] = self._get_target_encoder(X[col], y)
else:
self.target_encoders[i] = []
for i_cv, (i_trn, i_val) in enumerate(self.cv.split(X[col], y), 1):
self.target_encoders[i].append(self._get_target_encoder(X.loc[i_trn, col], y[i_trn]))
return self