{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#
Pandas Datetime [25 exercises with solution]
" ] }, { "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
citystate/provincecountryUFO_shapelength_of_encounter_secondsdescribed_duration_of_encounterdescriptiondate_documentedlatitudelongitude
Date_time
1910-06-01 15:00:00wills pointtxuscigar1202 minutesCigar shaped object moving from West to East04/16/200532.709167-96.008056
1920-06-11 21:00:00ciceroinusunknown601 minute((NUFORC Note: Probable hoax. Note date. PD...05/12/200940.123889-86.013333
1929-07-05 14:00:00buchanan (or burns)orusdisk601minwe were traveling east of burns&#44clmbing up ...08/16/200243.642500-118.627500
1931-06-01 13:00:00abileneksusdisk180030 min. approx.This is a what my mother related to me regardi...02/14/200638.917222-97.213611
1939-06-01 20:00:00waterlooalusfireball3005 minutes4 high school students see bright&#44 ground-l...02/21/201434.918056-88.064167
\n", "
" ], "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": [ "![UFO](https://www.w3resource.com/w3r_images/ufo.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![Column Name Description](https://i.imgur.com/UrE9JKv.png)" ] }, { "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]
\n", "Exercises data sources: ufo.csv - [347 random records from ufo_sighting_data.csv]
\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 }