{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# <center>Pandas Datetime [25 exercises with solution]</center>" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Importing necessary libraries:\n", "\n", "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 3, "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>state/province</th>\n", " <th>country</th>\n", " <th>UFO_shape</th>\n", " <th>length_of_encounter_seconds</th>\n", " <th>described_duration_of_encounter</th>\n", " <th>description</th>\n", " <th>date_documented</th>\n", " <th>latitude</th>\n", " <th>longitude</th>\n", " </tr>\n", " <tr>\n", " <th>Date_time</th>\n", " <th></th>\n", " <th></th>\n", " <th></th>\n", " <th></th>\n", " <th></th>\n", " <th></th>\n", " <th></th>\n", " <th></th>\n", " <th></th>\n", " <th></th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>1910-06-01 15:00:00</th>\n", " <td>wills point</td>\n", " <td>tx</td>\n", " <td>us</td>\n", " <td>cigar</td>\n", " <td>120</td>\n", " <td>2 minutes</td>\n", " <td>Cigar shaped object moving from West to East</td>\n", " <td>04/16/2005</td>\n", " <td>32.709167</td>\n", " <td>-96.008056</td>\n", " </tr>\n", " <tr>\n", " <th>1920-06-11 21:00:00</th>\n", " <td>cicero</td>\n", " <td>in</td>\n", " <td>us</td>\n", " <td>unknown</td>\n", " <td>60</td>\n", " <td>1 minute</td>\n", " <td>((NUFORC Note: Probable hoax. Note date. PD...</td>\n", " <td>05/12/2009</td>\n", " <td>40.123889</td>\n", " <td>-86.013333</td>\n", " </tr>\n", " <tr>\n", " <th>1929-07-05 14:00:00</th>\n", " <td>buchanan (or burns)</td>\n", " <td>or</td>\n", " <td>us</td>\n", " <td>disk</td>\n", " <td>60</td>\n", " <td>1min</td>\n", " <td>we were traveling east of burns&#44clmbing up ...</td>\n", " <td>08/16/2002</td>\n", " <td>43.642500</td>\n", " <td>-118.627500</td>\n", " </tr>\n", " <tr>\n", " <th>1931-06-01 13:00:00</th>\n", " <td>abilene</td>\n", " <td>ks</td>\n", " <td>us</td>\n", " <td>disk</td>\n", " <td>1800</td>\n", " <td>30 min. approx.</td>\n", " <td>This is a what my mother related to me regardi...</td>\n", " <td>02/14/2006</td>\n", " <td>38.917222</td>\n", " <td>-97.213611</td>\n", " </tr>\n", " <tr>\n", " <th>1939-06-01 20:00:00</th>\n", " <td>waterloo</td>\n", " <td>al</td>\n", " <td>us</td>\n", " <td>fireball</td>\n", " <td>300</td>\n", " <td>5 minutes</td>\n", " <td>4 high school students see bright&#44 ground-l...</td>\n", " <td>02/21/2014</td>\n", " <td>34.918056</td>\n", " <td>-88.064167</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " city state/province country UFO_shape \\\n", "Date_time \n", "1910-06-01 15:00:00 wills point tx us cigar \n", "1920-06-11 21:00:00 cicero in us unknown \n", "1929-07-05 14:00:00 buchanan (or burns) or us disk \n", "1931-06-01 13:00:00 abilene ks us disk \n", "1939-06-01 20:00:00 waterloo al us fireball \n", "\n", " length_of_encounter_seconds \\\n", "Date_time \n", "1910-06-01 15:00:00 120 \n", "1920-06-11 21:00:00 60 \n", "1929-07-05 14:00:00 60 \n", "1931-06-01 13:00:00 1800 \n", "1939-06-01 20:00:00 300 \n", "\n", " described_duration_of_encounter \\\n", "Date_time \n", "1910-06-01 15:00:00 2 minutes \n", "1920-06-11 21:00:00 1 minute \n", "1929-07-05 14:00:00 1min \n", "1931-06-01 13:00:00 30 min. approx. \n", "1939-06-01 20:00:00 5 minutes \n", "\n", " description \\\n", "Date_time \n", "1910-06-01 15:00:00 Cigar shaped object moving from West to East \n", "1920-06-11 21:00:00 ((NUFORC Note: Probable hoax. Note date. PD... \n", "1929-07-05 14:00:00 we were traveling east of burns,clmbing up ... \n", "1931-06-01 13:00:00 This is a what my mother related to me regardi... \n", "1939-06-01 20:00:00 4 high school students see bright, ground-l... \n", "\n", " date_documented latitude longitude \n", "Date_time \n", "1910-06-01 15:00:00 04/16/2005 32.709167 -96.008056 \n", "1920-06-11 21:00:00 05/12/2009 40.123889 -86.013333 \n", "1929-07-05 14:00:00 08/16/2002 43.642500 -118.627500 \n", "1931-06-01 13:00:00 02/14/2006 38.917222 -97.213611 \n", "1939-06-01 20:00:00 02/21/2014 34.918056 -88.064167 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Knowing about the csv file:\n", "\n", "ufo_data = pd.read_csv('ufo.csv', index_col = 'Date_time', parse_dates = True)\n", "ufo_data.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 25 Exercises Solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. Write a Pandas program to create the todays date." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Today's Date-> 2021-02-09\n" ] } ], "source": [ "from datetime import date\n", "\n", "now = pd.to_datetime(date.today()) # '2019-11-02 00:00:00'\n", "date = str(now).split()[0] # After split: ['2019-11-02', '00:00:00']\n", "print('Today\\'s Date->', date)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. Write a Pandas program to calculate all the sighting days of the unidentified flying object (ufo) from current date." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Diff in Days\n", "0 40430\n", "1 36767\n", "2 33456\n", "3 32760\n", "4 29838\n", ".. ...\n", "342 6153\n", "343 5969\n", "344 5937\n", "345 5906\n", "346 6208\n", "\n", "[347 rows x 1 columns]\n" ] } ], "source": [ "date_diff = (now - ufo_data.index).days # difference of corresponding time in days\n", "result = date_diff.to_frame(index = False, name='Diff in Days')\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Write a Pandas program to get the current date, oldest date and number of days between Current date and oldest date of Ufo dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. Write a Pandas program to get all the sighting days of the unidentified flying object (ufo) which are less than or equal to 40 years (365*40 days)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5. Write a Pandas program to get all the sighting days of the unidentified flying object (ufo) between 1950-10-10 and 1960-10-10." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6. Write a Pandas program to get all the sighting years of the unidentified flying object (ufo) and create the year as column." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7. Write a Pandas program to create a plot to present the number of unidentified flying object (UFO) reports per year." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 8. Write a Pandas program to extract year, month, day, hour, minute, second and weekday from unidentified flying object (UFO) reporting date." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 9. Write a Pandas program to convert given datetime to timestamp. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 10. Write a Pandas program to count year-country wise frequency of reporting dates of unidentified flying object(UFO). " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 11. Write a Pandas program to extract unique reporting dates of unidentified flying object (UFO)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 12. Write a Pandas program to get the difference (in days) between documented date and reporting date of unidentified flying object (UFO)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 13. Write a Pandas program to add 100 days with reporting date of unidentified flying object (UFO). " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 14. Write a Pandas program to generate sequences of fixed-frequency dates and time spans." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 15. Write a Pandas program to create a conversion between strings and datetime." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 16. Write a Pandas program to manipulate and convert date times with timezone information." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 17. Write a Pandas program to get the average mean of the UFO (unidentified flying object) sighting was reported." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 18. Write a Pandas program to create a graphical analysis of UFO (unidentified flying object) Sightings year." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 19. Write a Pandas program to check the empty values of UFO (unidentified flying object) Dataframe." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 20. Write a Pandas program to create a plot of distribution of UFO (unidentified flying object) observation time." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 21. Write a Pandas program to create a graphical analysis of UFO (unidentified flying object) sighted by month." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 22. Write a Pandas program to create a comparison of the top 10 years in which the UFO was sighted vs the hours of the day." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 23. Write a Pandas program to create a comparison of the top 10 years in which the UFO was sighted vs each Month." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 24. Write a Pandas program to create a heatmap (rectangular data as a color-encoded matrix) for comparison of the top 10 years in which the UFO was sighted vs each Month. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 25. Write a Pandas program to create a Timewheel of Hour Vs Year comparison of the top 10 years in which the UFO was sighted." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Data Sources: ufo_sighting_data.csv - [80,332 records] <br>\n", "Exercises data sources: ufo.csv - [347 random records from ufo_sighting_data.csv] <br>\n", "\n", "Note there are no missing data in the columns (ufo.csv).\n", "\n", "Source:\n", "- https://github.com/planetsig/ufo-reports \n", "- https://www.kaggle.com/camnugent/ufo-sightings-around-the-world" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference: [https://www.w3resource.com/python-exercises/pandas/datetime/index.php](https://www.w3resource.com/python-exercises/pandas/datetime/index.php)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.5" } }, "nbformat": 4, "nbformat_minor": 2 }