<jupyter_start><jupyter_text>Objective of the session*  datetime objects<jupyter_code># Use pythons datetime:
from datetime import datetime
now = datetime.now()                 # checking for the current datetime
print(now)                 
# now I am trying to put my own datetime
t1 = datetime.now()
t2 = datetime(1970, 1, 1)      #a random date I am giving
diff = t1 - t2
print(diff)
# okey now let us check for the above datatypes.
print(type(diff))
#Convert to datetime : for this we will load our dataset, another way of loading the dataset is given below.
import pandas as pd
data = ('country_timeseries.csv')
ebola = pd.read_csv(data)
ebola.head(3)  #here the first column is the dates. 
#but there is a question. Is this the really the date??
ebola['Day'].dtypes  #we can notice that the date type is just in the form of integers.
# now we are going to take this old column and create a new column that will have actual daytime object.
ebola['date_dt'] = pd.to_datetime(ebola['Date'])
ebola.loc[0:3, ['Date','date_dt']]
# Load data with dates
# If our dataset is having the dates column then we must use the 'parse_dates' parameter at the time of importing

ebola = pd.read_csv(data, parse_dates = [0])

# above I am parsing the dates. 0 is the column number as we know that Date is in first position.
# checking wheter the above thing happend or not.
ebola.dtypes
#yes we can notice that for Date this time it is coming as datetime64.
# Extracting various elements out of the date.
#What do I mean by that. e.g.
d = pd.to_datetime('2016-02-29')
print(d)                            #here we have created the date for the leap year.
print(type(d))          #by timestamp we mean 
#now out of this I can fetch the attributes:In first case the year attribute
d.year
# for getting the month
d.month
# any guess for the day?
d.day
# Now let us do this on the dataframe
ebola.dtypes  #basically we want to get those(day,month and year) attributes in our datasets

ebola['date_dt'] = pd.to_datetime(ebola['Date'])

ebola['year'] = ebola['date_dt'].dt.year  #dt.year is letting us fetch us the date out of our date_dt.
ebola[['Date','date_dt','year']].head(3)
# Now in the same fashion I will access the month and day, let us ttry it together.
# Please tell me what I have used as the container now on the right hand side.
ebola['month'], ebola['day'] = (ebola['date_dt'].dt.month, ebola['date_dt'].dt.day)
ebola[['Date', 'date_dt', 'year', 'month', 'day']].head(2)
# Now please tell in what you are expecting year, month and date to be?
ebola.dtypes   
# Implement date calculations and timedeltas
ebola.iloc[-5: , 0:5]
# If I will apply minimum function on my timestamp then lets see:
ebola['date_dt'].min()   #this is oldest date in our dataset. How this knowledge can be handy?
# let's use this knowledge in analysing the dataset.
ebola['outbreak_d'] = ebola['date_dt'] - ebola['date_dt'].min() #this will tell us how many days are over since 
                                                                # the brekdown of ebola.

ebola[['Date', 'Day', 'outbreak_d']].tail(2)

#Question - Now please check the object for outbreak_d? First guess and then have the look.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09731752963194011, 'fraction_numerical': 0.009669369931378666, 'mean_word_length': 3.621037463976945, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '12040', 'license': 'MIT', 'n_tokens_mistral': 992, 'n_tokens_neox': 964, 'n_words': 441, 'path': '08-Sep-2018/three/introduction to datetime.ipynb', 'repo_name': 'ankushdata/New_Batch', 'size': '3206'}