w3resource

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 1910-06-01 15:00:00           wills point     ...      32.709167  -96.008056
1 1920-06-11 21:00:00                cicero     ...      40.123889  -86.013333
2 1929-07-05 14:00:00  buchanan  (or burns)     ...      43.642500 -118.627500
3 1931-06-01 13:00:00               abilene     ...      38.917222  -97.213611
4 1939-06-01 20:00:00              waterloo     ...      34.918056  -88.064167

[5 rows x 11 columns]

Unique reporting dates of UFO:
[datetime.date(1910, 6, 1) datetime.date(1920, 6, 11)
 datetime.date(1929, 7, 5) datetime.date(1931, 6, 1)
 datetime.date(1939, 6, 1) datetime.date(1939, 7, 7)
 datetime.date(1941, 6, 1) datetime.date(1942, 6, 1)
 datetime.date(1944, 1, 1) datetime.date(1944, 6, 1)
 datetime.date(1944, 4, 2) datetime.date(1945, 6, 1)
 datetime.date(1945, 6, 7) datetime.date(1945, 8, 8)
 datetime.date(1945, 7, 10) datetime.date(1946, 2, 1)
 datetime.date(1946, 7, 1) datetime.date(1946, 1, 8)
 datetime.date(1947, 6, 1) datetime.date(1947, 7, 1)
 datetime.date(1948, 8, 1) datetime.date(1948, 5, 10)
 datetime.date(1948, 12, 12) datetime.date(1949, 5, 1)
 datetime.date(1949, 7, 1) datetime.date(1949, 4, 10)
 datetime.date(1950, 6, 1) datetime.date(1950, 8, 1)
 datetime.date(1950, 10, 1) datetime.date(1951, 6, 1)
 datetime.date(1951, 7, 1) datetime.date(1951, 2, 3)
 datetime.date(1951, 6, 3) datetime.date(1952, 7, 1)
 datetime.date(1952, 8, 1) datetime.date(1952, 10, 1)
 datetime.date(1953, 4, 1) datetime.date(1953, 7, 1)
 datetime.date(1953, 8, 1) datetime.date(1954, 2, 1)
 datetime.date(1954, 6, 1) datetime.date(1955, 5, 1)
 datetime.date(1955, 6, 1) datetime.date(1956, 1, 1)
 datetime.date(1956, 3, 1) datetime.date(1956, 5, 1)
 datetime.date(1956, 6, 1) datetime.date(1957, 1, 1)
 datetime.date(1957, 5, 1) datetime.date(1957, 6, 1)
 datetime.date(1958, 1, 1) datetime.date(1958, 6, 1)
 datetime.date(1959, 4, 1) datetime.date(1959, 5, 1)
 datetime.date(1959, 6, 1) datetime.date(1960, 2, 1)
 datetime.date(1960, 4, 1) datetime.date(1960, 5, 1)
 datetime.date(1961, 1, 1) datetime.date(1961, 4, 1)
 datetime.date(1961, 5, 1) datetime.date(1962, 1, 1)
 datetime.date(1962, 3, 1) datetime.date(1962, 6, 1)
 datetime.date(1963, 1, 1) datetime.date(1963, 4, 1)
 datetime.date(1963, 6, 1) datetime.date(1964, 5, 1)
 datetime.date(1964, 6, 1) datetime.date(1964, 7, 1)
 datetime.date(1965, 1, 1) datetime.date(1965, 4, 1)
 datetime.date(1966, 1, 1) datetime.date(1966, 2, 1)
 datetime.date(1966, 4, 1) datetime.date(1967, 3, 1)
 datetime.date(1967, 4, 1) datetime.date(1968, 1, 1)
 datetime.date(1969, 1, 1) datetime.date(1969, 3, 1)
 datetime.date(1970, 1, 1) datetime.date(1970, 5, 1)
 datetime.date(1971, 1, 1) datetime.date(1971, 2, 1)
 datetime.date(1971, 4, 1) datetime.date(1972, 1, 1)
 datetime.date(1972, 3, 1) datetime.date(1972, 4, 1)
 datetime.date(1973, 1, 1) datetime.date(1973, 2, 1)
 datetime.date(1973, 3, 1) datetime.date(1974, 1, 1)
 datetime.date(1974, 4, 1) datetime.date(1974, 5, 1)
 datetime.date(1974, 6, 1) datetime.date(1975, 1, 1)
 datetime.date(1976, 5, 1) datetime.date(1977, 1, 1)
 datetime.date(1978, 1, 1) datetime.date(1978, 2, 1)
 datetime.date(1978, 3, 1) datetime.date(1979, 1, 1)
 datetime.date(1979, 2, 1) datetime.date(1979, 3, 1)
 datetime.date(1980, 1, 1) datetime.date(1980, 3, 1)
 datetime.date(1981, 1, 1) datetime.date(1981, 3, 1)
 datetime.date(1982, 1, 1) datetime.date(1982, 5, 1)
 datetime.date(1982, 6, 1) datetime.date(1983, 3, 1)
 datetime.date(1983, 5, 1) datetime.date(1984, 3, 1)
 datetime.date(1984, 5, 1) datetime.date(1985, 1, 1)
 datetime.date(1985, 2, 1) datetime.date(1985, 3, 1)
 datetime.date(1986, 1, 1) datetime.date(1986, 4, 1)
 datetime.date(1987, 1, 1) datetime.date(1987, 2, 1)
 datetime.date(1987, 3, 1) datetime.date(1988, 1, 1)
 datetime.date(1988, 8, 2) datetime.date(1988, 9, 3)
 datetime.date(1988, 7, 4) datetime.date(1989, 1, 1)
 datetime.date(1989, 9, 2) datetime.date(1989, 7, 3)
 datetime.date(1989, 1, 5) datetime.date(1990, 5, 1)
 datetime.date(1990, 9, 2) datetime.date(1990, 7, 3)
 datetime.date(1990, 10, 5) datetime.date(1991, 3, 1)
 datetime.date(1991, 9, 2) datetime.date(1991, 6, 3)
 datetime.date(1991, 7, 4) datetime.date(1992, 2, 1)
 datetime.date(1992, 4, 2) datetime.date(1992, 9, 3)
 datetime.date(1992, 7, 4) datetime.date(1992, 8, 5)
 datetime.date(1992, 4, 8) datetime.date(1992, 10, 9)
 datetime.date(1992, 10, 10) datetime.date(1993, 3, 1)
 datetime.date(1993, 11, 2) datetime.date(1993, 9, 3)
 datetime.date(1993, 8, 4) datetime.date(1993, 7, 5)
 datetime.date(1993, 9, 6) datetime.date(1993, 7, 7)
 datetime.date(1993, 9, 8) datetime.date(1993, 8, 9)
 datetime.date(1993, 4, 10) datetime.date(1993, 6, 11)
 datetime.date(1993, 7, 12) datetime.date(1994, 1, 1)
 datetime.date(1994, 5, 2) datetime.date(1994, 3, 3)
 datetime.date(1994, 4, 4) datetime.date(1994, 5, 5)
 datetime.date(1994, 12, 6) datetime.date(1994, 8, 7)
 datetime.date(1994, 3, 9) datetime.date(1994, 3, 10)
 datetime.date(1994, 4, 11) datetime.date(1994, 1, 12)
 datetime.date(1995, 1, 1) datetime.date(1995, 7, 2)
 datetime.date(1995, 7, 3) datetime.date(1995, 4, 4)
 datetime.date(1995, 1, 5) datetime.date(1995, 5, 6)
 datetime.date(1995, 2, 7) datetime.date(1995, 7, 8)
 datetime.date(1995, 11, 9) datetime.date(1995, 6, 10)
 datetime.date(1995, 9, 11) datetime.date(1995, 11, 12)
 datetime.date(1996, 2, 1) datetime.date(1996, 8, 2)
 datetime.date(1996, 7, 3) datetime.date(1996, 7, 4)
 datetime.date(1996, 4, 5) datetime.date(1996, 7, 5)
 datetime.date(1996, 9, 5) datetime.date(1996, 11, 6)
 datetime.date(1996, 8, 8) datetime.date(1996, 6, 10)
 datetime.date(1996, 5, 11) datetime.date(1996, 8, 12)
 datetime.date(1997, 2, 1) datetime.date(1997, 3, 1)
 datetime.date(1997, 6, 1) datetime.date(1997, 5, 2)
 datetime.date(1997, 5, 3) datetime.date(1997, 7, 4)
 datetime.date(1997, 10, 5) datetime.date(1997, 9, 6)
 datetime.date(1997, 7, 7) datetime.date(1997, 8, 8)
 datetime.date(1997, 12, 9) datetime.date(1997, 2, 10)
 datetime.date(1997, 11, 11) datetime.date(1997, 7, 12)
 datetime.date(1998, 4, 1) datetime.date(1998, 10, 2)
 datetime.date(1998, 3, 3) datetime.date(1998, 6, 4)
 datetime.date(1998, 2, 5) datetime.date(1998, 2, 6)
 datetime.date(1998, 6, 7) datetime.date(1998, 1, 8)
 datetime.date(1998, 12, 8) datetime.date(1998, 4, 9)
 datetime.date(1998, 12, 9) datetime.date(1998, 4, 10)
 datetime.date(1998, 7, 11) datetime.date(1999, 6, 1)
 datetime.date(1999, 7, 2) datetime.date(1999, 12, 5)
 datetime.date(1999, 7, 6) datetime.date(1999, 7, 7)
 datetime.date(1999, 10, 7) datetime.date(1999, 7, 8)
 datetime.date(1999, 6, 9) datetime.date(1999, 3, 10)
 datetime.date(1999, 3, 11) datetime.date(1999, 5, 12)
 datetime.date(2000, 2, 1) datetime.date(2000, 3, 2)
 datetime.date(2000, 5, 3) datetime.date(2000, 2, 4)
 datetime.date(2000, 5, 5) datetime.date(2000, 3, 6)
 datetime.date(2000, 12, 7) datetime.date(2000, 11, 8)
 datetime.date(2000, 12, 9) datetime.date(2000, 6, 10)
 datetime.date(2000, 2, 11) datetime.date(2000, 7, 12)
 datetime.date(2001, 1, 1) datetime.date(2001, 5, 2)
 datetime.date(2001, 3, 3) datetime.date(2001, 8, 4)
 datetime.date(2001, 6, 5) datetime.date(2001, 11, 5)
 datetime.date(2001, 4, 6) datetime.date(2001, 2, 7)
 datetime.date(2001, 4, 8) datetime.date(2001, 9, 9)
 datetime.date(2001, 6, 10) datetime.date(2001, 1, 11)
 datetime.date(2001, 12, 11) datetime.date(2001, 3, 12)
 datetime.date(2001, 9, 12) datetime.date(2002, 3, 1)
 datetime.date(2002, 8, 1) datetime.date(2002, 1, 2)
 datetime.date(2002, 7, 3) datetime.date(2002, 7, 4)
 datetime.date(2002, 9, 5) datetime.date(2002, 10, 5)
 datetime.date(2002, 5, 6) datetime.date(2002, 1, 7)
 datetime.date(2002, 9, 8) datetime.date(2002, 5, 9)
 datetime.date(2002, 5, 10) datetime.date(2002, 1, 11)
 datetime.date(2002, 2, 12) datetime.date(2003, 4, 1)
 datetime.date(2003, 10, 2) datetime.date(2003, 11, 4)
 datetime.date(2003, 1, 6) datetime.date(2003, 5, 7)
 datetime.date(2003, 7, 8) datetime.date(2003, 4, 9)
 datetime.date(2003, 3, 10) datetime.date(2003, 7, 11)
 datetime.date(2004, 2, 1) datetime.date(2004, 10, 2)
 datetime.date(2004, 4, 5) datetime.date(2004, 10, 6)
 datetime.date(2004, 11, 7) datetime.date(2004, 12, 8)
 datetime.date(2004, 2, 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?



Python: Tips of the Day

Python: Dictionary As Arguments Using **arguments

It allows you to pass varying number of keyword arguments to a function.
You can also pass in dictionary values as keyword arguments:

def myfunc(arguments):
  return arguments['key']