本文整理匯總了Python中pandas.core.frame.DataFrame.dropna方法的典型用法代碼示例。如果您正苦於以下問題:Python DataFrame.dropna方法的具體用法?Python DataFrame.dropna怎麽用?Python DataFrame.dropna使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas.core.frame.DataFrame
的用法示例。
在下文中一共展示了DataFrame.dropna方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _stack_multi_columns
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import dropna [as 別名]
def _stack_multi_columns(frame, level_num=-1, dropna=True):
def _convert_level_number(level_num, columns):
"""
Logic for converting the level number to something we can safely pass
to swaplevel:
We generally want to convert the level number into a level name, except
when columns do not have names, in which case we must leave as a level
number
"""
if level_num in columns.names:
return columns.names[level_num]
else:
if columns.names[level_num] is None:
return level_num
else:
return columns.names[level_num]
this = frame.copy()
# this makes life much simpler
if level_num != frame.columns.nlevels - 1:
# roll levels to put selected level at end
roll_columns = this.columns
for i in range(level_num, frame.columns.nlevels - 1):
# Need to check if the ints conflict with level names
lev1 = _convert_level_number(i, roll_columns)
lev2 = _convert_level_number(i + 1, roll_columns)
roll_columns = roll_columns.swaplevel(lev1, lev2)
this.columns = roll_columns
if not this.columns.is_lexsorted():
# Workaround the edge case where 0 is one of the column names,
# which interferes with trying to sort based on the first
# level
level_to_sort = _convert_level_number(0, this.columns)
this = this.sortlevel(level_to_sort, axis=1)
# tuple list excluding level for grouping columns
if len(frame.columns.levels) > 2:
tuples = list(zip(*[lev.take(lab) for lev, lab in zip(this.columns.levels[:-1], this.columns.labels[:-1])]))
unique_groups = [key for key, _ in itertools.groupby(tuples)]
new_names = this.columns.names[:-1]
new_columns = MultiIndex.from_tuples(unique_groups, names=new_names)
else:
new_columns = unique_groups = this.columns.levels[0]
# time to ravel the values
new_data = {}
level_vals = this.columns.levels[-1]
level_labels = sorted(set(this.columns.labels[-1]))
level_vals_used = level_vals[level_labels]
levsize = len(level_labels)
drop_cols = []
for key in unique_groups:
loc = this.columns.get_loc(key)
slice_len = loc.stop - loc.start
# can make more efficient?
if slice_len == 0:
drop_cols.append(key)
continue
elif slice_len != levsize:
chunk = this.ix[:, this.columns[loc]]
chunk.columns = level_vals.take(chunk.columns.labels[-1])
value_slice = chunk.reindex(columns=level_vals_used).values
else:
if frame._is_mixed_type:
value_slice = this.ix[:, this.columns[loc]].values
else:
value_slice = this.values[:, loc]
new_data[key] = value_slice.ravel()
if len(drop_cols) > 0:
new_columns = new_columns.difference(drop_cols)
N = len(this)
if isinstance(this.index, MultiIndex):
new_levels = list(this.index.levels)
new_names = list(this.index.names)
new_labels = [lab.repeat(levsize) for lab in this.index.labels]
else:
new_levels = [this.index]
new_labels = [np.arange(N).repeat(levsize)]
new_names = [this.index.name] # something better?
new_levels.append(frame.columns.levels[level_num])
new_labels.append(np.tile(level_labels, N))
new_names.append(frame.columns.names[level_num])
new_index = MultiIndex(levels=new_levels, labels=new_labels, names=new_names, verify_integrity=False)
result = DataFrame(new_data, index=new_index, columns=new_columns)
# more efficient way to go about this? can do the whole masking biz but
# will only save a small amount of time...
if dropna:
result = result.dropna(axis=0, how="all")
#.........這裏部分代碼省略.........
示例2: _stack_multi_columns
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import dropna [as 別名]
def _stack_multi_columns(frame, level=-1, dropna=True):
this = frame.copy()
# this makes life much simpler
if level != frame.columns.nlevels - 1:
# roll levels to put selected level at end
roll_columns = this.columns
for i in range(level, frame.columns.nlevels - 1):
roll_columns = roll_columns.swaplevel(i, i + 1)
this.columns = roll_columns
if not this.columns.is_lexsorted():
this = this.sortlevel(0, axis=1)
# tuple list excluding level for grouping columns
if len(frame.columns.levels) > 2:
tuples = zip(*[lev.values.take(lab)
for lev, lab in zip(this.columns.levels[:-1],
this.columns.labels[:-1])])
unique_groups = [key for key, _ in itertools.groupby(tuples)]
new_names = this.columns.names[:-1]
new_columns = MultiIndex.from_tuples(unique_groups, names=new_names)
else:
new_columns = unique_groups = this.columns.levels[0]
# time to ravel the values
new_data = {}
level_vals = this.columns.levels[-1]
levsize = len(level_vals)
drop_cols = []
for key in unique_groups:
loc = this.columns.get_loc(key)
slice_len = loc.stop - loc.start
# can make more efficient?
if slice_len == 0:
drop_cols.append(key)
continue
elif slice_len != levsize:
chunk = this.ix[:, this.columns[loc]]
chunk.columns = level_vals.take(chunk.columns.labels[-1])
value_slice = chunk.reindex(columns=level_vals).values
else:
if frame._is_mixed_type:
value_slice = this.ix[:, this.columns[loc]].values
else:
value_slice = this.values[:, loc]
new_data[key] = value_slice.ravel()
if len(drop_cols) > 0:
new_columns = new_columns - drop_cols
N = len(this)
if isinstance(this.index, MultiIndex):
new_levels = list(this.index.levels)
new_names = list(this.index.names)
new_labels = [lab.repeat(levsize) for lab in this.index.labels]
else:
new_levels = [this.index]
new_labels = [np.arange(N).repeat(levsize)]
new_names = [this.index.name] # something better?
new_levels.append(frame.columns.levels[level])
new_labels.append(np.tile(np.arange(levsize), N))
new_names.append(frame.columns.names[level])
new_index = MultiIndex(levels=new_levels, labels=new_labels,
names=new_names)
result = DataFrame(new_data, index=new_index, columns=new_columns)
# more efficient way to go about this? can do the whole masking biz but
# will only save a small amount of time...
if dropna:
result = result.dropna(axis=0, how='all')
return result
示例3: strat_maLong_maShort
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import dropna [as 別名]
def strat_maLong_maShort(
df=readYahoo("SPY"),
maLongDays=10,
maShortDays=3,
closeCol="Close",
highCol="High",
lowCol="Low",
openCol="Open",
signOfTrade=1,
printit=True,
block=False,
):
""" execute strategy which enters and exit based on Moving Average crossovers
Example:
from pystrats.state_strats import strat_maLong_maShort as ss
dfretfinal = ss() #strat_maLong_maShort()
print dfretfinal
print dfretfinal['ret'].mean()
"""
close = np.array(df[closeCol])
high = np.array(df[highCol])
low = np.array(df[lowCol])
open = np.array(df[openCol])
date = np.array(df["Date"])
ma10 = rolling_mean(close, maLongDays)
ma9 = rolling_mean(close, maLongDays - 1)
ma3 = rolling_mean(close, maShortDays)
ma2 = rolling_mean(close, maShortDays - 1)
n = len(df)
nl = n - 1
# pMa10 = dsInsert(ma10[0:nl],0,None)
# pMa9 = dsInsert(ma9[0:nl],0,None)
# pMa3 = dsInsert(ma3[0:nl],0,None)
# pMa2 = dsInsert(ma2[0:nl],0,None)
pMa10 = np.insert(ma10[0:nl], 0, None)
pMa9 = np.insert(ma9[0:nl], 0, None)
pMa3 = np.insert(ma3[0:nl], 0, None)
pMa2 = np.insert(ma2[0:nl], 0, None)
pClose = np.insert(close[0:nl], 0, None)
pHigh = np.insert(high[0:nl], 0, None)
pLow = np.insert(low[0:nl], 0, None)
# initialize state vector
state = np.array([1] * n)
# loop
start_i = maLongDays + 1
for i in range(start_i, n):
if (pClose[i] < pMa10[i]) & (state[i - 1] == 1) & (high[i] > pMa9[i]):
state[i] = 2
elif (state[i - 1] == 2) & (low[i] > pMa2[i]):
state[i] = 2
elif (state[i - 1] == 2) & (low[i] <= pMa2[i]):
state[i] = 1
pState = np.insert(state[0:nl], 0, 1)
# create entry conditions
# 1. initial entry (state 1 to state 2)
e1_2 = np.array((pState == 1) & (state == 2))
e2_2 = np.array((pState == 2) & (state == 2))
e2_1 = np.array((pState == 2) & (state == 1))
dfret = DataFrame([date, pHigh, pLow, pClose, pMa10, pMa9, pMa3, pMa2]).T
dfret.columns = ["Date", "pHigh", "pLow", "pClose", "pMa10", "pMa9", "pMa3", "pMa2"]
# create daily entry prices
dailyEntryPrices = np.array([0] * n)
# default entry
dailyEntryPrices = asb(dailyEntryPrices, pMa9, e1_2)
useCloseOnEntry = e1_2 & (low > pMa9)
dailyEntryPrices = asb(dailyEntryPrices, close, useCloseOnEntry)
dailyEntryPrices = asb(dailyEntryPrices, pClose, e2_2)
dailyEntryPrices = asb(dailyEntryPrices, pClose, e2_1)
dfret["entry"] = dailyEntryPrices
# create DAILY settle prices, which are either 0 or the Close
# dfret$Close <- close
dailySettlePrices = np.array([0] * n)
dailySettlePrices = asb(dailySettlePrices, close, e1_2) # <- close[w1_2]
dailySettlePrices = asb(dailySettlePrices, close, e2_2) # dailySettlePrices[w2_2] <- close[w2_2]
dailySettlePrices = asb(dailySettlePrices, pMa2, e2_1) # dailySettlePrices[w2_1] <- pMa2[w2_1]
# adjust for situations where the high is below the pMa2, so you get out at the close
useCloseOnExit = e2_1 & (high < pMa2)
dailySettlePrices = asb(
dailySettlePrices, close, useCloseOnExit
) # dailySettlePrices[useCloseOnExit] <- close[useCloseOnExit]
dfret["exit"] = dailySettlePrices
dfret["ret"] = dfret["exit"] / dfret["entry"] - 1
dfret["ret"].fillna(0)
dfretfinal = dfret.dropna(0) # dfretfinal <- dfret[-badrows(dfret),]
#.........這裏部分代碼省略.........