{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# 📅 Работа с датой и временем в Pandas"
      ],
      "metadata": {
        "id": "EpIvo1sidsYX"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "В Data Science время — это не просто строка. Это ось, по которой мы можем анализировать тренды, считать длительность событий и группировать данные.\n",
        "В этом ноутбуке разберем:\n",
        "1. Создание datetime объектов.\n",
        "2. Извлечение признаков (Feature Extraction).\n",
        "3. Математические операции со временем."
      ],
      "metadata": {
        "id": "WZ9keSr2dwf2"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import pandas as pd\n",
        "import numpy as np"
      ],
      "metadata": {
        "id": "Gq9CvdfGd05l"
      },
      "execution_count": 1,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Преобразование в datetime"
      ],
      "metadata": {
        "id": "5WgSw0HWd34F"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Часто данные приходят в виде строк. Pandas умеет автоматически или по заданному формату парсить их в тип `datetime64[ns]`"
      ],
      "metadata": {
        "id": "Mm3Q-XtAd7wN"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Пример: логи входа игроков в Rublox\n",
        "logins = ['2026-11-08 14:30:00', '2026-11-08 15:45:00', '2026-11-09 10:00:00']\n",
        "\n",
        "# Преобразуем в datetime\n",
        "dt_series = pd.to_datetime(logins)\n",
        "print(dt_series)\n",
        "print(f\"Тип данных: {dt_series.dtype}\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "uEuwkYPVd_lL",
        "outputId": "d0dd72c4-ad12-444b-fdd3-0c2da7ae4281"
      },
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "DatetimeIndex(['2026-11-08 14:30:00', '2026-11-08 15:45:00',\n",
            "               '2026-11-09 10:00:00'],\n",
            "              dtype='datetime64[ns]', freq=None)\n",
            "Тип данных: datetime64[ns]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Извлечение компонентов"
      ],
      "metadata": {
        "id": "iIsepuMleUiN"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Как только столбец стал типом `datetime`, мы можем использовать аксессор `.dt`, чтобы \"вытащить\" из него год, месяц, день недели и т.д. Это супер-полезно для аналитики!"
      ],
      "metadata": {
        "id": "cy5jIPiEeYfN"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "df = pd.DataFrame({'login_time': dt_series})\n",
        "\n",
        "# Извлекаем признаки\n",
        "df['year'] = df['login_time'].dt.year\n",
        "df['month'] = df['login_time'].dt.month\n",
        "df['day'] = df['login_time'].dt.day\n",
        "df['hour'] = df['login_time'].dt.hour\n",
        "df['dayofweek'] = df['login_time'].dt.dayofweek # 0 = Понедельник, 6 = Воскресенье\n",
        "df['day_name'] = df['login_time'].dt.day_name() # Название дня\n",
        "\n",
        "df"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 143
        },
        "id": "-0KF54uxec49",
        "outputId": "be6a7f63-d8ba-4241-83f1-52116cb27fe7"
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "           login_time  year  month  day  hour  dayofweek day_name\n",
              "0 2026-11-08 14:30:00  2026     11    8    14          6   Sunday\n",
              "1 2026-11-08 15:45:00  2026     11    8    15          6   Sunday\n",
              "2 2026-11-09 10:00:00  2026     11    9    10          0   Monday"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-6f4f1be7-0bbd-450f-b141-ed34da95d44d\" class=\"colab-df-container\">\n",
              "    <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>login_time</th>\n",
              "      <th>year</th>\n",
              "      <th>month</th>\n",
              "      <th>day</th>\n",
              "      <th>hour</th>\n",
              "      <th>dayofweek</th>\n",
              "      <th>day_name</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>2026-11-08 14:30:00</td>\n",
              "      <td>2026</td>\n",
              "      <td>11</td>\n",
              "      <td>8</td>\n",
              "      <td>14</td>\n",
              "      <td>6</td>\n",
              "      <td>Sunday</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>2026-11-08 15:45:00</td>\n",
              "      <td>2026</td>\n",
              "      <td>11</td>\n",
              "      <td>8</td>\n",
              "      <td>15</td>\n",
              "      <td>6</td>\n",
              "      <td>Sunday</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>2026-11-09 10:00:00</td>\n",
              "      <td>2026</td>\n",
              "      <td>11</td>\n",
              "      <td>9</td>\n",
              "      <td>10</td>\n",
              "      <td>0</td>\n",
              "      <td>Monday</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "    <div class=\"colab-df-buttons\">\n",
              "\n",
              "  <div class=\"colab-df-container\">\n",
              "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-6f4f1be7-0bbd-450f-b141-ed34da95d44d')\"\n",
              "            title=\"Convert this dataframe to an interactive table.\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
              "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
              "  </svg>\n",
              "    </button>\n",
              "\n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    .colab-df-buttons div {\n",
              "      margin-bottom: 4px;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "    <script>\n",
              "      const buttonEl =\n",
              "        document.querySelector('#df-6f4f1be7-0bbd-450f-b141-ed34da95d44d button.colab-df-convert');\n",
              "      buttonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "      async function convertToInteractive(key) {\n",
              "        const element = document.querySelector('#df-6f4f1be7-0bbd-450f-b141-ed34da95d44d');\n",
              "        const dataTable =\n",
              "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                    [key], {});\n",
              "        if (!dataTable) return;\n",
              "\n",
              "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "          + ' to learn more about interactive tables.';\n",
              "        element.innerHTML = '';\n",
              "        dataTable['output_type'] = 'display_data';\n",
              "        await google.colab.output.renderOutput(dataTable, element);\n",
              "        const docLink = document.createElement('div');\n",
              "        docLink.innerHTML = docLinkHtml;\n",
              "        element.appendChild(docLink);\n",
              "      }\n",
              "    </script>\n",
              "  </div>\n",
              "\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "dataframe",
              "variable_name": "df",
              "summary": "{\n  \"name\": \"df\",\n  \"rows\": 3,\n  \"fields\": [\n    {\n      \"column\": \"login_time\",\n      \"properties\": {\n        \"dtype\": \"date\",\n        \"min\": \"2026-11-08 14:30:00\",\n        \"max\": \"2026-11-09 10:00:00\",\n        \"num_unique_values\": 3,\n        \"samples\": [\n          \"2026-11-08 14:30:00\",\n          \"2026-11-08 15:45:00\",\n          \"2026-11-09 10:00:00\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"year\",\n      \"properties\": {\n        \"dtype\": \"int32\",\n        \"num_unique_values\": 1,\n        \"samples\": [\n          2026\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"month\",\n      \"properties\": {\n        \"dtype\": \"int32\",\n        \"num_unique_values\": 1,\n        \"samples\": [\n          11\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"day\",\n      \"properties\": {\n        \"dtype\": \"int32\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          9\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"hour\",\n      \"properties\": {\n        \"dtype\": \"int32\",\n        \"num_unique_values\": 3,\n        \"samples\": [\n          14\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"dayofweek\",\n      \"properties\": {\n        \"dtype\": \"int32\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"day_name\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          \"Monday\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}"
            }
          },
          "metadata": {},
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Математика времени Timedelta"
      ],
      "metadata": {
        "id": "pR7I5MoiefAU"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "`Timedelta` — это разница между двумя моментами времени. Отлично подходит для расчета длительности сессий."
      ],
      "metadata": {
        "id": "VxkphzXvejhV"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Добавим время выхода из игры\n",
        "df['logout_time'] = pd.to_datetime(['2026-11-08 16:00:00', '2026-11-08 17:15:00', '2026-11-09 11:30:00'])\n",
        "\n",
        "# Считаем время в игре\n",
        "df['session_duration'] = df['logout_time'] - df['login_time']\n",
        "print(df['session_duration'])\n",
        "\n",
        "# Можем перевести в минуты или часы\n",
        "df['duration_minutes'] = df['session_duration'].dt.total_seconds() / 60\n",
        "df"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 213
        },
        "id": "Y9kai7zxelud",
        "outputId": "c6d3e614-7cee-41a1-934a-6e7de022164d"
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "0   0 days 01:30:00\n",
            "1   0 days 01:30:00\n",
            "2   0 days 01:30:00\n",
            "Name: session_duration, dtype: timedelta64[ns]\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "           login_time  year  month  day  hour  dayofweek day_name  \\\n",
              "0 2026-11-08 14:30:00  2026     11    8    14          6   Sunday   \n",
              "1 2026-11-08 15:45:00  2026     11    8    15          6   Sunday   \n",
              "2 2026-11-09 10:00:00  2026     11    9    10          0   Monday   \n",
              "\n",
              "          logout_time session_duration  duration_minutes  \n",
              "0 2026-11-08 16:00:00  0 days 01:30:00              90.0  \n",
              "1 2026-11-08 17:15:00  0 days 01:30:00              90.0  \n",
              "2 2026-11-09 11:30:00  0 days 01:30:00              90.0  "
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-518a2cb0-ba1e-4f6a-92b5-cafa1d6eb08d\" class=\"colab-df-container\">\n",
              "    <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>login_time</th>\n",
              "      <th>year</th>\n",
              "      <th>month</th>\n",
              "      <th>day</th>\n",
              "      <th>hour</th>\n",
              "      <th>dayofweek</th>\n",
              "      <th>day_name</th>\n",
              "      <th>logout_time</th>\n",
              "      <th>session_duration</th>\n",
              "      <th>duration_minutes</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>2026-11-08 14:30:00</td>\n",
              "      <td>2026</td>\n",
              "      <td>11</td>\n",
              "      <td>8</td>\n",
              "      <td>14</td>\n",
              "      <td>6</td>\n",
              "      <td>Sunday</td>\n",
              "      <td>2026-11-08 16:00:00</td>\n",
              "      <td>0 days 01:30:00</td>\n",
              "      <td>90.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>2026-11-08 15:45:00</td>\n",
              "      <td>2026</td>\n",
              "      <td>11</td>\n",
              "      <td>8</td>\n",
              "      <td>15</td>\n",
              "      <td>6</td>\n",
              "      <td>Sunday</td>\n",
              "      <td>2026-11-08 17:15:00</td>\n",
              "      <td>0 days 01:30:00</td>\n",
              "      <td>90.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>2026-11-09 10:00:00</td>\n",
              "      <td>2026</td>\n",
              "      <td>11</td>\n",
              "      <td>9</td>\n",
              "      <td>10</td>\n",
              "      <td>0</td>\n",
              "      <td>Monday</td>\n",
              "      <td>2026-11-09 11:30:00</td>\n",
              "      <td>0 days 01:30:00</td>\n",
              "      <td>90.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "    <div class=\"colab-df-buttons\">\n",
              "\n",
              "  <div class=\"colab-df-container\">\n",
              "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-518a2cb0-ba1e-4f6a-92b5-cafa1d6eb08d')\"\n",
              "            title=\"Convert this dataframe to an interactive table.\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
              "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
              "  </svg>\n",
              "    </button>\n",
              "\n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    .colab-df-buttons div {\n",
              "      margin-bottom: 4px;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "    <script>\n",
              "      const buttonEl =\n",
              "        document.querySelector('#df-518a2cb0-ba1e-4f6a-92b5-cafa1d6eb08d button.colab-df-convert');\n",
              "      buttonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "      async function convertToInteractive(key) {\n",
              "        const element = document.querySelector('#df-518a2cb0-ba1e-4f6a-92b5-cafa1d6eb08d');\n",
              "        const dataTable =\n",
              "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                    [key], {});\n",
              "        if (!dataTable) return;\n",
              "\n",
              "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "          + ' to learn more about interactive tables.';\n",
              "        element.innerHTML = '';\n",
              "        dataTable['output_type'] = 'display_data';\n",
              "        await google.colab.output.renderOutput(dataTable, element);\n",
              "        const docLink = document.createElement('div');\n",
              "        docLink.innerHTML = docLinkHtml;\n",
              "        element.appendChild(docLink);\n",
              "      }\n",
              "    </script>\n",
              "  </div>\n",
              "\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "dataframe",
              "variable_name": "df",
              "summary": "{\n  \"name\": \"df\",\n  \"rows\": 3,\n  \"fields\": [\n    {\n      \"column\": \"login_time\",\n      \"properties\": {\n        \"dtype\": \"date\",\n        \"min\": \"2026-11-08 14:30:00\",\n        \"max\": \"2026-11-09 10:00:00\",\n        \"num_unique_values\": 3,\n        \"samples\": [\n          \"2026-11-08 14:30:00\",\n          \"2026-11-08 15:45:00\",\n          \"2026-11-09 10:00:00\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"year\",\n      \"properties\": {\n        \"dtype\": \"int32\",\n        \"num_unique_values\": 1,\n        \"samples\": [\n          2026\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"month\",\n      \"properties\": {\n        \"dtype\": \"int32\",\n        \"num_unique_values\": 1,\n        \"samples\": [\n          11\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"day\",\n      \"properties\": {\n        \"dtype\": \"int32\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          9\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"hour\",\n      \"properties\": {\n        \"dtype\": \"int32\",\n        \"num_unique_values\": 3,\n        \"samples\": [\n          14\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"dayofweek\",\n      \"properties\": {\n        \"dtype\": \"int32\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"day_name\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          \"Monday\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"logout_time\",\n      \"properties\": {\n        \"dtype\": \"date\",\n        \"min\": \"2026-11-08 16:00:00\",\n        \"max\": \"2026-11-09 11:30:00\",\n        \"num_unique_values\": 3,\n        \"samples\": [\n          \"2026-11-08 16:00:00\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"session_duration\",\n      \"properties\": {\n        \"dtype\": \"timedelta64[ns]\",\n        \"num_unique_values\": 1,\n        \"samples\": [\n          \"0 days 01:30:00\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"duration_minutes\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.0,\n        \"min\": 90.0,\n        \"max\": 90.0,\n        \"num_unique_values\": 1,\n        \"samples\": [\n          90.0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}"
            }
          },
          "metadata": {},
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 🚀 Практика: Аналитика сайта \"Школьные конспекты\""
      ],
      "metadata": {
        "id": "iM0Lrh8QeyUP"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Ты запустил свой сайт для обмена конспектами. У тебя есть сырые данные о том, когда пользователи скачивали материалы.\n",
        "Твоя задача: очистить данные, извлечь признаки и ответить на бизнес-вопросы."
      ],
      "metadata": {
        "id": "EofFv9kke2Z8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import pandas as pd\n",
        "\n",
        "# Сырые данные (обрати внимание, что даты в разных форматах и есть пропуски/ошибки)\n",
        "raw_data = {\n",
        "    'user_id': [1, 2, 3, 4, 5, 6, 7],\n",
        "    'download_time': [\n",
        "        '2026-11-02 09:15:00', # Понедельник\n",
        "        '03.11.2026 14:30',    # Вторник (другой формат)\n",
        "        '2026-11-04 18:00:00', # Среда\n",
        "        'invalid_date',        # Ошибка в данных\n",
        "        '2026-11-07 10:00:00', # Суббота\n",
        "        '2026-11-08 23:45:00', # Воскресенье (сегодня!)\n",
        "        '2026-11-02 11:00:00'  # Понедельник\n",
        "    ],\n",
        "    'subject': ['Math', 'English', 'Physics', 'Math', 'IT', 'IT', 'English']\n",
        "}\n",
        "\n",
        "df = pd.DataFrame(raw_data)\n",
        "df"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 269
        },
        "id": "oPsRXrE-e6bc",
        "outputId": "7b3cefcb-7593-4377-fb7a-205cf25a48a4"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "   user_id        download_time  subject\n",
              "0        1  2026-11-02 09:15:00     Math\n",
              "1        2     03.11.2026 14:30  English\n",
              "2        3  2026-11-04 18:00:00  Physics\n",
              "3        4         invalid_date     Math\n",
              "4        5  2026-11-07 10:00:00       IT\n",
              "5        6  2026-11-08 23:45:00       IT\n",
              "6        7  2026-11-02 11:00:00  English"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-1eb6ee62-f118-4c65-acaa-aad94c36038a\" class=\"colab-df-container\">\n",
              "    <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>user_id</th>\n",
              "      <th>download_time</th>\n",
              "      <th>subject</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>1</td>\n",
              "      <td>2026-11-02 09:15:00</td>\n",
              "      <td>Math</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>2</td>\n",
              "      <td>03.11.2026 14:30</td>\n",
              "      <td>English</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>3</td>\n",
              "      <td>2026-11-04 18:00:00</td>\n",
              "      <td>Physics</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>4</td>\n",
              "      <td>invalid_date</td>\n",
              "      <td>Math</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>5</td>\n",
              "      <td>2026-11-07 10:00:00</td>\n",
              "      <td>IT</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>6</td>\n",
              "      <td>2026-11-08 23:45:00</td>\n",
              "      <td>IT</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6</th>\n",
              "      <td>7</td>\n",
              "      <td>2026-11-02 11:00:00</td>\n",
              "      <td>English</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "    <div class=\"colab-df-buttons\">\n",
              "\n",
              "  <div class=\"colab-df-container\">\n",
              "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-1eb6ee62-f118-4c65-acaa-aad94c36038a')\"\n",
              "            title=\"Convert this dataframe to an interactive table.\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
              "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
              "  </svg>\n",
              "    </button>\n",
              "\n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    .colab-df-buttons div {\n",
              "      margin-bottom: 4px;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "    <script>\n",
              "      const buttonEl =\n",
              "        document.querySelector('#df-1eb6ee62-f118-4c65-acaa-aad94c36038a button.colab-df-convert');\n",
              "      buttonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "      async function convertToInteractive(key) {\n",
              "        const element = document.querySelector('#df-1eb6ee62-f118-4c65-acaa-aad94c36038a');\n",
              "        const dataTable =\n",
              "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                    [key], {});\n",
              "        if (!dataTable) return;\n",
              "\n",
              "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "          + ' to learn more about interactive tables.';\n",
              "        element.innerHTML = '';\n",
              "        dataTable['output_type'] = 'display_data';\n",
              "        await google.colab.output.renderOutput(dataTable, element);\n",
              "        const docLink = document.createElement('div');\n",
              "        docLink.innerHTML = docLinkHtml;\n",
              "        element.appendChild(docLink);\n",
              "      }\n",
              "    </script>\n",
              "  </div>\n",
              "\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "dataframe",
              "variable_name": "df",
              "summary": "{\n  \"name\": \"df\",\n  \"rows\": 7,\n  \"fields\": [\n    {\n      \"column\": \"user_id\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 2,\n        \"min\": 1,\n        \"max\": 7,\n        \"num_unique_values\": 7,\n        \"samples\": [\n          1,\n          2,\n          6\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"download_time\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 7,\n        \"samples\": [\n          \"2026-11-02 09:15:00\",\n          \"03.11.2026 14:30\",\n          \"2026-11-08 23:45:00\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"subject\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 4,\n        \"samples\": [\n          \"English\",\n          \"IT\",\n          \"Math\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}"
            }
          },
          "metadata": {},
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Очистка и парсинг дат"
      ],
      "metadata": {
        "id": "9wLAViVhe_nU"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Преобразуй столбец `download_time` в формат `datetime`.\n",
        "Подсказка: Используй `pd.to_datetime(..., errors='coerce')`, чтобы некорректные даты (как 'invalid_date') превратились в `NaT` (Not a Time), а не вызывали ошибку. Удали строки с `NaT`."
      ],
      "metadata": {
        "id": "rkGW6RsbfC3A"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# ТВОЙ КОД ЗДЕСЬ"
      ],
      "metadata": {
        "id": "JdxoxkeHfNz8"
      },
      "execution_count": 9,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Извлечение признаков"
      ],
      "metadata": {
        "id": "hAvklS3gfPqc"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Создай новые столбцы:\n",
        "1. day_of_week (число от 0 до 6)\n",
        "2. is_weekend (булево значение: True если суббота или воскресенье)\n",
        "3. hour (час скачивания)"
      ],
      "metadata": {
        "id": "awZMk5eAfXEr"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# ТВОЙ КОД ЗДЕСЬ"
      ],
      "metadata": {
        "id": "SOQLXm__fb4z"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Бизнес-аналитика"
      ],
      "metadata": {
        "id": "O2K6OK5TfhSc"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Ответь на вопросы, используя группировку (groupby) и фильтрацию:\n",
        "1. В какие дни недели (названия дней) пользователи скачивают конспекты чаще всего?\n",
        "2. Какой процент скачиваний приходится на выходные (is_weekend)?\n",
        "3. Во сколько часов (от 0 до 23) пик активности пользователей?"
      ],
      "metadata": {
        "id": "gQ3B95eifl-r"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# ТВОЙ КОД ЗДЕСЬ"
      ],
      "metadata": {
        "id": "bfPZtpRRfp5M"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}