w3resource
Python Pandas Exercises

Pandas Datetime: Extract unique reporting dates of unidentified flying object (UFO)

Pandas Datetime: Exercise-11 with Solution

Write a Pandas program to extract unique reporting dates of unidentified flying object (UFO).

Sample Solution :

Python Code :

import pandas as pd
df = pd.read_csv(r'ufo.csv')
df['Date_time'] = df['Date_time'].astype('datetime64[ns]')
print("Original Dataframe:")
print(df.head())
print("\nUnique reporting dates of UFO:")
print(df["Date_time"].map(lambda t: t.date()).unique())

Sample Output:

Original Dataframe:
            Date_time                  city     ...       latitude   longitude
0 1949-10-10 20:30:00            san marcos     ...      29.883056  -97.941111
1 1949-10-10 21:00:00          lackland afb     ...      29.384210  -98.581082
2 1955-10-10 17:00:00  chester (uk/england)     ...      53.200000   -2.916667
3 1956-10-10 21:00:00                  edna     ...      28.978333  -96.645833
4 1960-10-10 20:00:00               kaneohe     ...      21.418056 -157.803611

[5 rows x 11 columns]

Unique reporting dates of UFO:
[datetime.date(1949, 10, 10) datetime.date(1955, 10, 10)
 datetime.date(1956, 10, 10) datetime.date(1960, 10, 10)
 datetime.date(1961, 10, 10) datetime.date(1965, 10, 10)
 datetime.date(1966, 10, 10) datetime.date(1968, 10, 10)
 datetime.date(1970, 10, 10) datetime.date(1971, 10, 10)
 datetime.date(1972, 10, 10) datetime.date(1973, 10, 10)
 datetime.date(1974, 10, 10) datetime.date(1975, 10, 10)
 datetime.date(1976, 10, 10) datetime.date(1977, 10, 10)
 datetime.date(1978, 10, 10) datetime.date(1979, 10, 10)
 datetime.date(1980, 10, 10) datetime.date(1982, 10, 10)
 datetime.date(1984, 10, 10) datetime.date(1985, 10, 10)
 datetime.date(1986, 10, 10) datetime.date(1988, 10, 10)
 datetime.date(1989, 10, 10) datetime.date(1990, 10, 10)
 datetime.date(1991, 10, 10) datetime.date(1992, 10, 10)
 datetime.date(1993, 10, 10) datetime.date(1994, 10, 10)
 datetime.date(1995, 10, 10) datetime.date(1996, 10, 10)
 datetime.date(1997, 10, 10) datetime.date(1998, 10, 10)
 datetime.date(1999, 10, 10) datetime.date(2000, 10, 10)
 datetime.date(2001, 10, 10) datetime.date(2002, 10, 10)
 datetime.date(2003, 10, 10) datetime.date(2004, 10, 10)]

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to count year-country wise frequency of reporting dates of unidentified flying object (UFO).
Next: Write a Pandas program to get the difference (in days) between documented date and reporting date of unidentified flying object (UFO).

What is the difficulty level of this exercise?