Skip to content

Commit cd2c6c0

Browse files
authored
Fix: "kom" -> "komm" (#6)
* Fixing column names to comply with SSB standards: kom_nr -> komm_nr and kom_navn -> komm_navn * Bump version
1 parent 3a9f09f commit cd2c6c0

3 files changed

Lines changed: 19 additions & 19 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ssb-kostra-python"
3-
version = "0.0.2"
3+
version = "0.0.3"
44
description = "SSB Kostra Python"
55
authors = ["Magne Furuholmen Myhren <mfm@ssb.no>"]
66
license = "MIT"

src/ssb_kostra_python/kommunekorr.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def kostra_kommunekorr(year: str) -> pd.DataFrame:
1313
- The correspondence between municipality (KLASS 131) and county (KLASS 104).
1414
1515
The retrieved data is merged into a single DataFrame containing information on:
16-
- Municipality number (kom_nr) and name (kom_navn)
16+
- Municipality number (komm_nr) and name (komm_navn)
1717
- County number (fylke_nr) and name (fylke_navn)
1818
- KOSTRA group number (kostra_gr) and name (kostra_gr_navn)
1919
- Validity start and end dates for both KOSTRA group and county classifications.
@@ -28,8 +28,8 @@ def kostra_kommunekorr(year: str) -> pd.DataFrame:
2828
2929
Returns:
3030
pd.DataFrame: A DataFrame with the following columns:
31-
- kom_nr: Municipality number.
32-
- kom_navn: Municipality name.
31+
- komm_nr: Municipality number.
32+
- komm_navn: Municipality name.
3333
- fylke_nr: County number.
3434
- fylke_navn: County name.
3535
- fylke_nr_eka: County number prefixed with "EKA".
@@ -51,7 +51,7 @@ def kostra_kommunekorr(year: str) -> pd.DataFrame:
5151
>>> df = kostra_kommunekorr("2025")
5252
>>> df['verdi'] = 1000
5353
>>> groups = [
54-
... ['kom_nr', 'kom_navn'],
54+
... ['komm_nr', 'komm_navn'],
5555
... ['fylke_nr', 'fylke_navn'],
5656
... ['kostra_gr', 'kostra_gr_navn'],
5757
... ['landet_u_oslo'],
@@ -70,11 +70,11 @@ def kostra_kommunekorr(year: str) -> pd.DataFrame:
7070
KlassClassification("131", language="nb", include_future=False)
7171
.get_codes(from_date=from_date, to_date=to_date)
7272
.data[["code", "name"]]
73-
.rename(columns={"code": "kom_nr", "name": "kom_navn"})
73+
.rename(columns={"code": "komm_nr", "name": "komm_navn"})
7474
)
7575

7676
# Manually add Longyearbyen
77-
df_longyear = pd.DataFrame({"kom_nr": ["2111"], "kom_navn": ["Longyearbyen"]})
77+
df_longyear = pd.DataFrame({"komm_nr": ["2111"], "komm_navn": ["Longyearbyen"]})
7878
kom = pd.concat([kom, df_longyear], ignore_index=True)
7979

8080
# Retrieve the correspondence between municipality and KOSTRA group (KLASS 112)
@@ -87,7 +87,7 @@ def kostra_kommunekorr(year: str) -> pd.DataFrame:
8787
)
8888
kom_kostra_gr = korresp_kostra.data.rename(
8989
columns={
90-
"sourceCode": "kom_nr",
90+
"sourceCode": "komm_nr",
9191
"targetCode": "kostra_gr",
9292
"targetName": "kostra_gr_navn",
9393
"validFrom": "kostra_validFrom",
@@ -111,7 +111,7 @@ def kostra_kommunekorr(year: str) -> pd.DataFrame:
111111
)
112112
kom_fyl = korresp_fyl.data.rename(
113113
columns={
114-
"sourceCode": "kom_nr",
114+
"sourceCode": "komm_nr",
115115
"targetCode": "fylke_nr",
116116
"targetName": "fylke_navn",
117117
"validFrom": "fylke_validFrom",
@@ -120,29 +120,29 @@ def kostra_kommunekorr(year: str) -> pd.DataFrame:
120120
).drop(columns=["sourceName", "sourceShortName", "targetShortName"])
121121

122122
# Merge the data
123-
kom = pd.merge(kom, kom_kostra_gr, on="kom_nr", how="left", validate="1:m")
124-
kom = pd.merge(kom, kom_fyl, on="kom_nr", how="left", validate="1:m")
123+
kom = pd.merge(kom, kom_kostra_gr, on="komm_nr", how="left", validate="1:m")
124+
kom = pd.merge(kom, kom_fyl, on="komm_nr", how="left", validate="1:m")
125125

126126
# Check for duplicate municipality numbers and raise an error if found
127-
if kom.duplicated("kom_nr").sum() > 0:
128-
duplicates = list(kom[kom.duplicated("kom_nr")]["kom_navn"])
127+
if kom.duplicated("komm_nr").sum() > 0:
128+
duplicates = list(kom[kom.duplicated("komm_nr")]["komm_navn"])
129129
raise ValueError(
130130
"Duplicates detected for municipality numbers: " + ", ".join(duplicates)
131131
)
132132

133-
kom = kom[kom["kom_nr"] != "9999"].copy()
133+
kom = kom[kom["komm_nr"] != "9999"].copy()
134134

135135
# Add extra columns for county data and national categorization
136136
kom["fylke_nr_eka"] = "EKA" + kom["fylke_nr"].str[:2]
137137
kom["fylke_nr_eka_m_tekst"] = kom["fylke_nr_eka"] + " " + kom["fylke_navn"]
138138
kom["landet"] = "EAK Landet"
139139
kom["landet_u_oslo"] = "EAKUO Landet uten Oslo"
140-
kom.loc[kom["kom_nr"] == "0301", "landet_u_oslo"] = pd.NA
140+
kom.loc[kom["komm_nr"] == "0301", "landet_u_oslo"] = pd.NA
141141

142142
return kom[
143143
[
144-
"kom_nr",
145-
"kom_navn",
144+
"komm_nr",
145+
"komm_navn",
146146
"fylke_nr",
147147
"fylke_navn",
148148
"fylke_nr_eka",

tests/test_kommunekorr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def test_kostra_kommunekorr(mock_klass_classification, mock_klass_correspondence
1010
assert result.shape[0] == 7
1111

1212
exp_cols = [
13-
"kom_nr",
14-
"kom_navn",
13+
"komm_nr",
14+
"komm_navn",
1515
"fylke_nr",
1616
"fylke_navn",
1717
"fylke_nr_eka",

0 commit comments

Comments
 (0)