{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "2c11156c-2afb-40c0-9a1d-ac93057b7296",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PassengerFirstName     1588941\n",
      "PassengerSecondName    1556752\n",
      "PassengerLastName      1851607\n",
      "PassengerSex            155169\n",
      "PassengerBirthDate     2172062\n",
      "PassengerDocument      2327209\n",
      "BookingCode            2095801\n",
      "TicketNumber           2869544\n",
      "Baggage                 155195\n",
      "FlightDate             1241176\n",
      "FlightTime             1396327\n",
      "FlightNumber           1551474\n",
      "CodeShare               155170\n",
      "Destination             155733\n",
      "Index                        0\n",
      "dtype: int64 18.379364013671875 MiB\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerFirstName</th>\n",
       "      <th>PassengerSecondName</th>\n",
       "      <th>PassengerLastName</th>\n",
       "      <th>PassengerSex</th>\n",
       "      <th>PassengerBirthDate</th>\n",
       "      <th>PassengerDocument</th>\n",
       "      <th>BookingCode</th>\n",
       "      <th>TicketNumber</th>\n",
       "      <th>Baggage</th>\n",
       "      <th>FlightDate</th>\n",
       "      <th>FlightTime</th>\n",
       "      <th>FlightNumber</th>\n",
       "      <th>CodeShare</th>\n",
       "      <th>Destination</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SAVELII</td>\n",
       "      <td>VIKTOROVICH</td>\n",
       "      <td>RUSANOV</td>\n",
       "      <td>Male</td>\n",
       "      <td>03/10/1983</td>\n",
       "      <td>2879 096860</td>\n",
       "      <td>FRNINO</td>\n",
       "      <td>6625956945991971</td>\n",
       "      <td>Transit</td>\n",
       "      <td>2017-03-22</td>\n",
       "      <td>06:05</td>\n",
       "      <td>SU1369</td>\n",
       "      <td>Own</td>\n",
       "      <td>Moscow</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>LEV</td>\n",
       "      <td>MARKOVICH</td>\n",
       "      <td>ISAEV</td>\n",
       "      <td>Male</td>\n",
       "      <td>12/13/1975</td>\n",
       "      <td>1788 173211</td>\n",
       "      <td>Not presented</td>\n",
       "      <td>1643715499224676</td>\n",
       "      <td>Registered</td>\n",
       "      <td>2017-03-18</td>\n",
       "      <td>22:10</td>\n",
       "      <td>SU1180</td>\n",
       "      <td>Own</td>\n",
       "      <td>Volgograd</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NIKOLAI</td>\n",
       "      <td>I.</td>\n",
       "      <td>NIKOLSKII</td>\n",
       "      <td>Male</td>\n",
       "      <td>12/26/1990</td>\n",
       "      <td>4396 926588</td>\n",
       "      <td>VWNYGF</td>\n",
       "      <td>6247422701565929</td>\n",
       "      <td>Transit</td>\n",
       "      <td>2017-03-18</td>\n",
       "      <td>22:10</td>\n",
       "      <td>SU1180</td>\n",
       "      <td>Own</td>\n",
       "      <td>Volgograd</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ANATOLII</td>\n",
       "      <td>PETROVICH</td>\n",
       "      <td>SHILOV</td>\n",
       "      <td>Male</td>\n",
       "      <td>05/24/1997</td>\n",
       "      <td>2595 919752</td>\n",
       "      <td>WQFFUE</td>\n",
       "      <td>Not presented</td>\n",
       "      <td>Registered</td>\n",
       "      <td>2017-03-18</td>\n",
       "      <td>22:10</td>\n",
       "      <td>SU1180</td>\n",
       "      <td>Own</td>\n",
       "      <td>Volgograd</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>MIROSLAVA</td>\n",
       "      <td>VIACHESLAVOVNA</td>\n",
       "      <td>SEMENOVA</td>\n",
       "      <td>Female</td>\n",
       "      <td>01/31/1976</td>\n",
       "      <td>6775 516990</td>\n",
       "      <td>Not presented</td>\n",
       "      <td>Not presented</td>\n",
       "      <td>Registered</td>\n",
       "      <td>2017-03-12</td>\n",
       "      <td>11:45</td>\n",
       "      <td>SU6284</td>\n",
       "      <td>Own</td>\n",
       "      <td>Moscow</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  PassengerFirstName PassengerSecondName PassengerLastName PassengerSex  \\\n",
       "0            SAVELII         VIKTOROVICH           RUSANOV         Male   \n",
       "1                LEV           MARKOVICH             ISAEV         Male   \n",
       "2            NIKOLAI                  I.         NIKOLSKII         Male   \n",
       "3           ANATOLII           PETROVICH            SHILOV         Male   \n",
       "4          MIROSLAVA      VIACHESLAVOVNA          SEMENOVA       Female   \n",
       "\n",
       "  PassengerBirthDate PassengerDocument    BookingCode      TicketNumber  \\\n",
       "0         03/10/1983       2879 096860         FRNINO  6625956945991971   \n",
       "1         12/13/1975       1788 173211  Not presented  1643715499224676   \n",
       "2         12/26/1990       4396 926588         VWNYGF  6247422701565929   \n",
       "3         05/24/1997       2595 919752         WQFFUE     Not presented   \n",
       "4         01/31/1976       6775 516990  Not presented     Not presented   \n",
       "\n",
       "      Baggage FlightDate FlightTime FlightNumber CodeShare Destination  \n",
       "0     Transit 2017-03-22      06:05       SU1369       Own      Moscow  \n",
       "1  Registered 2017-03-18      22:10       SU1180       Own   Volgograd  \n",
       "2     Transit 2017-03-18      22:10       SU1180       Own   Volgograd  \n",
       "3  Registered 2017-03-18      22:10       SU1180       Own   Volgograd  \n",
       "4  Registered 2017-03-12      11:45       SU6284       Own      Moscow  "
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cudf\n",
    "\n",
    "df = cudf.read_csv('BoardingData.csv', sep=';', dtype={\n",
    "    'PassengerFirstName': 'object',\n",
    "    'PassengerSecondName': 'object',\n",
    "    'PassengerLastName': 'object',\n",
    "    'PassengerBirthDate': 'object',\n",
    "    'PassengerDocument': 'object',\n",
    "    'BookingCode': 'object',\n",
    "    'TicketNumber': 'object',\n",
    "    'Baggage': 'category',\n",
    "    'FlightDate': 'datetime64[s]',\n",
    "    'FlightTime': 'object',\n",
    "    'FlightNumber': 'object',\n",
    "    'PassengerSex': 'category',\n",
    "    'CodeShare': 'category',\n",
    "    'Destination\\r': 'category',\n",
    "})\n",
    "df.rename(columns={'Destination\\r': 'Destination'}, inplace=1)\n",
    "print(df.memory_usage(), df.memory_usage().sum() / 2**20, 'MiB')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "0a1045e8-74f9-4323-b2ec-42449adde616",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['PassengerSecondName'] = '.' + df['PassengerSecondName'].str.slice(0, 1) + '.'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "2a748997-b20f-41d4-b8e2-0929c7d1dad3",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['PassengerBirthDate_y'] = df['PassengerBirthDate'].str.slice(6, 10).astype('int16')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "8f3b7ef1-86c0-428e-8f2c-72bb96c70629",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['PassengerName'] = (df['PassengerFirstName'] +\\\n",
    "                       df['PassengerSecondName'] +\\\n",
    "                       df['PassengerLastName']).hash_values('murmur3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "505924e3-4eaa-42bb-83d4-e2bde09e14f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['PassengerDocument'] = df['PassengerDocument'].str.replace(' ', '').astype('uint64')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "e658a327-2355-48d8-85e5-4e431785304e",
   "metadata": {},
   "outputs": [],
   "source": [
    "hours = df['FlightTime'].str.slice(0, 2).astype('int32')\n",
    "minutes = df['FlightTime'].str.slice(3, 5).astype('int32')\n",
    "\n",
    "df['FlightDate_s'] = df['FlightDate'] + (hours * 3600 + minutes * 60).astype('timedelta64[s]')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "7b32961b-5c6c-482c-9bc3-97f1a296a452",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['BookingCode'] = (df['BookingCode'] != 'Not presented').astype('int8')\n",
    "df['TicketNumber'] = (df['TicketNumber'] != 'Not presented').astype('int8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "12a2369f-5c7f-4776-ba4f-da6a58cd0a9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.drop(columns=['FlightDate', 'FlightTime',\\\n",
    "                 'PassengerBirthDate', 'PassengerFirstName',\\\n",
    "                 'PassengerSecondName', 'PassengerLastName'], inplace=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "df8ba5f2-2ad3-4964-b0aa-07f49ab5dbd4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PassengerSex             155169\n",
      "PassengerDocument       1241176\n",
      "BookingCode              155147\n",
      "TicketNumber             155147\n",
      "Baggage                  155195\n",
      "FlightNumber            1551474\n",
      "CodeShare                155170\n",
      "Destination              155733\n",
      "PassengerBirthDate_y     310294\n",
      "PassengerName            620588\n",
      "FlightDate_s            1241176\n",
      "Index                         0\n",
      "dtype: int64 5.623120307922363 MiB\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerSex</th>\n",
       "      <th>PassengerDocument</th>\n",
       "      <th>BookingCode</th>\n",
       "      <th>TicketNumber</th>\n",
       "      <th>Baggage</th>\n",
       "      <th>FlightNumber</th>\n",
       "      <th>CodeShare</th>\n",
       "      <th>Destination</th>\n",
       "      <th>PassengerBirthDate_y</th>\n",
       "      <th>PassengerName</th>\n",
       "      <th>FlightDate_s</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Male</td>\n",
       "      <td>2879096860</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Transit</td>\n",
       "      <td>SU1369</td>\n",
       "      <td>Own</td>\n",
       "      <td>Moscow</td>\n",
       "      <td>1983</td>\n",
       "      <td>702066535</td>\n",
       "      <td>2017-03-22 06:05:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Male</td>\n",
       "      <td>1788173211</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>Registered</td>\n",
       "      <td>SU1180</td>\n",
       "      <td>Own</td>\n",
       "      <td>Volgograd</td>\n",
       "      <td>1975</td>\n",
       "      <td>1506391098</td>\n",
       "      <td>2017-03-18 22:10:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Male</td>\n",
       "      <td>4396926588</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Transit</td>\n",
       "      <td>SU1180</td>\n",
       "      <td>Own</td>\n",
       "      <td>Volgograd</td>\n",
       "      <td>1990</td>\n",
       "      <td>921530796</td>\n",
       "      <td>2017-03-18 22:10:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Male</td>\n",
       "      <td>2595919752</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Registered</td>\n",
       "      <td>SU1180</td>\n",
       "      <td>Own</td>\n",
       "      <td>Volgograd</td>\n",
       "      <td>1997</td>\n",
       "      <td>2477460736</td>\n",
       "      <td>2017-03-18 22:10:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Female</td>\n",
       "      <td>6775516990</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>Registered</td>\n",
       "      <td>SU6284</td>\n",
       "      <td>Own</td>\n",
       "      <td>Moscow</td>\n",
       "      <td>1976</td>\n",
       "      <td>3539264187</td>\n",
       "      <td>2017-03-12 11:45:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  PassengerSex  PassengerDocument  BookingCode  TicketNumber     Baggage  \\\n",
       "0         Male         2879096860            1             1     Transit   \n",
       "1         Male         1788173211            0             1  Registered   \n",
       "2         Male         4396926588            1             1     Transit   \n",
       "3         Male         2595919752            1             0  Registered   \n",
       "4       Female         6775516990            0             0  Registered   \n",
       "\n",
       "  FlightNumber CodeShare Destination  PassengerBirthDate_y  PassengerName  \\\n",
       "0       SU1369       Own      Moscow                  1983      702066535   \n",
       "1       SU1180       Own   Volgograd                  1975     1506391098   \n",
       "2       SU1180       Own   Volgograd                  1990      921530796   \n",
       "3       SU1180       Own   Volgograd                  1997     2477460736   \n",
       "4       SU6284       Own      Moscow                  1976     3539264187   \n",
       "\n",
       "         FlightDate_s  \n",
       "0 2017-03-22 06:05:00  \n",
       "1 2017-03-18 22:10:00  \n",
       "2 2017-03-18 22:10:00  \n",
       "3 2017-03-18 22:10:00  \n",
       "4 2017-03-12 11:45:00  "
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(df.memory_usage(), df.memory_usage().sum() / 2 ** 20, 'MiB')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "3c078b2c-ded7-44fd-aebe-7ce623cfa6dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PassengerSex                 category\n",
       "PassengerDocument              uint64\n",
       "BookingCode                      int8\n",
       "TicketNumber                     int8\n",
       "Baggage                      category\n",
       "FlightNumber                   object\n",
       "CodeShare                    category\n",
       "Destination                  category\n",
       "PassengerBirthDate_y            int16\n",
       "PassengerName                  uint32\n",
       "FlightDate_s            datetime64[s]\n",
       "dtype: object"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "bfb512ae-e250-413a-8c43-290f586b32e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>city</th>\n",
       "      <th>lat</th>\n",
       "      <th>lng</th>\n",
       "      <th>population</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>Moscow</td>\n",
       "      <td>55.750599</td>\n",
       "      <td>37.617496</td>\n",
       "      <td>17332000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>108</th>\n",
       "      <td>Saint Petersburg</td>\n",
       "      <td>59.937500</td>\n",
       "      <td>30.308599</td>\n",
       "      <td>5597763</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>475</th>\n",
       "      <td>Novosibirsk</td>\n",
       "      <td>55.049999</td>\n",
       "      <td>82.950005</td>\n",
       "      <td>1633851</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>525</th>\n",
       "      <td>Yekaterinburg</td>\n",
       "      <td>56.835598</td>\n",
       "      <td>60.612797</td>\n",
       "      <td>1468833</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>594</th>\n",
       "      <td>Kazan</td>\n",
       "      <td>55.796402</td>\n",
       "      <td>49.108898</td>\n",
       "      <td>1259173</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 city        lat        lng  population\n",
       "18             Moscow  55.750599  37.617496    17332000\n",
       "108  Saint Petersburg  59.937500  30.308599     5597763\n",
       "475       Novosibirsk  55.049999  82.950005     1633851\n",
       "525     Yekaterinburg  56.835598  60.612797     1468833\n",
       "594             Kazan  55.796402  49.108898     1259173"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cities_df = cudf.read_csv('worldcities.csv', dtype={\n",
    "    'lat': 'float32',\n",
    "    'lng': 'float32',\n",
    "    'population': 'uint32',\n",
    "})\n",
    "cities_df = cities_df[cities_df['country'] == 'Russia']\n",
    "cities_df.drop(columns=['city_ascii', 'country', 'iso2', 'iso3',\\\n",
    "                        'admin_name', 'capital', 'id'], inplace = 1)\n",
    "cities_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "97c13fe2-6308-4aee-bc82-9cfdafacb945",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 124836 characters]",
      "text/plain": [
       "<Figure size 1000x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import datashader as ds\n",
    "import datashader.transfer_functions as tf\n",
    "from datashader.colors import inferno\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "canvas = ds.Canvas(\n",
    "    x_range=(19, 169),\n",
    "    y_range=(41, 82),\n",
    "    plot_width=800,\n",
    "    plot_height=400\n",
    ")\n",
    "\n",
    "agg = canvas.points(cities_df, 'lng', 'lat')\n",
    "spread = tf.spread(agg, px=3)\n",
    "img = tf.shade(spread, cmap=inferno, how='log')\n",
    "\n",
    "plt.figure(figsize=(10, 5))\n",
    "plt.imshow(img.to_pil(), origin='lower')\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "2ee12a60-8440-4af2-aa18-81a5088ae347",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unique_documents = df.groupby('PassengerName')['PassengerDocument'].nunique()\n",
    "unique_documents.head().max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "46b5a759-95f6-45ef-8342-da1044629a84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PassengerDocument\n",
       "1548113497    16\n",
       "7126372174    15\n",
       "8816864475    14\n",
       "5293500602    14\n",
       "6937933652    14\n",
       "dtype: int64"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flight_count = df.groupby('PassengerDocument').size().sort_values(ascending=False)\n",
    "flight_count.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "cd4ddd40-245c-4649-8fdd-50eb85093ac6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "48 22.92 2.08\n"
     ]
    }
   ],
   "source": [
    "unique_dest = df['Destination'].nunique()\n",
    "passenger_dest = df.groupby('PassengerDocument')['Destination'].nunique() / unique_dest * 100\n",
    "passenger_dest = passenger_dest.round(2)\n",
    "print(unique_dest, passenger_dest.max(), passenger_dest.min())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "cd5f7f62-dd65-46f8-83f4-b3bf64e84543",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-01-01T22:50:00 2017-01-01T00:00:00\n"
     ]
    }
   ],
   "source": [
    "print(df['FlightDate_s'].max(), df['FlightDate_s'].min())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "63a5a4f0-cbe1-4ce5-8f34-9dc7fa3d16d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2000 1970\n"
     ]
    }
   ],
   "source": [
    "birth_date = df['PassengerBirthDate_y'].sort_values(ascending=True)\n",
    "print(birth_date.max(), birth_date.min())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "1aca0e78-fdc5-477e-ac85-06923fd1d49f",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3524809427.py, line 7)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[140], line 7\u001b[0;36m\u001b[0m\n\u001b[0;31m    passenger_dt_trigger = avg_passenger < avg_total ? 1 - avg_passenger / avg_total : 0\u001b[0m\n\u001b[0m                                                     ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "df_sorted = df.sort_values(['PassengerDocument','FlightDate_s'])\n",
    "df_sorted['PrevFlight_s'] = df_sorted.groupby('PassengerDocument')['FlightDate_s'].shift(1)\n",
    "df_sorted['FlightDt_s'] = df_sorted['FlightDate_s'] - df_sorted['PrevFlight_s']\n",
    "avg_passenger = df_sorted.groupby('PassengerDocument')['FlightDt_s'].mean()\n",
    "avg_total = avg_passenger.mean()\n",
    "\n",
    "passenger_dt_trigger = avg_passenger < avg_total ? 1 - avg_passenger / avg_total : 0\n",
    "passenger_dt_trigger.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed4b01d4-1ff0-42f0-a105-14a6f07e912a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
