<jupyter_start><jupyter_text>![title](img/cover4.png) Copyright! This material is protected, please do not copy or distribute.    by:<NAME> ***Udemy course : Python Bootcamp for Data Science 2021 Numpy Pandas & Seaborn ***  14.3 Basics of Time Series First we import pandas and datetime libraries:<jupyter_code>import pandas as pd
from datetime import datetime<jupyter_output><empty_output><jupyter_text>Lets read this time series from a csv file using the function **pd.read_csv()**:<jupyter_code>df = pd.read_csv('data/ex15.csv')
df.head()<jupyter_output><empty_output><jupyter_text>To display the data type of all columns in this dataframe we use the attribute **dtypes**:<jupyter_code>df.dtypes<jupyter_output><empty_output><jupyter_text>We can convert the 'date' column into datetime format using the function **pd.to_datetime()**:<jupyter_code>df['date'] = pd.to_datetime(df['date'])<jupyter_output><empty_output><jupyter_text>We can check **again** the data types using the attribute **dtypes**:<jupyter_code>df.dtypes<jupyter_output><empty_output><jupyter_text>To display the head of this dataframe we use the function **head()**:<jupyter_code>df.head()<jupyter_output><empty_output><jupyter_text>For time series, we set the index to be the date column using the function **set_index()**:<jupyter_code>df = df.set_index('date')
df.head()<jupyter_output><empty_output><jupyter_text>We can combine the above steps in one code line, like this:<jupyter_code>df = pd.read_csv('data/ex15.csv', parse_dates = ['date'], index_col = 'date')
df.head()<jupyter_output><empty_output><jupyter_text>In time series, we can select data that belong to specific year using the function **loc [ ]**:<jupyter_code>df.loc['2021']<jupyter_output><empty_output><jupyter_text>We can also select data that belong to a specif month like this:<jupyter_code>df.loc['2021-4']<jupyter_output><empty_output><jupyter_text>We can also select data in a range of dates, like this:<jupyter_code>df.loc['2019':'2020']<jupyter_output><empty_output><jupyter_text>To select from the very begining to a specific date, we leave the start befor the colon empty:<jupyter_code>df.loc[:'2020']<jupyter_output><empty_output><jupyter_text>To check for duplicate index dates, we use the attribute **is_unique** for the index like this:<jupyter_code>df.index.is_unique<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11969439728353141, 'fraction_numerical': 0.01400679117147708, 'mean_word_length': 8.353174603174603, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '270655', 'license': 'MIT', 'n_tokens_mistral': 821, 'n_tokens_neox': 796, 'n_words': 211, 'path': '14.3 Basics of Time Series.ipynb', 'repo_name': 'SiamakMushakhian/Numpy-Pandas-Seaborn', 'size': '2361'}