text_html_output_1.png text_plain_output_1.png
import numpy as np

import pandas as pd

df_train = pd.read_csv('../input/train.csv', usecols=[1, 2, 3, 4, 5], dtype={'onpromotion': bool}, converters={'unit_sales': lambda u: np.log1p(float(u)) if float(u) > 0 else 0}, parse_dates=['date'], skiprows=range(1, 66458909))
df_test = pd.read_csv('../input/test.csv', usecols=[0, 1, 2, 3, 4], dtype={'onpromotion': bool}, parse_dates=['date']).set_index(['store_nbr', 'item_nbr', 'date'])
items = pd.read_csv('../input/items.csv').set_index('item_nbr')

df_2017 = df_train.loc[df_train.date >= pd.datetime(2017, 1, 1)]
del df_train

promo_2017_train = df_2017.set_index(['store_nbr', 'item_nbr', 'date'])[['onpromotion']].unstack(level=-1).fillna(False)
promo_2017_train.columns = promo_2017_train.columns.get_level_values(1)
promo_2017_test = df_test[['onpromotion']].unstack(level=-1).fillna(False)
promo_2017_test.columns = promo_2017_test.columns.get_level_values(1)
promo_2017_train = promo_2017_train.reindex(promo_2017_test.index).fillna(False)
promo_2017 = pd.concat([promo_2017_train, promo_2017_test], axis=1)
del promo_2017_test, promo_2017_train

df_2017 = df_2017.set_index(['store_nbr', 'item_nbr', 'date'])[['unit_sales']].unstack(level=-1).fillna(0)
df_2017.columns = df_2017.columns.get_level_values(1)
items = items.reindex(df_2017.index.get_level_values(1))

print(df_2017.shape)
df_2017.head()