本文整理匯總了Python中pandas.notna方法的典型用法代碼示例。如果您正苦於以下問題:Python pandas.notna方法的具體用法?Python pandas.notna怎麽用?Python pandas.notna使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas
的用法示例。
在下文中一共展示了pandas.notna方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _similarity_smooth
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def _similarity_smooth(presented, recalled, features, distance):
lists = presented.index.get_values()
res = np.empty((len(lists), len(features), recalled.iloc[0].shape[0], presented.iloc[0].shape[0]))*np.nan
for li, l in enumerate(lists):
p_list = presented.loc[l]
r_list = recalled.loc[l]
for i, feature in enumerate(features):
get_feature = lambda x: np.array(x[feature]) if np.array(pd.notna(x['item'])).any() else np.nan
p = np.vstack(p_list.apply(get_feature).get_values())
r = r_list.dropna().apply(get_feature).get_values()
r = np.vstack(list(filter(lambda x: x is not np.nan, r)))
tmp = 1 - cdist(r, p, distance)
res[li, i, :tmp.shape[0], :] = tmp
if distance == 'correlation':
return np.nanmean(res, 1)
else:
return np.mean(res, 1)
示例2: test_where_other
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def test_where_other(self):
# other is ndarray or Index
i = pd.date_range('20130101', periods=3, tz='US/Eastern')
for arr in [np.nan, pd.NaT]:
result = i.where(notna(i), other=np.nan)
expected = i
tm.assert_index_equal(result, expected)
i2 = i.copy()
i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist())
result = i.where(notna(i2), i2)
tm.assert_index_equal(result, i2)
i2 = i.copy()
i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist())
result = i.where(notna(i2), i2.values)
tm.assert_index_equal(result, i2)
示例3: test_basic
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def test_basic(self):
# array or list or dates
N = 50
rng = date_range('1/1/1990', periods=N, freq='53s')
ts = Series(np.random.randn(N), index=rng)
ts[15:30] = np.nan
dates = date_range('1/1/1990', periods=N * 3, freq='25s')
result = ts.asof(dates)
assert notna(result).all()
lb = ts.index[14]
ub = ts.index[30]
result = ts.asof(list(dates))
assert notna(result).all()
lb = ts.index[14]
ub = ts.index[30]
mask = (result.index >= lb) & (result.index < ub)
rs = result[mask]
assert (rs == ts[lb]).all()
val = result[result.index[result.index >= ub][0]]
assert ts[ub] == val
示例4: test_len
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def test_len(self):
values = Series(['foo', 'fooo', 'fooooo', np.nan, 'fooooooo'])
result = values.str.len()
exp = values.map(lambda x: len(x) if notna(x) else NA)
tm.assert_series_equal(result, exp)
# mixed
mixed = Series(['a_b', NA, 'asdf_cas_asdf', True, datetime.today(),
'foo', None, 1, 2.])
rs = Series(mixed).str.len()
xp = Series([3, NA, 13, NA, NA, 3, NA, NA, NA])
assert isinstance(rs, Series)
tm.assert_almost_equal(rs, xp)
# unicode
values = Series([u('foo'), u('fooo'), u('fooooo'), np.nan, u(
'fooooooo')])
result = values.str.len()
exp = values.map(lambda x: len(x) if notna(x) else NA)
tm.assert_series_equal(result, exp)
示例5: test_setitem
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def test_setitem(self):
df = self.df
idx = self.idx
# setitem
df['C'] = idx
assert_series_equal(df['C'], Series(idx, name='C'))
df['D'] = 'foo'
df['D'] = idx
assert_series_equal(df['D'], Series(idx, name='D'))
del df['D']
# With NaN: because uint64 has no NaN element,
# the column should be cast to object.
df2 = df.copy()
df2.iloc[1, 1] = pd.NaT
df2.iloc[1, 2] = pd.NaT
result = df2['B']
assert_series_equal(notna(result), Series(
[True, False, True], name='B'))
assert_series_equal(df2.dtypes, Series([np.dtype('uint64'),
np.dtype('O'), np.dtype('O')],
index=['A', 'B', 'C']))
示例6: kdata_df_save
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def kdata_df_save(df, to_path, calculate_change=False):
df = df.drop_duplicates(subset='timestamp', keep='last')
df = df.set_index(df['timestamp'], drop=False)
df.index = pd.to_datetime(df.index)
df = df.sort_index()
if calculate_change:
pre_close = None
for index in df.index:
try:
if pd.notna(df.loc[index, ['preClose', 'change', 'changePct']]).all():
continue
current_close = df.loc[index, 'close']
if pre_close:
df.loc[index, 'preClose'] = pre_close
change = current_close - pre_close
df.loc[index, 'change'] = change
df.loc[index, 'changePct'] = change / current_close
pre_close = df.loc[index, 'close']
except Exception as e:
logger.exception("pre_close:{},current:{}".format(pre_close, df.loc[index, :].to_dict()), e)
df.to_csv(to_path, index=False)
示例7: earliest
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def earliest(s):
"""
Finds the earliest date from the columns of an activity dataframe that show messages sent per date.
Takes a pandas.Series where the columns are the dates (and a couple of calculated columns which will be ignored for the purposes of the calculation) and the cells are the number of messages sent on each date.
"""
just_dates = s.drop(['Earliest Date', 'Latest Date', 'Total Messages', 'Tenure'], errors='ignore')
earliest = None
if 'Earliest Date' in s.index:
if pd.notna(s['Earliest Date']):
earliest = s['Earliest Date']
# limited to all the dates where the value is greater than 0
# then take the array of the dates and calculate the min value
earliest_date = just_dates.loc[just_dates > 0].index.min()
if earliest is not None:
if earliest < earliest_date:
return earliest
return earliest_date
示例8: latest
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def latest(s):
"""
Finds the latest date from the columns of an activity dataframe that show messages sent per date.
Takes a pandas.Series where the columns are the dates (and a couple of calculated columns which will be ignored for the purposes of the calculation) and the cells are the number of messages sent on each date.
"""
just_dates = s.drop(['Earliest Date', 'Latest Date', 'Total Messages', 'Tenure'], errors='ignore')
latest = None
if 'Latest Date' in s.index:
if pd.notna(s['Latest Date']):
latest = s['Latest Date']
# limited to all the dates where the value is greater than 0
# then take the array of the dates and calculate the max value
latest_date = just_dates.loc[just_dates > 0].index.max()
if latest is not None:
if latest > latest_date:
return latest
return latest_date
示例9: test_where_other
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def test_where_other(self):
# other is ndarray or Index
i = pd.date_range('20130101', periods=3, tz='US/Eastern')
for arr in [np.nan, pd.NaT]:
result = i.where(notna(i), other=np.nan)
expected = i
tm.assert_index_equal(result, expected)
i2 = i.copy()
i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist())
result = i.where(notna(i2), i2)
tm.assert_index_equal(result, i2)
i2 = i.copy()
i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist())
result = i.where(notna(i2), i2.values)
tm.assert_index_equal(result, i2)
示例10: test_where_tz
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def test_where_tz(self):
i = pd.date_range('20130101', periods=3, tz='US/Eastern')
result = i.where(notna(i))
expected = i
tm.assert_index_equal(result, expected)
i2 = i.copy()
i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist())
result = i.where(notna(i2))
expected = i2
tm.assert_index_equal(result, expected)
示例11: test_valid
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def test_valid(self, datetime_series):
ts = datetime_series.copy()
ts[::2] = np.NaN
result = ts.dropna()
assert len(result) == ts.count()
tm.assert_series_equal(result, ts[1::2])
tm.assert_series_equal(result, ts[pd.notna(ts)])
示例12: test_notna
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def test_notna(self):
ser = Series([0, 5.4, 3, nan, -0.001])
expected = Series([True, True, True, False, True])
tm.assert_series_equal(ser.notna(), expected)
ser = Series(["hi", "", nan])
expected = Series([True, True, False])
tm.assert_series_equal(ser.notna(), expected)
示例13: test_setitem_always_copy
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def test_setitem_always_copy(self):
s = self.frame['A'].copy()
self.frame['E'] = s
self.frame['E'][5:10] = np.nan
assert notna(s[5:10]).all()
示例14: test_where_align
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def test_where_align(self):
def create():
df = DataFrame(np.random.randn(10, 3))
df.iloc[3:5, 0] = np.nan
df.iloc[4:6, 1] = np.nan
df.iloc[5:8, 2] = np.nan
return df
# series
df = create()
expected = df.fillna(df.mean())
result = df.where(pd.notna(df), df.mean(), axis='columns')
assert_frame_equal(result, expected)
df.where(pd.notna(df), df.mean(), inplace=True, axis='columns')
assert_frame_equal(df, expected)
df = create().fillna(0)
expected = df.apply(lambda x, y: x.where(x > 0, y), y=df[0])
result = df.where(df > 0, df[0], axis='index')
assert_frame_equal(result, expected)
result = df.where(df > 0, df[0], axis='rows')
assert_frame_equal(result, expected)
# frame
df = create()
expected = df.fillna(1)
result = df.where(pd.notna(df), DataFrame(
1, index=df.index, columns=df.columns))
assert_frame_equal(result, expected)
示例15: test_series_setitem
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import notna [as 別名]
def test_series_setitem(self):
s = self.ymd['A']
s[2000, 3] = np.nan
assert isna(s.values[42:65]).all()
assert notna(s.values[:42]).all()
assert notna(s.values[65:]).all()
s[2000, 3, 10] = np.nan
assert isna(s[49])