<p>这可以通过<code>reindex</code>来获得每个商店的所有日期,<code>fillna</code>用平均销售额替换丢失的日期。你知道吗</p>
<pre><code>import pandas as pd
#df['Date'] = pd.to_datetime(df['Date'])
idx = pd.MultiIndex.from_product(
[df['Store Number'].unique(),
pd.date_range(f'{df.Date.dt.year.min()}-01-01',
f'{df.Date.dt.year.max()}-12-31', freq='D')])
df = df.set_index(['Store Number', 'Date']).reindex(idx)
# Mean per year by store.
s = df.groupby([df.index.get_level_values(0),
df.index.get_level_values(1).year]).transform('mean')
df['Sales'] = df[['Sales']].fillna(s)
</code></pre>
<hr/>
<p>打印(df)</p>
<pre><code># Sales
#10 2015-01-01 4000.0
# 2015-01-02 6000.0
# 2015-01-03 5000.0
# 2015-01-04 5000.0
# 2015-01-05 5000.0
#... ...
# 2016-12-27 10000.0
# 2016-12-28 10000.0
# 2016-12-29 10000.0
# 2016-12-30 10000.0
# 2016-12-31 10000.0
#[731 rows x 1 columns]
</code></pre>
<hr/>
<p>因为<code>reindex</code>是笛卡尔积,而不是慢慢地重新索引每个组,我们可以处理的商店从来没有任何销售和下降后,事实。你知道吗</p>
<pre><code>m = (df['Sales'].isnull()
.groupby([df.index.get_level_values(0),
df.index.get_level_values(1).year]).transform('all'))
df = df.loc[~m]
</code></pre>