Skip to content

Commit

Permalink
Merge pull request #36 from statisticsnorway/fame_functions
Browse files Browse the repository at this point in the history
Fame functions
  • Loading branch information
joxssb authored Jun 12, 2024
2 parents 07435b6 + c5c8c6d commit 5f76ae1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ssb-konjunk"
version = "0.0.5"
version = "0.0.6"
description = "SSB Konjunk"
authors = ["Edvard Garmannslund <[email protected]>"]
license = "MIT"
Expand Down
53 changes: 53 additions & 0 deletions src/ssb_konjunk/fame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""A collection of functions to make fame files in the cloud.
The template and this example uses Google style docstrings as described at:
https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
"""

# Importing external packages
import pandas as pd
from dapla import FileClient

# Getting filesystem
fs = FileClient.get_gcs_file_system()


def change_date_format_fame(series: pd.Series) -> pd.Series:
"""Function for turning ISO-8601 to fame time format.
Args:
series: A pandas series containing string for dates in format ISO-8601(YYYY-mm-dd).
Returns:
pd.Series: A pandas series with dates in fame format(YYYY:M:D)
"""
series = pd.to_datetime(series)

# Format the datetime column as "YYYY:M:D"
series = series.dt.strftime("%Y:%-m:%-d")

return series


def write_out_fame_format_txt(
names: pd.Series,
dates: pd.Series,
values: pd.Series,
gcp_path: str,
) -> None:
"""Function to write out txt file in fame format.
Args:
names: Pandas series containing name or type for value.
dates: Pandas series containing date for values.
values: Pandas series containing values.
gcp_path: String to google cloud.
"""
with fs.open(gcp_path, "w") as f:
# Write data rows
for name, date, value in zip(names, dates, values, strict=False):
# Apply format specification
formatted_value = f"{value:20.2f}"
# Write data row
f.write(f"{name:<35}{date:<50}{formatted_value}\n")

0 comments on commit 5f76ae1

Please sign in to comment.