【Pandas】pandas Series interpolate

# Pandas2.2 Series

## Computations descriptive stats

|方法|描述|

|-|:-------|

|Series.backfill(*[, axis, inplace, limit, ...])|用于填充 `Series` 中缺失值(NaN)的方法|

|Series.bfill(*[, axis, inplace, limit, ...])|用于填充 `Series` 中缺失值(NaN)的方法|

|Series.dropna(*[, axis, inplace, how, ...])|用于删除 `Series` 中包含缺失值(NaN)的元素的方法|

|Series.ffill(*[, axis, inplace, limit, ...])|用于填充 `Series` 中缺失值(NaN)的方法|

|Series.fillna([value, method, axis, ...])|用于填充 `Series` 中缺失值(NaN)的方法|

|Series.interpolate([method, axis, limit, ...])|用于填充 `Series` 中缺失值(NaN)的方法|

### pandas.Series.interpolate

`pandas.Series.interpolate` 是用于填充 `Series` 中缺失值(NaN)的方法,它通过插值法来估计缺失值。插值方法可以根据数据的趋势和模式进行更复杂的填充,适用于时间序列或其他有序数据。

#### 参数说明

- **method**:字符串,默认为 `'linear'`。指定插值方法:

  - `'linear'`:线性插值。

  - `'time'`:基于时间的插值。

  - `'index'`:基于索引的插值。

  - `'values'`:基于值的插值。

  - `'nearest'`:最近邻插值。

  - `'zero'`:零阶保持插值。

  - `'slinear'`:一阶样条插值。

  - `'quadratic'`:二阶样条插值。

  - `'cubic'`:三阶样条插值。

  - `'barycentric'`:使用Barycentric插值器。

  - `'krogh'`:使用Krogh插值器。

  - `'polynomial'`:多项式插值。

  - `'spline'`:样条插值。

  - `'piecewise_polynomial'`:分段多项式插值。

  - `'from_derivatives'`:从导数中插值。

  - `'pchip'`:PCHIP插值。

  - `'akima'`:Akima插值。

  - `'cubicspline'`:三次样条插值。

- **axis**:{0或'index'},默认为0。表示沿哪个轴进行操作。对于 `Series` 来说,这个参数通常不需要设置。

- **limit**:整数,默认为 `None`。指定最大连续填充个数,超过此限制的缺失值将不被填充。

- **inplace**:布尔值,默认为 `False`。如果为 `True`,则就地修改原 `Series`,否则返回一个新的 `Series`。

- **limit_direction**:字符串,默认为 `None`。指定填充方向:

  - `'forward'`:向前填充。

  - `'backward'`:向后填充。

  - `'both'`:双向填充。

- **limit_area**:可选,默认为 `None`。指定填充区域,可以是 'inside' 或 'outside',用于控制填充范围。

- **downcast**:可选,默认为 ``。用于向下转换数据类型,例如从浮点型转换为整型。

- **kwargs**:其他传递给插值器的关键字参数。

#### 示例及结果

##### 示例1:基本用法(线性插值)

```python

import pandas as pd

import numpy as np

# 创建一个包含缺失值的 Series

s = pd.Series([1, np.nan, np.nan, 4, 5, np.nan, 7])

print("原始 Series:")

print(s)

# 使用 interpolate 方法进行线性插值

filled_s = s.interpolate(method='linear')

print("\n插值后的 Series (使用 linear 方法):")

print(filled_s)

```

**输出结果:**

```plaintext

原始 Series:

0    1.0

1    NaN

2    NaN

3    4.0

4    5.0

5    NaN

6    7.0

dtype: float64

插值后的 Series (使用 linear 方法):

0    1.0

1    2.0

2    3.0

3    4.0

4    5.0

5    6.0

6    7.0

dtype: float64

```

##### 示例2:使用 `inplace` 参数

```python

# 创建一个包含缺失值的 Series

s_inplace = pd.Series([1, np.nan, np.nan, 4, 5, np.nan, 7])

print("原始 Series:")

print(s_inplace)

# 使用 interpolate 方法并设置 inplace=True

s_inplace.interpolate(method='linear', inplace=True)

print("\n插值后的 Series (使用 linear 方法并设置 inplace=True):")

print(s_inplace)

```

**输出结果:**

```plaintext

原始 Series:

0    1.0

1    NaN

2    NaN

3    4.0

4    5.0

5    NaN

6    7.0

dtype: float64

插值后的 Series (使用 linear 方法并设置 inplace=True):

0    1.0

1    2.0

2    3.0

3    4.0

4    5.0

5    6.0

6    7.0

dtype: float64

```

##### 示例3:使用 `limit` 参数

```python

# 创建一个包含缺失值的 Series

s_limit = pd.Series([1, np.nan, np.nan, 4, 5, np.nan, np.nan, 8])

print("原始 Series:")

print(s_limit)

# 使用 interpolate 方法并设置 limit=1

filled_s_limit = s_limit.interpolate(method='linear', limit=1)

print("\n插值后的 Series (使用 linear 方法并设置 limit=1):")

print(filled_s_limit)

```

**输出结果:**

```plaintext

原始 Series:

0    1.0

1    NaN

2    NaN

3    4.0

4    5.0

5    NaN

6    NaN

7    8.0

dtype: float64

插值后的 Series (使用 linear 方法并设置 limit=1):

0    1.0

1    2.0

2    NaN

3    4.0

4    5.0

5    6.0

6    NaN

7    8.0

dtype: float64

```

##### 示例4:使用 `limit_direction` 参数

```python

# 创建一个包含缺失值的 Series

s_limit_direction = pd.Series([1, np.nan, np.nan, 4, 5, np.nan, np.nan, 8])

print("原始 Series:")

print(s_limit_direction)

# 使用 interpolate 方法并设置 limit_direction='backward'

filled_s_backward = s_limit_direction.interpolate(method='linear', limit_direction='backward')

print("\n插值后的 Series (使用 linear 方法并设置 limit_direction='backward'):")

print(filled_s_backward)

# 使用 interpolate 方法并设置 limit_direction='both'

filled_s_both = s_limit_direction.interpolate(method='linear', limit_direction='both')

print("\n插值后的 Series (使用 linear 方法并设置 limit_direction='both'):")

print(filled_s_both)

```

**输出结果:**

```plaintext

原始 Series:

0    1.0

1    NaN

2    NaN

3    4.0

4    5.0

5    NaN

6    NaN

7    8.0

dtype: float64

插值后的 Series (使用 linear 方法并设置 limit_direction='backward'):

0    1.0

1    2.0

2    3.0

3    4.0

4    5.0

5    6.0

6    7.0

7    8.0

dtype: float64

插值后的 Series (使用 linear 方法并设置 limit_direction='both'):

0    1.0

1    2.0

2    3.0

3    4.0

4    5.0

5    6.0

6    7.0

7    8.0

dtype: float64

```

##### 示例5:使用 `limit_area` 参数

```python

# 创建一个包含缺失值的 Series

s_limit_area = pd.Series([1, np.nan, np.nan, 4, 5, np.nan, np.nan, 8, np.nan])

print("原始 Series:")

print(s_limit_area)

# 使用 interpolate 方法并设置 limit_area='inside'

filled_s_inside = s_limit_area.interpolate(method='linear', limit=2, limit_area='inside')

print("\n插值后的 Series (使用 linear 方法并设置 limit=2 和 limit_area='inside'):")

print(filled_s_inside)

# 使用 interpolate 方法并设置 limit_area='outside'

filled_s_outside = s_limit_area.interpolate(method='linear', limit=2, limit_area='outside')

print("\n插值后的 Series (使用 linear 方法并设置 limit=2 和 limit_area='outside'):")

print(filled_s_outside)

```

**输出结果:**

```plaintext

原始 Series:

0    1.0

1    NaN

2    NaN

3    4.0

4    5.0

5    NaN

6    NaN

7    8.0

8    NaN

dtype: float64

插值后的 Series (使用 linear 方法并设置 limit=2 和 limit_area='inside'):

0    1.0

1    2.0

2    3.0

3    4.0

4    5.0

5    6.0

6    7.0

7    8.0

8    NaN

dtype: float64

插值后的 Series (使用 linear 方法并设置 limit=2 和 limit_area='outside'):

0    1.0

1    NaN

2    NaN

3    4.0

4    5.0

5    NaN

6    NaN

7    8.0

8    8.0

dtype: float64

```

通过这些示例,可以看到 `interpolate` 方法在不同参数下的使用方式及其效果。特别是 `method`、`limit`、`limit_direction` 和 `limit_area` 参数可以更灵活地控制插值行为,从而更好地处理缺失值。

你可能感兴趣的:(Pandas,Series,pandas)