本文整理汇总了Python中pandas.sparse.api.SparseDataFrame.applymap方法的典型用法代码示例。如果您正苦于以下问题:Python SparseDataFrame.applymap方法的具体用法?Python SparseDataFrame.applymap怎么用?Python SparseDataFrame.applymap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.sparse.api.SparseDataFrame
的用法示例。
在下文中一共展示了SparseDataFrame.applymap方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestSparseDataFrame
# 需要导入模块: from pandas.sparse.api import SparseDataFrame [as 别名]
# 或者: from pandas.sparse.api.SparseDataFrame import applymap [as 别名]
#.........这里部分代码省略.........
# agg / broadcast
broadcasted = self.frame.apply(np.sum, broadcast=True)
tm.assertIsInstance(broadcasted, SparseDataFrame)
exp = self.frame.to_dense().apply(np.sum, broadcast=True)
tm.assert_frame_equal(broadcasted.to_dense(), exp)
self.assertIs(self.empty.apply(np.sqrt), self.empty)
from pandas.core import nanops
applied = self.frame.apply(np.sum)
tm.assert_series_equal(applied,
self.frame.to_dense().apply(nanops.nansum))
def test_apply_nonuq(self):
orig = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
index=['a', 'a', 'c'])
sparse = orig.to_sparse()
res = sparse.apply(lambda s: s[0], axis=1)
exp = orig.apply(lambda s: s[0], axis=1)
# dtype must be kept
self.assertEqual(res.dtype, np.int64)
# ToDo: apply must return subclassed dtype
self.assertIsInstance(res, pd.Series)
tm.assert_series_equal(res.to_dense(), exp)
# df.T breaks
sparse = orig.T.to_sparse()
res = sparse.apply(lambda s: s[0], axis=0) # noqa
exp = orig.T.apply(lambda s: s[0], axis=0)
# TODO: no non-unique columns supported in sparse yet
# tm.assert_series_equal(res.to_dense(), exp)
def test_applymap(self):
# just test that it works
result = self.frame.applymap(lambda x: x * 2)
tm.assertIsInstance(result, SparseDataFrame)
def test_astype(self):
sparse = pd.SparseDataFrame({'A': SparseArray([1, 2, 3, 4],
dtype=np.int64),
'B': SparseArray([4, 5, 6, 7],
dtype=np.int64)})
self.assertEqual(sparse['A'].dtype, np.int64)
self.assertEqual(sparse['B'].dtype, np.int64)
res = sparse.astype(np.float64)
exp = pd.SparseDataFrame({'A': SparseArray([1., 2., 3., 4.],
fill_value=0.),
'B': SparseArray([4., 5., 6., 7.],
fill_value=0.)},
default_fill_value=np.nan)
tm.assert_sp_frame_equal(res, exp)
self.assertEqual(res['A'].dtype, np.float64)
self.assertEqual(res['B'].dtype, np.float64)
sparse = pd.SparseDataFrame({'A': SparseArray([0, 2, 0, 4],
dtype=np.int64),
'B': SparseArray([0, 5, 0, 7],
dtype=np.int64)},
default_fill_value=0)
self.assertEqual(sparse['A'].dtype, np.int64)
self.assertEqual(sparse['B'].dtype, np.int64)
res = sparse.astype(np.float64)
exp = pd.SparseDataFrame({'A': SparseArray([0., 2., 0., 4.],
示例2: TestSparseDataFrame
# 需要导入模块: from pandas.sparse.api import SparseDataFrame [as 别名]
# 或者: from pandas.sparse.api.SparseDataFrame import applymap [as 别名]
#.........这里部分代码省略.........
self.frame.index = self.frame.index
self.assertRaises(Exception, setattr, self.frame, "index", self.frame.index[:-1])
def test_append(self):
a = self.frame[:5]
b = self.frame[5:]
appended = a.append(b)
assert_sp_frame_equal(appended, self.frame)
a = self.frame.ix[:5, :3]
b = self.frame.ix[5:]
appended = a.append(b)
assert_sp_frame_equal(appended.ix[:, :3], self.frame.ix[:, :3])
def test_apply(self):
applied = self.frame.apply(np.sqrt)
self.assert_(isinstance(applied, SparseDataFrame))
assert_almost_equal(applied.values, np.sqrt(self.frame.values))
applied = self.fill_frame.apply(np.sqrt)
self.assert_(applied["A"].fill_value == np.sqrt(2))
# agg / broadcast
applied = self.frame.apply(np.sum)
assert_series_equal(applied, self.frame.to_dense().apply(np.sum))
broadcasted = self.frame.apply(np.sum, broadcast=True)
self.assert_(isinstance(broadcasted, SparseDataFrame))
assert_frame_equal(broadcasted.to_dense(), self.frame.to_dense().apply(np.sum, broadcast=True))
self.assert_(self.empty.apply(np.sqrt) is self.empty)
def test_applymap(self):
# just test that it works
result = self.frame.applymap(lambda x: x * 2)
self.assert_(isinstance(result, SparseDataFrame))
def test_astype(self):
self.assertRaises(Exception, self.frame.astype, np.int64)
def test_fillna(self):
self.assertRaises(NotImplementedError, self.frame.fillna, 0)
def test_rename(self):
# just check this works
renamed = self.frame.rename(index=str)
renamed = self.frame.rename(columns=lambda x: "%s%d" % (x, len(x)))
def test_corr(self):
res = self.frame.corr()
assert_frame_equal(res, self.frame.to_dense().corr())
def test_describe(self):
self.frame["foo"] = np.nan
desc = self.frame.describe()
def test_join(self):
left = self.frame.ix[:, ["A", "B"]]
right = self.frame.ix[:, ["C", "D"]]
joined = left.join(right)
assert_sp_frame_equal(joined, self.frame)
right = self.frame.ix[:, ["B", "D"]]
self.assertRaises(Exception, left.join, right)