<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Visualising Time Series Data
In the section, we will explore ways to visualise data gathered over time. We will
Step1:  Visualising Simple Time Series Data
Let's say you want to visualise numeric variables such as Sales, Profit, Shipping_Cost etc. over time.
Step2:  Since the Order_Date variable is in the orders dataframe, let's merge it.
Step3:  Since Order_Date is a string, we need to convert it into a datetime object. You can do that using pd.to_datetime().
Step4:  Now, since on each day, multiple orders were placed, we need to aggregate Sales using a metric such as mean, median etc., and then create a time series plot.
We will group by Order_Date and compute the sum of Sales on each day.
Step5:  We can now create a time-series plot using sns.tsplot().
Step6:  Using Derived Date Metrics for Visualisation
It is often helpful to use derived variables from date such as month and year and using them to identify hidden patterns.
Step7:  Now you can plot the average sales across years and months.
Step8:  There is another way to visualise numeric variables, such as Sales, across the year and month. We can pivot the month column to create a wide-format dataframe, and then plot a heatmap.
Step9:  You can now create a heatmap using sns.heatmap().
<ASSISTANT_TASK:>
Python Code:
# loading libraries and reading the data
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# set seaborn theme if you prefer
sns.set(style="white")
# read data
market_df = pd.read_csv("./global_sales_data/market_fact.csv")
customer_df = pd.read_csv("./global_sales_data/cust_dimen.csv")
product_df = pd.read_csv("./global_sales_data/prod_dimen.csv")
shipping_df = pd.read_csv("./global_sales_data/shipping_dimen.csv")
orders_df = pd.read_csv("./global_sales_data/orders_dimen.csv")
Explanation: Visualising Time Series Data
In the section, we will explore ways to visualise data gathered over time. We will:
- Plot simple time series plots
- Derive variables such as month and year and use them for richer visualisations
End of explanation
market_df.head()
Explanation: Visualising Simple Time Series Data
Let's say you want to visualise numeric variables such as Sales, Profit, Shipping_Cost etc. over time.
End of explanation
# merging with the Orders data to get the Date column
df = pd.merge(market_df, orders_df, how='inner', on='Ord_id')
df.head()
# Now we have the Order_Date in the df
# It is stored as a string (object) currently
df.info()
Explanation: Since the Order_Date variable is in the orders dataframe, let's merge it.
End of explanation
# Convert Order_Date to datetime type
df['Order_Date'] = pd.to_datetime(df['Order_Date'])
# Order_Date is now datetime type
df.info()
Explanation: Since Order_Date is a string, we need to convert it into a datetime object. You can do that using pd.to_datetime().
End of explanation
# aggregating total sales on each day
time_df = df.groupby('Order_Date')['Sales'].sum()
print(time_df.head())
print(type(time_df))
Explanation: Now, since on each day, multiple orders were placed, we need to aggregate Sales using a metric such as mean, median etc., and then create a time series plot.
We will group by Order_Date and compute the sum of Sales on each day.
End of explanation
# time series plot
# figure size
plt.figure(figsize=(16, 8))
# tsplot
sns.tsplot(data=time_df)
plt.show()
Explanation: We can now create a time-series plot using sns.tsplot().
End of explanation
# extracting month and year from date
# extract month
df['month'] = df['Order_Date'].dt.month
# extract year
df['year'] = df['Order_Date'].dt.year
df.head()
Explanation: Using Derived Date Metrics for Visualisation
It is often helpful to use derived variables from date such as month and year and using them to identify hidden patterns.
End of explanation
# grouping by year and month
df_time = df.groupby(["year", "month"]).Sales.mean()
df_time.head()
plt.figure(figsize=(8, 6))
# time series plot
sns.tsplot(df_time)
plt.xlabel("Time")
plt.ylabel("Sales")
plt.show()
Explanation: Now you can plot the average sales across years and months.
End of explanation
# Pivoting the data using 'month' 
year_month = pd.pivot_table(df, values='Sales', index='year', columns='month', aggfunc='mean')
year_month.head()
Explanation: There is another way to visualise numeric variables, such as Sales, across the year and month. We can pivot the month column to create a wide-format dataframe, and then plot a heatmap.
End of explanation
# figure size
plt.figure(figsize=(12, 8))
# heatmap with a color map of choice
sns.heatmap(year_month, cmap="YlGnBu")
plt.show()
Explanation: You can now create a heatmap using sns.heatmap().
End of explanation
<END_TASK>