Pandas Datetime: Add 100 days with reporting date of unidentified flying object (UFO)
Pandas Datetime: Exercise-13 with Solution
Write a Pandas program to add 100 days with reporting date of unidentified flying object (UFO).
Sample Solution :
Python Code :
import pandas as pd
from datetime import timedelta
df = pd.read_csv(r'ufo.csv')
df['Date_time'] = df['Date_time'].astype('datetime64[ns]')
print("Original Dataframe:")
print(df.head())
print("\nAdd 100 days with reporting date:")
df['New_doc_dt'] = df['Date_time'] + timedelta(days=180)
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] Add 100 days with reporting date: Date_time ... New_doc_dt 0 1910-06-01 15:00:00 ... 1910-11-28 15:00:00 1 1920-06-11 21:00:00 ... 1920-12-08 21:00:00 2 1929-07-05 14:00:00 ... 1930-01-01 14:00:00 3 1931-06-01 13:00:00 ... 1931-11-28 13:00:00 4 1939-06-01 20:00:00 ... 1939-11-28 20:00:00 5 1939-07-07 02:00:00 ... 1940-01-03 02:00:00 6 1941-06-01 13:00:00 ... 1941-11-28 13:00:00 7 1942-06-01 22:30:00 ... 1942-11-28 22:30:00 8 1944-01-01 12:00:00 ... 1944-06-29 12:00:00 9 1944-06-01 12:00:00 ... 1944-11-28 12:00:00 10 1944-04-02 11:00:00 ... 1944-09-29 11:00:00 11 1945-06-01 13:30:00 ... 1945-11-28 13:30:00 12 1945-06-07 07:00:00 ... 1945-12-04 07:00:00 13 1945-08-08 12:00:00 ... 1946-02-04 12:00:00 14 1945-07-10 01:30:00 ... 1946-01-06 01:30:00 15 1946-02-01 17:00:00 ... 1946-07-31 17:00:00 16 1946-07-01 13:30:00 ... 1946-12-28 13:30:00 17 1946-01-08 02:00:00 ... 1946-07-07 02:00:00 18 1947-06-01 02:30:00 ... 1947-11-28 02:30:00 19 1947-06-01 17:00:00 ... 1947-11-28 17:00:00 20 1947-07-01 20:00:00 ... 1947-12-28 20:00:00 21 1947-07-01 20:00:00 ... 1947-12-28 20:00:00 22 1948-08-01 02:00:00 ... 1949-01-28 02:00:00 23 1948-05-10 19:00:00 ... 1948-11-06 19:00:00 24 1948-12-12 23:30:00 ... 1949-06-10 23:30:00 25 1949-05-01 14:00:00 ... 1949-10-28 14:00:00 26 1949-07-01 11:00:00 ... 1949-12-28 11:00:00 27 1949-07-01 16:00:00 ... 1949-12-28 16:00:00 28 1949-04-10 15:00:00 ... 1949-10-07 15:00:00 29 1950-06-01 16:00:00 ... 1950-11-28 16:00:00 .. ... ... ... 317 2002-03-01 06:15:00 ... 2002-08-28 06:15:00 318 2002-08-01 15:25:00 ... 2003-01-28 15:25:00 319 2002-01-02 17:30:00 ... 2002-07-01 17:30:00 320 2002-07-03 01:00:00 ... 2002-12-30 01:00:00 321 2002-07-04 20:23:00 ... 2002-12-31 20:23:00 322 2002-09-05 23:00:00 ... 2003-03-04 23:00:00 323 2002-10-05 23:00:00 ... 2003-04-03 23:00:00 324 2002-05-06 15:50:00 ... 2002-11-02 15:50:00 325 2002-01-07 18:00:00 ... 2002-07-06 18:00:00 326 2002-09-08 16:00:00 ... 2003-03-07 16:00:00 327 2002-05-09 18:00:00 ... 2002-11-05 18:00:00 328 2002-05-10 23:30:00 ... 2002-11-06 23:30:00 329 2002-01-11 18:45:00 ... 2002-07-10 18:45:00 330 2002-02-12 20:00:00 ... 2002-08-11 20:00:00 331 2003-04-01 01:00:00 ... 2003-09-28 01:00:00 332 2003-10-02 02:45:00 ... 2004-03-30 02:45:00 333 2003-11-04 20:00:00 ... 2004-05-02 20:00:00 334 2003-01-06 10:10:00 ... 2003-07-05 10:10:00 335 2003-05-07 02:00:00 ... 2003-11-03 02:00:00 336 2003-07-08 00:30:00 ... 2004-01-04 00:30:00 337 2003-04-09 21:00:00 ... 2003-10-06 21:00:00 338 2003-03-10 20:52:00 ... 2003-09-06 20:52:00 339 2003-07-11 20:50:00 ... 2004-01-07 20:50:00 340 2004-02-01 01:00:00 ... 2004-07-30 01:00:00 341 2004-10-02 18:20:00 ... 2005-03-31 18:20:00 342 2004-04-05 20:35:00 ... 2004-10-02 20:35:00 343 2004-10-06 23:00:00 ... 2005-04-04 23:00:00 344 2004-11-07 20:30:00 ... 2005-05-06 20:30:00 345 2004-12-08 05:30:00 ... 2005-06-06 05:30:00 346 2004-02-10 05:15:00 ... 2004-08-08 05:15:00 [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 get the difference (in days) between documented date and reporting date of unidentified flying object (UFO).
Next: Write a Pandas program to generate sequences of fixed-frequency dates and time spans.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/pandas/datetime/pandas-datetime-exercise-13.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics