本文整理汇总了Python中pandas.core.api.Series.copy方法的典型用法代码示例。如果您正苦于以下问题:Python Series.copy方法的具体用法?Python Series.copy怎么用?Python Series.copy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.core.api.Series
的用法示例。
在下文中一共展示了Series.copy方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_interpolate
# 需要导入模块: from pandas.core.api import Series [as 别名]
# 或者: from pandas.core.api.Series import copy [as 别名]
def test_interpolate(self):
ts = Series(np.arange(len(self.ts), dtype=float), self.ts.index)
ts_copy = ts.copy()
ts_copy[5:10] = np.NaN
linear_interp = ts_copy.interpolate(method='linear')
self.assert_(np.array_equal(linear_interp, ts))
ord_ts = Series([d.toordinal() for d in self.ts.index],
index=self.ts.index).astype(float)
ord_ts_copy = ord_ts.copy()
ord_ts_copy[5:10] = np.NaN
time_interp = ord_ts_copy.interpolate(method='time')
self.assert_(np.array_equal(time_interp, ord_ts))
# try time interpolation on a non-TimeSeries
self.assertRaises(Exception, self.series.interpolate, method='time')
示例2: test_partial_setting
# 需要导入模块: from pandas.core.api import Series [as 别名]
# 或者: from pandas.core.api.Series import copy [as 别名]
def test_partial_setting(self):
# GH2578, allow ix and friends to partially set
### series ###
s_orig = Series([1,2,3])
s = s_orig.copy()
s[5] = 5
expected = Series([1,2,3,5],index=[0,1,2,5])
assert_series_equal(s,expected)
s = s_orig.copy()
s.loc[5] = 5
expected = Series([1,2,3,5],index=[0,1,2,5])
assert_series_equal(s,expected)
s = s_orig.copy()
s[5] = 5.
expected = Series([1,2,3,5.],index=[0,1,2,5])
assert_series_equal(s,expected)
s = s_orig.copy()
s.loc[5] = 5.
expected = Series([1,2,3,5.],index=[0,1,2,5])
assert_series_equal(s,expected)
# iloc/iat raise
s = s_orig.copy()
def f():
s.iloc[3] = 5.
self.assertRaises(IndexError, f)
def f():
s.iat[3] = 5.
self.assertRaises(IndexError, f)
### frame ###
df_orig = DataFrame(np.arange(6).reshape(3,2),columns=['A','B'])
# iloc/iat raise
df = df_orig.copy()
def f():
df.iloc[4,2] = 5.
self.assertRaises(IndexError, f)
def f():
df.iat[4,2] = 5.
self.assertRaises(IndexError, f)
# row setting where it exists
expected = DataFrame(dict({ 'A' : [0,4,4], 'B' : [1,5,5] }))
df = df_orig.copy()
df.iloc[1] = df.iloc[2]
assert_frame_equal(df,expected)
expected = DataFrame(dict({ 'A' : [0,4,4], 'B' : [1,5,5] }))
df = df_orig.copy()
df.loc[1] = df.loc[2]
assert_frame_equal(df,expected)
expected = DataFrame(dict({ 'A' : [0,2,4,4], 'B' : [1,3,5,5] }),dtype='float64')
df = df_orig.copy()
df.loc[3] = df.loc[2]
assert_frame_equal(df,expected)
# single dtype frame, overwrite
expected = DataFrame(dict({ 'A' : [0,2,4], 'B' : [0,2,4] }))
df = df_orig.copy()
df.ix[:,'B'] = df.ix[:,'A']
assert_frame_equal(df,expected)
# mixed dtype frame, overwrite
expected = DataFrame(dict({ 'A' : [0,2,4], 'B' : Series([0.,2.,4.]) }))
df = df_orig.copy()
df['B'] = df['B'].astype(np.float64)
df.ix[:,'B'] = df.ix[:,'A']
assert_frame_equal(df,expected)
# single dtype frame, partial setting
expected = df_orig.copy()
expected['C'] = df['A'].astype(np.float64)
df = df_orig.copy()
df.ix[:,'C'] = df.ix[:,'A']
assert_frame_equal(df,expected)
# mixed frame, partial setting
expected = df_orig.copy()
expected['C'] = df['A'].astype(np.float64)
df = df_orig.copy()
df.ix[:,'C'] = df.ix[:,'A']
assert_frame_equal(df,expected)
### panel ###
p_orig = Panel(np.arange(16).reshape(2,4,2),items=['Item1','Item2'],major_axis=pd.date_range('2001/1/12',periods=4),minor_axis=['A','B'],dtype='float64')
# panel setting via item
p_orig = Panel(np.arange(16).reshape(2,4,2),items=['Item1','Item2'],major_axis=pd.date_range('2001/1/12',periods=4),minor_axis=['A','B'],dtype='float64')
expected = p_orig.copy()
expected['Item3'] = expected['Item1']
p = p_orig.copy()
#.........这里部分代码省略.........