w3resource

Pandas Datetime: Exercises, Practice, Solution

[An editor is available at the bottom of the page to write and execute the scripts.]

Pandas Datetime [25 exercises with solution]

ufo.csv

Extraterrestrials, visitors, little green men, UFOs, swap gas. What do they want? Where do they come from? Do they like cheeseburgers? This dataset will likely not help you answer these questions. It does contain over 80,000 records of UFO sightings dating back as far as 1949. With the latitude and longitude data it is possible to assess the global distribution of UFO sightings (patterns could aid in planetary defense if invasion proves to be imminent). The dates and times, along with the duration of the UFO's stay and description of the craft also lend themselves to predictions. Can we find patterns in their arrival times and durations? Do aliens work on weekends? Help defend the planet and learn about your fellow earthlings (and when they are most likely to see ET).

Pandas Datetime Exercises

Data Sources: ufo_sighting_data.csv - [80,332 records]

Exercises data sources: ufo.csv - [347 random records from ufo_sighting_data.csv]

Content

Column Name Description
Date_time standardized date and time of sighting
city location of UFO sighting
state/province the US state or Canadian province, appears blank for other locations
country Country of UFO sighting
UFO_shape a one word description of the "spacecraft"
length_of_encounter_seconds standardized to seconds, length of the observation of the UFO
described_duration _of_encounter raw description of the length of the encounter (shows uncertainty to previous column)
description text description of the UFO encounter. Warning column is messy, with some curation it could lend itself to some natural language processing and sentiment analysis.
date_documented when was the UFO sighting reported
latitude latitude
longitude longitude

Note: There are no missing data in the columns (ufo.csv).

Source:

(a) https://github.com/planetsig/ufo-reports (b) https://www.kaggle.com/camnugent/ufo-sightings-around-the-world

1. Write a Pandas program to create the todays date. Go to the editor
Click me to see the sample solution

2. Write a Pandas program to calculate all the sighting days of the unidentified flying object (ufo) from current date. Go to the editor
Click me to see the sample solution

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. Go to the editor
Click me to see the sample solution

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). Go to the editor
Click me to see the sample solution

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. Go to the editor
Click me to see the sample solution

6. Write a Pandas program to get all the sighting years of the unidentified flying object (ufo) and create the year as column. Go to the editor
Click me to see the sample solution

7. Write a Pandas program to create a plot to present the number of unidentified flying object (UFO) reports per year. Go to the editor
Click me to see the sample solution

8. Write a Pandas program to extract year, month, day, hour, minute, second and weekday from unidentified flying object (UFO) reporting date. Go to the editor
Click me to see the sample solution

9. Write a Pandas program to convert given datetime to timestamp. Go to the editor
Click me to see the sample solution

10. Write a Pandas program to count year-country wise frequency of reporting dates of unidentified flying object(UFO). Go to the editor
Click me to see the sample solution

11. Write a Pandas program to extract unique reporting dates of unidentified flying object (UFO). Go to the editor
Click me to see the sample solution

12. Write a Pandas program to get the difference (in days) between documented date and reporting date of unidentified flying object (UFO). Go to the editor
Click me to see the sample solution

13. Write a Pandas program to add 100 days with reporting date of unidentified flying object (UFO). Go to the editor
Click me to see the sample solution

14. Write a Pandas program to generate sequences of fixed-frequency dates and time spans. Go to the editor
Click me to see the sample solution

15. Write a Pandas program to create a conversion between strings and datetime. Go to the editor
Click me to see the sample solution

16. Write a Pandas program to manipulate and convert date times with timezone information. Go to the editor
Click me to see the sample solution

17. Write a Pandas program to get the average mean of the UFO (unidentified flying object) sighting was reported. Go to the editor
Click me to see the sample solution

18. Write a Pandas program to create a graphical analysis of UFO (unidentified flying object) Sightings year. Go to the editor
Click me to see the sample solution

19. Write a Pandas program to check the empty values of UFO (unidentified flying object) Dataframe. Go to the editor
Click me to see the sample solution

20. Write a Pandas program to create a plot of distribution of UFO (unidentified flying object) observation time. Go to the editor
Click me to see the sample solution

21. Write a Pandas program to create a graphical analysis of UFO (unidentified flying object) sighted by month. Go to the editor
Click me to see the sample solution

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. Go to the editor
Click me to see the sample solution

23. Write a Pandas program to create a comparison of the top 10 years in which the UFO was sighted vs each Month. Go to the editor
Click me to see the sample solution

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. Go to the editor
Click me to see the sample solution

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. Go to the editor
Click me to see the sample solution

Python Code Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

[ Want to contribute to Python Pandas exercises? Send your code (attached with a .zip file) to us at w3resource[at]yahoo[dot]com. Please avoid copyrighted materials.]

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Understanding slice notation:

It's pretty simple really:

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

There is also the step value, which can be used with any of the above:

a[start:stop:step] # start through not past stop, by step

The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default).

The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning. So:

a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

Similarly, step may be a negative number:

a[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

Relation to slice() object

The slicing operator [] is actually being used in the above code with a slice() object using the : notation (which is only valid within []), i.e.:

a[start:stop:step]

is equivalent to:

a[slice(start, stop, step)]

Slice objects also behave slightly differently depending on the number of arguments, similarly to range(), i.e. both slice(stop) and slice(start, stop[, step]) are supported. To skip specifying a given argument, one might use None, so that e.g. a[start:] is equivalent to a[slice(start, None)] or a[::-1] is equivalent to a[slice(None, None, -1)].

While the : -based notation is very helpful for simple slicing, the explicit use of slice() objects simplifies the programmatic generation of slicing.

Ref: https://bit.ly/2MHaTp7