美文网首页
22 Pandas怎么处理日期索引的缺失

22 Pandas怎么处理日期索引的缺失

作者: Viterbi | 来源:发表于2022-11-12 13:14 被阅读0次

22 Pandas怎么处理日期索引的缺失?

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

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

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

import pandas as pd
%matplotlib inline

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
.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
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
df.set_index("pdate").plot()




    <matplotlib.axes._subplots.AxesSubplot at 0x1a0d908bf48>

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

方法1:使用pandas.reindex方法

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

df_date = df.set_index("pdate")
df_date

.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
pv uv
pdate
2019-12-01 100 10
2019-12-02 200 20
2019-12-04 400 40
2019-12-05 500 50
df_date.index


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



# 将df的索引设置为日期索引
df_date = df_date.set_index(pd.to_datetime(df_date.index))
df_date
.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
pv uv
pdate
2019-12-01 100 10
2019-12-02 200 20
2019-12-04 400 40
2019-12-05 500 50
df_date.index



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

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

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


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



df_date_new = df_date.reindex(pdates, fill_value=0)
df_date_new
.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
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
df_date_new.plot()





    <matplotlib.axes._subplots.AxesSubplot at 0x1a0db1ab388>

方法2:使用pandas.resample方法

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

df
.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
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
df_new2 = df.set_index(pd.to_datetime(df["pdate"])).drop("pdate", axis=1)
df_new2
.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
pv uv
pdate
2019-12-01 100 10
2019-12-02 200 20
2019-12-04 400 40
2019-12-05 500 50
df_new2.index



    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


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

.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
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
# resample的使用方式
df_new2.resample("2D").mean()

.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
pv uv
pdate
2019-12-01 150.0 15.0
2019-12-03 200.0 20.0
2019-12-05 500.0 50.0

本文使用 文章同步助手 同步

相关文章

网友评论

      本文标题:22 Pandas怎么处理日期索引的缺失

      本文链接:https://www.haomeiwen.com/subject/jpeltdtx.html