Exercise : Pandas and Date time Basic

Pandas date & time Pandas

  1. Create a datetime out of your birthday. Use year, month, day , hour , minute and second.
  2. Using the above datetime of your birthday, print your birth year , birth month , birth day , hour , minute and second.
  3. How old you were as on 1st of Jan 2020, 13 PM 22 minutes and 34 seconds
  4. How old you are as on present date and time.
  5. Add 5 years , 2 months and 25 days to your birthday.
  6. What is the date and time now. Write in different formats ( 1st April 2019, 20 December 2010, 16/02/2020 12:32:45 )
  7. Create a date and time series with 10 elements, with a frequency of 3 days 6 hours starting from previous 10 days
  8. Write milliseconds and microseconds to above format.
  9. Prepare a list of name and date of birth of your friends. Find out the name of the weekday from their date of birth.
  10. Print the business days in last 10 days from today.

Create a datetime out of your birthday. Use year, month, day , hour , minute and second.

You can read on how to use to_datetime()
import pandas as pd 
dt_bd=pd.to_datetime('2001-12-02 16:20:05')
print(dt_bd)

Using the above datetime of your birthday, print your birth year , birth month , birth day , hour , minute and second.

import pandas as pd 
dt_bd=pd.to_datetime('2000-12-23 16:20:05')
print("year : ",dt_bd.year)
print("year : ",dt_bd.month)
print("year : ",dt_bd.day)
print("year : ",dt_bd.hour)

How old you were as on 1st of Jan 2020, 13 Hours, 22 minutes and 34 seconds

import pandas as pd 
dt_bd=pd.to_datetime('2000-12-23 16:20:05')
dt_target=pd.to_datetime('2020-01-01 13:22:34')
print(dt_target-dt_bd)
Output
6947 days 21:02:29
We can get the output in Week, Month, Year, hour etc by using timedelta64() Here are some examples.
Above code we will convert to weeks
import pandas as pd 
import numpy as np
from dateutil.relativedelta import relativedelta

dt_bd=pd.to_datetime('2001-11-22 16:20:05')
dt_target=pd.to_datetime('2020-01-01 13:22:34')
dt_diff=dt_target-dt_bd
print(dt_diff)
print(dt_diff/np.timedelta64(1,'W'))
Output , note : First line shows value in days and 2nd line shows value in weeks.
6613 days 21:02:29
944.8395320767196
Similarly we can get the age in Months as on 1st Jan 2020
import pandas as pd 
import numpy as np
from dateutil.relativedelta import relativedelta

dt_bd=pd.to_datetime('2001-11-22 16:20:05')
dt_target=pd.to_datetime('2020-01-01 13:22:34')
dt_diff=dt_target-dt_bd
print(dt_diff)
print(dt_diff/np.timedelta64(1,'M'))
6613 days 21:02:29
217.29815312961784

How old you are as on present date and time.

import pandas as pd 
dt_now=pd.to_datetime('now')
dt_bd=pd.to_datetime('2001-11-22 16:20:05')
dt_diff=dt_now-dt_bd
print(dt_diff)
Output
6721 days 10:37:40.983745
By using timedelta64() we can create output in different units of date and time(as explained above ).

Add 5 years , 2 months and 25 days to your birthday.

import pandas as pd 
import numpy as np
dt_bd=pd.to_datetime('2001-11-22 16:20:05')
dt_new=dt_bd + np.timedelta64(5,'Y') + np.timedelta64(2,'M') + np.timedelta64(25,'D')
print(dt_new)
Output
2007-02-16 18:24:17

What is the date and time now. Write in different formats ( 1st April 2019, 20 December 2010, 16/02/2020 12:32:45 )

import pandas as pd 
dt_now=pd.to_datetime('now')
print(dt_now)
Output in different fomats ( use strftime() )
import pandas as pd 
dt_now=pd.to_datetime('now')
print(dt_now)
print(dt_now.strftime('%d-%b-%Y')) #18-Apr-2020
print(dt_now.strftime('%d-%B-%Y')) #18-April-2020
print(dt_now.strftime('%D %X')) # 04/18/20 20:59:42
print(dt_now.strftime('%c')) # Sat Apr 18 20:05:03 2020

Create a date and time series with 10 elements, with a frequency of 3 days 6 hours starting from previous 10 days

import pandas as pd 
dt_now=pd.to_datetime('now')
dt_start=dt_now -  pd.to_timedelta(10,unit='days')
my_data=pd.date_range(start=dt_start, periods=10,freq='3D6H')
print(my_data)

Write milliseconds and microseconds to above format

print(my_data.strftime('%H:%M:%S %f'))

Prepare a list of name and date of birth of your friends. Find out the name of the weekday from their date of birth.

We created a date range by using 25 days frequency and one start date. The method day_name() returns the weekday name of the datetime object.
import pandas as pd 
dt_now=pd.to_datetime('now')
dt_start=pd.to_datetime('2001-01-22 16:20:05')
my_data=pd.date_range(start=dt_start, periods=10,freq='25D')
#print(my_data)
print(my_data.day_name())
Output
Index(['Monday', 'Friday', 'Tuesday', 'Saturday', 'Wednesday', 'Sunday',
       'Thursday', 'Monday', 'Friday', 'Tuesday'],
      dtype='object')

Print the business days in last 10 days from today.

To create business days we need to use period_range().
import pandas as pd 
dt_now=pd.to_datetime('now')
dt_start=dt_now -  pd.to_timedelta(10,unit='days')
my_data=pd.period_range(start=dt_start, periods=10,freq='B')
print(my_data.strftime('%x %A'))
Output ( uses strftime for formats and note that %x returns date and %A returns Name of the Weekday in full )
Index(['04/08/20 Wednesday', '04/09/20 Thursday', '04/10/20 Friday',
       '04/13/20 Monday', '04/14/20 Tuesday', '04/15/20 Wednesday',
       '04/16/20 Thursday', '04/17/20 Friday', '04/20/20 Monday',
       '04/21/20 Tuesday'],
      dtype='object')
Pandas date & time Basic Exercise using datetime 3-1
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer