@@ -348,14 +348,21 @@ that, by default, performs linear interpolation at missing data points.
348
348
np.random.seed(123456 )
349
349
idx = pd.date_range(' 1/1/2000' , periods = 100 , freq = ' BM' )
350
350
ts = pd.Series(np.random.randn(100 ), index = idx)
351
- ts[1 :20 ] = np.nan
351
+ ts[1 :5 ] = np.nan
352
+ ts[20 :30 ] = np.nan
352
353
ts[60 :80 ] = np.nan
353
354
ts = ts.cumsum()
354
355
355
356
.. ipython :: python
356
357
357
358
ts
358
359
ts.count()
360
+ @savefig series_before_interpolate.png
361
+ ts.plot()
362
+
363
+ .. ipython :: python
364
+
365
+ ts.interpolate()
359
366
ts.interpolate().count()
360
367
361
368
@savefig series_interpolate.png
@@ -435,9 +442,9 @@ Compare several methods:
435
442
436
443
np.random.seed(2 )
437
444
438
- ser = pd.Series(np.arange(1 , 10.1 , .25 )** 2 + np.random.randn(37 ))
439
- bad = np.array([4 , 13 , 14 , 15 , 16 , 17 , 18 , 20 , 29 ])
440
- ser[bad ] = np.nan
445
+ ser = pd.Series(np.arange(1 , 10.1 , .25 ) ** 2 + np.random.randn(37 ))
446
+ missing = np.array([4 , 13 , 14 , 15 , 16 , 17 , 18 , 20 , 29 ])
447
+ ser[missing ] = np.nan
441
448
methods = [' linear' , ' quadratic' , ' cubic' ]
442
449
443
450
df = pd.DataFrame({m: ser.interpolate(method = m) for m in methods})
@@ -451,6 +458,7 @@ You can mix pandas' ``reindex`` and ``interpolate`` methods to interpolate
451
458
at the new values.
452
459
453
460
.. ipython :: python
461
+ :okexcept:
454
462
455
463
ser = pd.Series(np.sort(np.random.uniform(size = 100 )))
456
464
@@ -476,6 +484,7 @@ filled since the last valid observation:
476
484
477
485
ser = pd.Series([np.nan, np.nan, 5 , np.nan, np.nan,
478
486
np.nan, 13 , np.nan, np.nan])
487
+ ser
479
488
480
489
# fill all consecutive values in a forward direction
481
490
ser.interpolate()
0 commit comments