1
1
.. currentmodule :: pandas
2
- .. _computation :
3
2
4
3
.. ipython :: python
5
4
:suppress:
6
5
7
6
import numpy as np
8
7
np.random.seed(123456 )
9
- from pandas import *
10
- import pandas.util.testing as tm
11
- randn = np.random.randn
12
8
np.set_printoptions(precision = 4 , suppress = True )
9
+ import pandas as pd
13
10
import matplotlib
14
11
try :
15
12
matplotlib.style.use(' ggplot' )
16
13
except AttributeError :
17
- options.display.mpl_style = ' default'
14
+ pd. options.display.mpl_style = ' default'
18
15
import matplotlib.pyplot as plt
19
16
plt.close(' all' )
20
- options.display.max_rows= 15
17
+ pd.options.display.max_rows= 15
18
+
19
+ .. _computation :
21
20
22
21
Computational tools
23
22
===================
@@ -36,13 +35,13 @@ NA/null values *before* computing the percent change).
36
35
37
36
.. ipython :: python
38
37
39
- ser = Series(randn(8 ))
38
+ ser = pd. Series(np.random. randn(8 ))
40
39
41
40
ser.pct_change()
42
41
43
42
.. ipython :: python
44
43
45
- df = DataFrame(randn(10 , 4 ))
44
+ df = pd. DataFrame(np.random. randn(10 , 4 ))
46
45
47
46
df.pct_change(periods = 3 )
48
47
@@ -56,8 +55,8 @@ The ``Series`` object has a method ``cov`` to compute covariance between series
56
55
57
56
.. ipython :: python
58
57
59
- s1 = Series(randn(1000 ))
60
- s2 = Series(randn(1000 ))
58
+ s1 = pd. Series(np.random. randn(1000 ))
59
+ s2 = pd. Series(np.random. randn(1000 ))
61
60
s1.cov(s2)
62
61
63
62
Analogously, ``DataFrame `` has a method ``cov `` to compute pairwise covariances
@@ -78,7 +77,7 @@ among the series in the DataFrame, also excluding NA/null values.
78
77
79
78
.. ipython :: python
80
79
81
- frame = DataFrame(randn(1000 , 5 ), columns = [' a' , ' b' , ' c' , ' d' , ' e' ])
80
+ frame = pd. DataFrame(np.random. randn(1000 , 5 ), columns = [' a' , ' b' , ' c' , ' d' , ' e' ])
82
81
frame.cov()
83
82
84
83
``DataFrame.cov `` also supports an optional ``min_periods `` keyword that
@@ -87,7 +86,7 @@ in order to have a valid result.
87
86
88
87
.. ipython :: python
89
88
90
- frame = DataFrame(randn(20 , 3 ), columns = [' a' , ' b' , ' c' ])
89
+ frame = pd. DataFrame(np.random. randn(20 , 3 ), columns = [' a' , ' b' , ' c' ])
91
90
frame.ix[:5 , ' a' ] = np.nan
92
91
frame.ix[5 :10 , ' b' ] = np.nan
93
92
@@ -123,7 +122,7 @@ All of these are currently computed using pairwise complete observations.
123
122
124
123
.. ipython :: python
125
124
126
- frame = DataFrame(randn(1000 , 5 ), columns = [' a' , ' b' , ' c' , ' d' , ' e' ])
125
+ frame = pd. DataFrame(np.random. randn(1000 , 5 ), columns = [' a' , ' b' , ' c' , ' d' , ' e' ])
127
126
frame.ix[::2 ] = np.nan
128
127
129
128
# Series with Series
@@ -140,7 +139,7 @@ Like ``cov``, ``corr`` also supports the optional ``min_periods`` keyword:
140
139
141
140
.. ipython :: python
142
141
143
- frame = DataFrame(randn(20 , 3 ), columns = [' a' , ' b' , ' c' ])
142
+ frame = pd. DataFrame(np.random. randn(20 , 3 ), columns = [' a' , ' b' , ' c' ])
144
143
frame.ix[:5 , ' a' ] = np.nan
145
144
frame.ix[5 :10 , ' b' ] = np.nan
146
145
@@ -157,8 +156,8 @@ objects.
157
156
158
157
index = [' a' , ' b' , ' c' , ' d' , ' e' ]
159
158
columns = [' one' , ' two' , ' three' , ' four' ]
160
- df1 = DataFrame(randn(5 , 4 ), index = index, columns = columns)
161
- df2 = DataFrame(randn(4 , 4 ), index = index[:4 ], columns = columns)
159
+ df1 = pd. DataFrame(np.random. randn(5 , 4 ), index = index, columns = columns)
160
+ df2 = pd. DataFrame(np.random. randn(4 , 4 ), index = index[:4 ], columns = columns)
162
161
df1.corrwith(df2)
163
162
df2.corrwith(df1, axis = 1 )
164
163
@@ -172,7 +171,7 @@ of the ranks (by default) for the group:
172
171
173
172
.. ipython :: python
174
173
175
- s = Series(np.random.randn(5 ), index = list (' abcde' ))
174
+ s = pd. Series(np.random. np.random.randn(5 ), index = list (' abcde' ))
176
175
s[' d' ] = s[' b' ] # so there's a tie
177
176
s.rank()
178
177
@@ -181,7 +180,7 @@ or the columns (``axis=1``). ``NaN`` values are excluded from the ranking.
181
180
182
181
.. ipython :: python
183
182
184
- df = DataFrame(np.random.randn(10 , 6 ))
183
+ df = pd. DataFrame(np.random. np.random.randn(10 , 6 ))
185
184
df[4 ] = df[2 ][:5 ] # some ties
186
185
df
187
186
df.rank(1 )
@@ -253,7 +252,7 @@ These functions can be applied to ndarrays or Series objects:
253
252
254
253
.. ipython :: python
255
254
256
- ts = Series(randn(1000 ), index = date_range(' 1/1/2000' , periods = 1000 ))
255
+ ts = pd. Series(np.random. randn(1000 ), index = pd. date_range(' 1/1/2000' , periods = 1000 ))
257
256
ts = ts.cumsum()
258
257
259
258
ts.plot(style = ' k--' )
@@ -271,7 +270,7 @@ sugar for applying the moving window operator to all of the DataFrame's columns:
271
270
272
271
.. ipython :: python
273
272
274
- df = DataFrame(randn(1000 , 4 ), index = ts.index,
273
+ df = pd. DataFrame(np.random. randn(1000 , 4 ), index = ts.index,
275
274
columns = [' A' , ' B' , ' C' , ' D' ])
276
275
df = df.cumsum()
277
276
@@ -310,7 +309,7 @@ keyword. The list of recognized types are:
310
309
311
310
.. ipython :: python
312
311
313
- ser = Series(randn(10 ), index = date_range(' 1/1/2000' , periods = 10 ))
312
+ ser = pd. Series(np.random. randn(10 ), index = pd. date_range(' 1/1/2000' , periods = 10 ))
314
313
315
314
rolling_window(ser, 5 , ' triang' )
316
315
0 commit comments