-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
maj fossil_reserves_OPEC #22
Open
Lohofora
wants to merge
3
commits into
dataforgoodfr:main
Choose a base branch
from
Lohofora:fossil_reserves
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
data-preparation/src/data_processing/FOSSIL_RESERVES_opec_fossil_with_zones_prod_2023.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#Fichier Python permettant de générer : | ||
#__FOSSIL_RESERVES_bp_fossil_with_zones_prod.csv__# | ||
|
||
import pandas as pd | ||
import numpy as np | ||
from utils.translation import CountryTranslatorFrenchToEnglish | ||
from transformation.demographic.countries import StatisticsPerCountriesAndZonesJoiner | ||
from utils.format import StatisticsDataframeFormatter | ||
|
||
# Charger donnée "country_groups.csv" | ||
|
||
# Charger données OPEC -> pas API disponible | ||
# site source Maj annuelle : https://publications.opec.org/asb | ||
|
||
class OpecFossilProvenReservesCleaner: | ||
def __init__(self) : | ||
pass | ||
|
||
def rename_column(self, df) : | ||
return df.rename(columns={'Unnamed: 0': 'country'}) | ||
|
||
def drop_unnecessary_lines(self, df) : | ||
""" | ||
Drop footnotes not necessary for data processing | ||
""" | ||
df.set_index('country', inplace=True) | ||
df = df.loc[:"Total World", :] | ||
df.drop(['Africa', 'Latin America', 'Other Asia', 'Other Eurasia', 'Middle East', 'OECD Europe', 'OECD Asia Pacific', 'OECD Americas', 'Others', 'Other Europe', 'Total World'], axis = 0, inplace = True) | ||
return df | ||
|
||
def mb_to_gb_scale(self, df) : | ||
df = df.apply(lambda x: round(pd.to_numeric(x, errors='coerce') / 1000, 3)) | ||
return df | ||
|
||
def translate_country(self, df) : | ||
df = df.reset_index() | ||
df["country"] = CountryTranslatorFrenchToEnglish().run(df["country"], raise_errors=False) | ||
return df | ||
|
||
def column_to_line(self, df) : | ||
df = pd.melt(df, | ||
id_vars = "country", | ||
var_name = "year", | ||
value_name = "proven_reserves") | ||
df.dropna(inplace = True) | ||
return df | ||
|
||
def stat_per_countries_zones_join(self, df, country) : | ||
list_col_group_by = ['group_type', 'group_name', 'year'] | ||
dict_agg = {"proven_reserves" : "sum"} | ||
df = StatisticsPerCountriesAndZonesJoiner().run(df, country, list_col_group_by, dict_agg) | ||
return df | ||
|
||
def stat_df_formatter(self, df) : | ||
col_statistics = "proven_reserves" | ||
df = StatisticsDataframeFormatter().select_and_sort_values(df, col_statistics) | ||
df["energy_source"] = "Oil" | ||
df["proven_reserves_unit"] = "Gb" | ||
df = df[["group_type", "group_name", "energy_source", "year", "proven_reserves", "proven_reserves_unit"]] | ||
return df | ||
|
||
def clean_data(self, df, country): | ||
df = self.rename_column(df) | ||
df = self.drop_unnecessary_lines(df) | ||
df = self.mb_to_gb_scale(df) | ||
df = self.translate_country(df) | ||
df = self.column_to_line(df) | ||
df = self.stat_per_countries_zones_join(df, country) | ||
df = self.stat_df_formatter(df) | ||
return df | ||
|
||
# Application class | ||
cleaner = OpecFossilProvenReservesCleaner() | ||
df_new = cleaner.clean_data(df, country) | ||
df_new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
J'ajouterais aussi un commentaire pour dire où se trouve ces lignes vides et les notes de bas de page. Et dans la docstring, j'expliquerais aussi pourquoi on exclut certains continents et certains groupements de pays, car sur le site on peut filtrer sur ce type de zones géographiques. Ça nous évitera aussi de nous reposer la question dans quelques mois :)
Ça donnerait quelque chose comme ça :
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C'est plus clair tu as raison merci