w3resource

Pandas Datetime: Get the difference between documented date and reporting date of unidentified flying object (UFO)

Pandas Datetime: Exercise-12 with Solution

Write a Pandas program to get the difference (in days) between documented date and reporting date 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]')
df['date_documented'] = df['date_documented'].astype('datetime64[ns]')
print("Original Dataframe:")
print(df.head())
print("\nDifference (in days) between documented date and reporting date of UFO:")
df['Difference'] = (df['date_documented'] - df['Date_time']).dt.days
print(df)

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]

Difference (in days) between documented date and reporting date of UFO:
              Date_time    ...     Difference
0   1910-06-01 15:00:00    ...          34652
1   1920-06-11 21:00:00    ...          32476
2   1929-07-05 14:00:00    ...          26704
3   1931-06-01 13:00:00    ...          27286
4   1939-06-01 20:00:00    ...          27293
5   1939-07-07 02:00:00    ...          24163
6   1941-06-01 13:00:00    ...          22772
7   1942-06-01 22:30:00    ...          23807
8   1944-01-01 12:00:00    ...          22120
9   1944-06-01 12:00:00    ...          23720
10  1944-04-02 11:00:00    ...          22293
11  1945-06-01 13:30:00    ...          23913
12  1945-06-07 07:00:00    ...          22001
13  1945-08-08 12:00:00    ...          21345
14  1945-07-10 01:30:00    ...          21322
15  1946-02-01 17:00:00    ...          21801
16  1946-07-01 13:30:00    ...          22626
17  1946-01-08 02:00:00    ...          22700
18  1947-06-01 02:30:00    ...          19689
19  1947-06-01 17:00:00    ...          24196
20  1947-07-01 20:00:00    ...          21108
21  1947-07-01 20:00:00    ...          21108
22  1948-08-01 02:00:00    ...          20863
23  1948-05-10 19:00:00    ...          20794
24  1948-12-12 23:30:00    ...          22011
25  1949-05-01 14:00:00    ...          19314
26  1949-07-01 11:00:00    ...          23550
27  1949-07-01 16:00:00    ...          20142
28  1949-04-10 15:00:00    ...          20105
29  1950-06-01 16:00:00    ...          21103
..                  ...    ...            ...
317 2002-03-01 06:15:00    ...             17
318 2002-08-01 15:25:00    ...             14
319 2002-01-02 17:30:00    ...              8
320 2002-07-03 01:00:00    ...             43
321 2002-07-04 20:23:00    ...             21
322 2002-09-05 23:00:00    ...           1694
323 2002-10-05 23:00:00    ...              9
324 2002-05-06 15:50:00    ...              7
325 2002-01-07 18:00:00    ...              3
326 2002-09-08 16:00:00    ...              4
327 2002-05-09 18:00:00    ...              4
328 2002-05-10 23:30:00    ...            226
329 2002-01-11 18:45:00    ...             17
330 2002-02-12 20:00:00    ...              9
331 2003-04-01 01:00:00    ...             62
332 2003-10-02 02:45:00    ...              4
333 2003-11-04 20:00:00    ...              3
334 2003-01-06 10:10:00    ...             63
335 2003-05-07 02:00:00    ...              1
336 2003-07-08 00:30:00    ...              7
337 2003-04-09 21:00:00    ...             12
338 2003-03-10 20:52:00    ...             10
339 2003-07-11 20:50:00    ...             74
340 2004-02-01 01:00:00    ...             10
341 2004-10-02 18:20:00    ...             24
342 2004-04-05 20:35:00    ...              3
343 2004-10-06 23:00:00    ...             20
344 2004-11-07 20:30:00    ...              1
345 2004-12-08 05:30:00    ...            971
346 2004-02-10 05:15:00    ...              1

[347 rows x 12 columns]

Python Code Editor:


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

Previous: Write a Pandas program to extract unique reporting dates of unidentified flying object (UFO).
Next: Write a Pandas program to add 100 days with reporting date of unidentified flying object (UFO).

What is the difficulty level of this exercise?



Python: Tips of the Day

Python: Cache results with decorators

There is a great way to cache functions with decorators in Python. Caching will help save time and precious resources when there is an expensive function at hand.

Implementation is easy, just import lru_cache from functools library and decorate your function using @lru_cache.

from functools import lru_cache

@lru_cache(maxsize=None)
def fibo(a):
    if a <= 1:
        return a
    else:
        return fibo(a-1) + fibo(a-2)

for i in range(20):
    print(fibo(i), end="|")

print("\n\n", fibo.cache_info())

Output:

0|1|1|2|3|5|8|13|21|34|55|89|144|233|377|610|987|1597|2584|4181|

 CacheInfo(hits=36, misses=20, maxsize=None, currsize=20)