22.Pandas怎么处理日期索引的缺失?[¶]

22.Pandas怎么处理日期索引的缺失?[¶](http://localhost:8888/notebooks/22. Pandas怎么处理日期索引的缺失.ipynb#Pandas怎么处理日期索引的缺失?)

问题:按日期统计的数据,缺失了某天,导致数据不全该怎么补充日期?

公众号:蚂蚁学Python

可以用两种方法实现:
1、DataFrame.reindex,调整dataframe的索引以适应新的索引
2、DataFrame.resample,可以对时间序列重采样,支持补充缺失值

问题:如果缺失了索引该怎么填充?

In [1]:

import pandas as pd
%matplotlib inline

In [2]:

df = pd.DataFrame({
    "pdate": ["2019-12-01", "2019-12-02", "2019-12-04", "2019-12-05"],
    "pv": [100, 200, 400, 500],
    "uv": [10, 20, 40, 50],
})

df

Out[2]:

pdate pv uv
0 2019-12-01 100 10
1 2019-12-02 200 20
2 2019-12-04 400 40
3 2019-12-05 500 50

In [3]:

df.set_index("pdate").plot()

Out[3]:


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1tQFmA7d-1684078486256)(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZcAAAEGCAYAAACpXNjrAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3deXxU9b3/8dcnOwFCIBAIEyJhD3sQENfiVgGpEKqirf2ppRdvW6vWW6u37bWL7a1219rWa2uvdLkCtQQQtZa6Va1YkQk7CCJKJoFAgLCErPP9/TGHGjFAEiaZJe/n4zGPOXPmnDOfmQPnnbN9v+acQ0REJJwSIl2AiIjEH4WLiIiEncJFRETCTuEiIiJhp3AREZGwS4p0ASfq3bu3GzhwYKTLEBGJKW+99dY+51yfSNdxXNSFy8CBA1m9enWkyxARiSlm9l6ka2hKh8VERCTsFC4iIhJ2ChcREQm7qDvn0pz6+npKS0upqamJdCmtlpaWRm5uLsn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ax+kcV4vFzfoDbgU2ep+/CjjvdN9fzb+IiEjY6Q59EREJO4WLiIiEncJFRETCTuEiIiJhp3AREZGwU7iInITXEuyK00wz3rtjWUSaULiInJnxhO5dEJEmFC7SKZnZQAv1b7HAQn1cPGlm6WY2zRv/KqE7oY9PP9lCfcj4vefhZpYCfAeY6zWhMdfMulqoT403vWlnRexLikSQbqKUTslCrdS+C1zgnHvNzH4L7ABuAS4hdEfzIiDdOTfTzDKAaudcg5ldBnzeOfdJM7sJmOicu9Vb7n8Dm5xzf7BQh0z/BAqdc0c7+CuKRJT2XKQz2+Wce80b/gOhxhXfdc5tc6G/uv7QZNoewJ8s1MPfT4GTNTn+ceAeMysBXgLSgLz2KF4kmrW4yX2ROHTibnuPZsYddx/wonOuyNvreekk0xnwSefc1nAUKBKrtOcinVmemZ3rDV9PqPvYfDMb3GTccT2AgDd8U5Pxhwl1YXvcc8CXvBZxMbPCcBctEgsULtKZbQZuNLN1QC9Ch7vmA097J/TfazLtD4Dvm9lrhPqCP+5FYOTxE/qE9nCSgXXeIbT7OuB7iEQdndCXTsk7tLXCOTc6wqWIxCXtuYiISNhpz0VERMJOey4iIhJ2ChcREQk7hYuIiISdwkVERMJO4SIiImH3/wF862pK84fU4wAAAABJRU5ErkJggg==)]

问题,这里缺失了2019-12-03的数据,导致数据不全该怎么补充?

方法1:使用pandas.reindex方法

1、将df的索引变成日期索引

In [4]:

df_date = df.set_index("pdate")
df_date

Out[4]:

pv uv
pdate
2019-12-01 100 10
2019-12-02 200 20
2019-12-04 400 40
2019-12-05 500 50

In [5]:

df_date.index

Out[5]:

Index(['2019-12-01', '2019-12-02', '2019-12-04', '2019-12-05'], dtype='object', name='pdate')

In [6]:

# 将df的索引设置为日期索引
df_date = df_date.set_index(pd.to_datetime(df_date.index))
df_date

Out[6]:

pv uv
pdate
2019-12-01 100 10
2019-12-02 200 20
2019-12-04 400 40
2019-12-05 500 50

In [7]:

df_date.index

Out[7]:

DatetimeIndex(['2019-12-01', '2019-12-02', '2019-12-04', '2019-12-05'], dtype='datetime64[ns]', name='pdate', freq=None)

2、使用pandas.reindex填充缺失的索引

In [8]:

# 生成完整的日期序列
pdates = pd.date_range(start="2019-12-01", end="2019-12-05")
pdates

Out[8]:

DatetimeIndex(['2019-12-01', '2019-12-02', '2019-12-03', '2019-12-04',
               '2019-12-05'],
              dtype='datetime64[ns]', freq='D')

In [9]:

df_date_new = df_date.reindex(pdates, fill_value=0)
df_date_new

Out[9]:

pv uv
2019-12-01 100 10
2019-12-02 200 20
2019-12-03 0 0
2019-12-04 400 40
2019-12-05 500 50

In [10]:

df_date_new.plot()

Out[10]:


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-r1lgGwh7-1684078486258)(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAX0AAAEQCAYAAABcE6TVAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3dd3yV9d3/8deXbJJAZI8EEyAs2YTlAjcqFquMYlFUEETt0mpp77v7vn+3Vau2VTZUnEWtrdpacQZQGQaQJQIZjAAJhDASQkLG9/fHdUUiJmSQnPl+Ph55nHOunHPy4QDvc+U613W9jbUWEREJDs28PYCIiHiOQl9EJIgo9EVEgohCX0QkiCj0RUSCiEJfRCSIhHp7AIA2bdrYxMREb48hIuJX1q9fn2etbVufx/hE6CcmJpKWlubtMURE/IoxZk99H6PNOyIiQUShLyISRBT6IiJBxCe26VentLSU7OxsiouLvT1KvUVGRhIfH09YWJi3RxER+YY6hb4xZjdQAJQDZdbaFGNMK2AZkAjsBiZaa48aYwzwJ+AGoAi401q7ob6DZWdnExsbS2JiIs5T+gdrLUeOHCE7O5ukpCRvjyMi8g312bxzhbV2oLU2xb09G/jQWpsMfOjeBrgeSHa/ZgBzGzJYcXExrVu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9dMY57cJS925LcU61gLX2kLX2c6D0rKfqDayx1ha5zTErgO82xcwiIsGgybfpG2MSgUHAWqB9ZZGve9mulodvBS43xrR2D9S6gW+WA4uISD006cFZxpgY4O/Aj621J5zjr+rOWrvdGPMH4H2gENiEs+lIREQaoMnW9N1TK/wdeMla+4a7ONcY09H9fkec8++ck7V2sbV2sLX2cpxt/7uaamYRkUDXVHvvGGAxsN1a+2SVb70FTHWvTwXerMNztXMvuwC3AK807rQiIsGjSU7DYIy5FFiFU5NYWYv4C5zt+q8CXYC9wARrbb4xpgOQBrRw718I9HE3Ca0CWuN8yPugtfbDRh9YRCRI6Nw7IiJBREfkiogEEYW+iEgQUeiLiAQRhb6ISBBR6IuIBBGFvohIEFHoi4gEEYW+iEgQ+f9/+xjxWeq6BQAAAABJRU5ErkJggg==)]

方法2:使用pandas.resample方法

1、先将索引变成日期索引

In [11]:

df

Out[11]:

pdate pv uv
0 2019-12-01 100 10
1 2019-12-02 200 20
2 2019-12-04 400 40
3 2019-12-05 500 50

In [12]:

df_new2 = df.set_index(pd.to_datetime(df["pdate"])).drop("pdate", axis=1)
df_new2

Out[12]:

pv uv
pdate
2019-12-01 100 10
2019-12-02 200 20
2019-12-04 400 40
2019-12-05 500 50

In [13]:

df_new2.index

Out[13]:

DatetimeIndex(['2019-12-01', '2019-12-02', '2019-12-04', '2019-12-05'], dtype='datetime64[ns]', name='pdate', freq=None)

2、使用dataframe的resample的方法按照天重采样

resample的含义:
改变数据的时间频率,比如把天数据变成月份,或者把小时数据变成分钟级别

resample的语法:
(DataFrame or Series).resample(arguments).(aggregate function)

resample的采样规则参数:
https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases

In [14]:

# 由于采样会让区间变成一个值,所以需要指定mean等采样值的设定方法
df_new2 = df_new2.resample("D").mean().fillna(0)
df_new2

Out[14]:

pv uv
pdate
2019-12-01 100.0 10.0
2019-12-02 200.0 20.0
2019-12-03 0.0 0.0
2019-12-04 400.0 40.0
2019-12-05 500.0 50.0

In [15]:

# resample的使用方式
df_new2.resample("2D").mean()

Out[15]:

pv uv
pdate
2019-12-01 150.0 15.0
2019-12-03 200.0 20.0
2019-12-05 500.0 50.0
import pandas as pd
import matplotlib.pyplot as plot

df = pd.DataFrame({
    'pdate': ['2019-12-01', '2019-12-02', '2019-12-04', '2019-12-05'],
    'pv': [100, 200, 400, 500],
    'uv': [10, 20, 40, 50],
})
print(df)

# df.set_index('pdate').plot()
# plot.show()

df_date = df.set_index('pdate')
print(df_date)

print(df_date.index)

df_date = df_date.set_index(pd.to_datetime(df_date.index))
print(df_date)
print(df_date.index)

pdates = pd.date_range(start='2019-12-01', end='2019-12-05')
print(pdates)

df_date_new = df_date.reindex(pdates, fill_value=0)
print(df_date_new)
# df_date_new.plot()
# plot.show()

print(df)
df_new2 = df.set_index(pd.to_datetime(df['pdate'])).drop('pdate', axis=1)
print(df_new2)
print(df_new2.index)

df_new2 = df_new2.resample('D').mean().fillna(0)
print(df_new2)
print(df_new2.resample('2D').mean())

你可能感兴趣的:(pandas,python,数据分析)