import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import datetime
= pd.read_csv('https://raw.githubusercontent.com/farhanage/dataset-for-study/main/Electric_Production.csv', names=['date', 'electric_production'], header=0)
df
15) df.head(
Pertemuan 5 : Time Series Visualization
Kembali ke EDA
datetime
object
# Mengecek tipe data tiap kolom
df.info()
Perhatikan bahwa kolom date memiliki tipe data object
(string). Pada pertemuan ini, kita akan membahas suatu tipe data baru yang bernama datetime
yang digunakan untuk memanipulasi data runtun waktu (time series).
Converting object
to datetime
Perhatikan code cell berikut untuk mengubah data bertipe object
menjadi datetime
# Mengubah data `object` -> `datetime`
'date'] = pd.to_datetime(df['date'], format='%m/%d/%Y') df[
Argumen format='%m/%d/%Y'
digunakan untuk membaca format penanggalan yang tertulis pada kolom yang ingin kita ubah. %d
menandakan hari, %m
untuk bulan, %y
untuk tahun 2 digit dan %Y
untuk tahun 4 digit.
e.g. : '2024-05-26' -> '%Y-%m-%d'
df.info()
Terlihat bahwa kolom date
kini memiliki tipe data datetime64[ns]
.
df.head()
Terlihat setelah dilakukan perubahan tipe data, format penanggalan pada kolom date
juga berubah.
Apa perbedaan string
dan datetime
?
Object datetime
memiliki attribut tanggal, bulan, waktu, hari, jam, menit, bahkan detik suatu observasi yang dapat kita akses melalui library datetime.
# Ambil tahun dari setiap observasi
'date'].dt.year df[
# Ambil Bulan dari setiap observasi
'date'].dt.month df[
# Ambil Tanggal dari setiap observasi
'date'].dt.day df[
object datetime
dapat digunakan sebagai nilai numerik dalam visualisasi data
='date', y='electric_production', data=df)
sns.lineplot(x
'Jumlah produksi listrik per tahun (1985-2018)')
plt.title(
plt.show()
Grouping
Bagaimana cara kita memanfaatkan attribut-attribut datetime? Gunakan grouping method (groupby
) untuk mendapatkan insight baru dari data per satuan waktu tertentu.
Informasi jumlah total produksi energi per tahunnya
'electric_production'].groupby(df['date'].dt.year).sum() # sum of electric_production, grouped by year df[
Informasi rata-rata produksi energi setiap bulan selama 33 tahun terakhir
'electric_production'].groupby(df['date'].dt.month).mean() # mean of electric_production, grouped by month df[
Plots
= df['electric_production'].groupby(df['date'].dt.year).sum() # sum of electric_production, grouped by year
sum_per_year
sns.lineplot(sum_per_year)
'Jumlah produksi listrik per tahun (1985-2018)')
plt.title(
plt.show()
kenapa tahun 2018 turun drastis? cek semua observasi pada tahun 2018
# Ambil data dengan tahun == 2018
'date'].dt.year == 2018] df[df[
Ternyata tahun 2018 hanya memiliki 1 observasi, sehingga tidak dapat dibandingkan dengan jumlah produksi tahun-tahun sebelumnya.
# buang data tahun 2018
'date'].dt.year == 2018].index, axis=0, inplace=True) df.drop(df[df[
'date'].dt.year == 2018] df[df[
Data sudah berhasil dibuang, sehingga dapat dilakukan visualisasi yang lebih akurat
= df['electric_production'].groupby(df['date'].dt.year).sum() # sum of electric_production, grouped by year
sum_per_year
sns.lineplot(sum_per_year)
'year')
plt.xlabel(
'electric production')
plt.ylabel(
'Jumlah produksi listrik per tahun (1985-2017)')
plt.title(
plt.show()
Interpretasinya?
= df['electric_production'].groupby(df['date'].dt.month).mean() # mean of electric_production, grouped by month
avg_per_month
sns.barplot(avg_per_month)
'month')
plt.xlabel(
'average electric production')
plt.ylabel(
'Rata-rata produksi listrik per bulan (1985-2017)')
plt.title(
plt.show()
Interpretasi? Kaitkan dengan musim?
Case Study
Download dataset di sini : Energy Time Series
Metadata :
- energy_reading.csv
building_id
- Foreign key for the building metadata.meter
- The meter id code. Read as {0: electricity, 1: chilledwater, 2: steam, 3: hotwater}. Not every building has all meter types.timestamp
- When the measurement was takenmeter_reading
- The target variable. Energy consumption in kWh (or equivalent). Note that this is real data with measurement error, which we expect will impose a baseline level of modeling error. UPDATE: as discussed here, the site 0 electric meter readings are in kBTU.
- building_metadata.csv
site_id
- Foreign key for the weather files.building_id
- Foreign key for training.csvprimary_use
- Indicator of the primary category of activities for the building based on EnergyStar property type definitionssquare_feet
- Gross floor area of the buildingyear_built
- Year building was openedfloor_count
- Number of floors of the building
- weather.csv Weather data from a meteorological station as close as possible to the site.
site_id
air_temperature
- Degrees Celsiuscloud_coverage
- Portion of the sky covered in clouds, in oktasdew_temperature
- Degrees Celsiusprecip_depth_1_hr
- Millimeterssea_level_pressure
- Millibar/hectopascalswind_direction
- Compass direction (0-360)wind_speed
- Meters per second
Challenge : Gali informasi sebanyak-banyaknya dari data tersebut