text_html_output_2.png text_html_output_1.png text_html_output_3.png
from datetime import timedelta

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_cs

TARGET = 'Is_CANCELLED'
NA_CATEGORY = 'NA_'

train = pd.read_csv('/kaggle/input/techcom-ai-competition/train.csv')
test = pd.read_csv('/kaggle/input/techcom-ai-competition/test.csv')
sample_sub = pd.read_csv('/kaggle/input/techcom-ai-competition/sample_submission.csv')
train.rename(columns={'MEMBERSHIP_STATUS': 'Is_CANCELLED'}, inplace=True)
train[TARGET] = train[TARGET].map({'INFORCE': 0, 'CANCELLED': 1})
test['END_DATE'] = np.nan

def add_date_features(df: pd.DataFrame):
    df['START_DATE'] = pd.to_datetime(df['START_DATE'], format='%Y%m%d')
    df['START_YEAR'] = df['START_DATE'].dt.year
    df['START_MONTH'] = df['START_DATE'].dt.month
    df['START_YM'] = df['START_YEAR'] * 100 + df['START_MONTH']
    df['END_DATE'] = pd.to_datetime(df['END_DATE'], format='%Y%m%d')
    df['END_YEAR'] = df['END_DATE'].dt.year
    df['END_MONTH'] = df['END_DATE'].dt.month
    df['END_YM'] = df['END_YEAR'] * 100 + df['END_MONTH']
    df['N_DAYS'] = (df['END_DATE'] - df['START_DATE']) / timedelta(days=1)
    return df
train = add_date_features(train)
test = add_date_features(test)

print(f'データの採取期間...\ntrain.csv:\n  START_DATE:\n    From. {train.START_DATE.min()} To. {train.START_DATE.max()}\n  END_DATE:\n    From. {train.END_DATE.min()} To. {train.END_DATE.max()}\ntest.csv\n  START_DATE:\n    From. {test.START_DATE.min()} To. {test.START_DATE.max()}')