# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Consumption analysis
# ### Author : <NAME> (https://github.com/NicolasLacroix)
#
# Licence : [Apache License 2.0]
#
# Data provided by [DataSud]
#
# Source file link (csv) : https://trouver.datasud.fr/dataset/8bfa93b0-ac2f-4148-b550-0ec5c917bb28/resource/52a8f5dd-758d-4e54-a837-8fc7ad57d378/download/eco2mix-regional-tr.csv
#
# [DataSud]: https://www.datasud.fr/
#
# [Apache License 2.0]: https://github.com/NicolasLacroix/data-representation/blob/master/LICENSE

# +
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

import dateutil.parser
from datetime import datetime, date
# -

data_link = 'https://trouver.datasud.fr/dataset/8bfa93b0-ac2f-4148-b550-0ec5c917bb28/resource/52a8f5dd-758d-4e54-a837-8fc7ad57d378/download/eco2mix-regional-tr.csv'
data = pd.read_csv(data_link, delimiter=';', encoding='utf_8', parse_dates=True)

data.head()

# TODO: use parse_date=True in pd.read_csv method instead
data['Date'] = pd.to_datetime(data['Date'])
data['Heure'] = pd.to_datetime(data['Heure'], format='%H:%M', utc=True).dt.time
data['Date - Heure'] = pd.to_datetime(data['Date - Heure'], format='%Y-%m-%dT%H:%M:%S', utc=True)
data['Date - Heure'] = data['Date - Heure'].dt.tz_convert('Europe/Paris')
data.dtypes

volumeLabels = list(data.columns.values)[6:15]
percentLabels = list(data.columns.values)[15:-1]

# Sorting data by ['Date - Heure']
data = data.sort_values(by=['Date - Heure'])
data.head()

# Today values
today = datetime.combine(date.today(), datetime.min.time())
dailyData = data.loc[data['Date'] == today][['Date - Heure']+volumeLabels]
dailyData.plot(x='Date - Heure', title="Today values")
plt.show()


def getExtremums(data, key):
    dailyData = getDailyData(data, 'Date - Heure', volumeLabels) # get dataframes per day
    min_serie = data.loc[data[key] == min(data[key])]['Date'] # Date column of data's serie where data[key] is min
    min_df = dailyData[pd.to_datetime(min_serie.values[0]).strftime('%Y-%m-%d')] # cell's value (date) to string
    max_serie = data.loc[data[key] == max(data[key])]['Date'] # Date column of data's serie where data[key] is max
    max_df = dailyData[pd.to_datetime(max_serie.values[0]).strftime('%Y-%m-%d')] # cell's value (date) to string
    return min_df, max_df


min_serie = data.min()
max_serie = data.max()
min_df = data.loc[data['Date'] == min_serie['Date']][['Date - Heure']+volumeLabels]
max_df = data.loc[data['Date'] == max_serie['Date']][['Date - Heure']+volumeLabels]

max_value = max_serie['Consommation (MW)']
min_value = min_serie['Consommation (MW)']
min_df.plot(x='Date - Heure', title="Minimum")
max_df.plot(x='Date - Heure', title="Maximum")
plt.show()

avg_df = data.mean()
avg_df

# average values per day
avg_day = data[['Date'] + volumeLabels].groupby(['Date']).agg(np.mean)
avg_day.plot(title="average values per day")
plt.show()

# average values per hour
avg_hr = data[['Heure'] + volumeLabels].groupby(['Heure']).agg(np.mean)
ax = avg_hr.plot(title="average values per hour")
plt.show()

# average percentage per day
percent_df = data[percentLabels].mean()
ax = percent_df.plot(autopct='%.2f', kind='pie', title='average percentage per day')
ax.set_ylabel('')
plt.show()



StarcoderdataJupyterScriptsDedupFiltered