Skip to content
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

Fame functions #36

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Loading