application_vnd.jupyter.stderr_output_1.png
import datetime

import pandas as pd

import numpy as np
import pandas as pd
import re
import datetime
from sklearn.neighbors import NearestNeighbors
import matplotlib.pyplot as plt
input_dir = '/kaggle/input/tabular-playground-series-mar-2022/'

def handle_dates(df):
    df['datetime'] = pd.to_datetime(df['time'])
    df['time'] = [datetime.datetime.time(d) for d in df.loc[:, 'datetime']]
    time_mapping = {t: ii for ii, t in enumerate(train.time.unique())}
    df['time_number'] = [time_mapping[d] for d in df.loc[:, 'time']]
    df['date'] = [datetime.datetime.date(d) for d in df.loc[:, 'datetime']]
    df['weekday'] = [d.weekday() for d in df.datetime]
    return df

train = pd.read_csv(input_dir + 'train.csv')
test = pd.read_csv(input_dir + 'test.csv')
train = handle_dates(train)
test = handle_dates(test)

train.loc[:, 'loc_dir_time'] = [str(x) + str(y) + direction + str(t) for _, x, y, direction, t in train.loc[:, ['x', 'y', 'direction', 'time_number']].itertuples()]
test.loc[:, 'loc_dir_time'] = [str(x) + str(y) + direction + str(t) for _, x, y, direction, t in test.loc[:, ['x', 'y', 'direction', 'time_number']].itertuples()]
Xy = train.loc[:, ['loc_dir_time', 'weekday', 'congestion', 'date']]
Xy = pd.pivot_table(Xy, values='congestion', index=['date', 'weekday'], columns=['loc_dir_time']).reset_index()

FINAL = Xy.query('date==datetime.date(1991,9,30)')
Xy = Xy.query('date<datetime.date(1991,9,30)')

Xy = Xy.fillna(Xy.groupby('weekday').transform('median')).set_index('date')