DatetimeIndex in Python Pandas – Time Series Data

We can parse dates at the time of creating DataFrames when Datasets have columns with Datetime data.

We can also set the column having date value as the index

Here is the Sample Code


# coding: utf-8

# # DatetimeIndex

# In[1]:

import pandas as pd

# In[9]:

df = pd.read_csv(‘sales.csv’, parse_dates=[‘date’], index_col=’date’)
# parsing dates and setting the column date as index of dataframe

# In[8]:

#df.set_index(‘date’)

# In[15]:

df.head(3)

# In[11]:

df.info()

# In[12]:

df.index

# In[13]:

# Why do we use DatetimeIndex ?
# Using DatetimeIndex we can find Data for any particular date as follows

# In[16]:

# Getting Properties Sales Data for this date
df[‘2019-07-25’]

# In[20]:

# Getting Series of all prices
df[‘2019-07-25’][‘price’]

# In[24]:

date = input(“Enter Date”)
print(“Total Amount of Properties Sold “, df[date][‘price’].sum())

# In[26]:

# July 2019
df[‘2019-07’]

# In[28]:

# Year 2019
df[‘2019’]

# In[39]:

df[‘July-2019’]

# In[40]:
# Between a Range
df[‘2019-03-25′:’2019-07-25’]

# In[45]:

# Plot Graphs

# In[51]:

# kind = ‘line’, ‘bar’, ‘barh’, ‘area’, ‘pie’
df[‘price’].head(1000).plot(kind=’area’)

Leave a Reply

Your email address will not be published. Required fields are marked *