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

[BUG] Cannot use 'Value' as the column name for observation column #857

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 16 additions & 2 deletions src/csvcubed/utils/csvdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,29 @@ def _melt_data_set(
]
id_cols = list(set(data_set.columns) - set(value_cols))

# Checking for any columns with the title "Value" and changing the value_name
# parameter passed to the melt function to "Not-Value" so that we don't
# trigger a pandas ValueError.
CharlesRendle marked this conversation as resolved.
Show resolved Hide resolved
value_name = "Value"
for col_title in value_cols:
if col_title == "Value":
value_name = "Not-Value"

# Melting the data set using pandas melt function.
return pd.melt(
melted_df = pd.melt(
data_set,
id_vars=id_cols,
value_vars=value_cols,
value_name="Value",
value_name=value_name,
var_name="Observation Value",
)

# Renaming columns in the returned melted df to their original title "Value"
if value_name == "Not-Value":
melted_df.rename(columns={"Not-Value": "Value"}, inplace=True)

return melted_df


def _get_unit_measure_col_for_standard_shape_cube(
qube_components: List[QubeComponentResult],
Expand Down
53 changes: 53 additions & 0 deletions tests/unit/inspect/test_inspectdatasetmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@
]
).replace("", np.NAN)

_expected_by_measure_and_unit_val_counts_df_with_column_titled_value = DataFrame(
[
{
"Measure": "gas emissions(gwp-ar4)",
"Unit": "millions of tonnes of carbon dioxide (mt co2)",
"Count": 19,
}
]
).replace("", np.NAN)


def get_arguments_qb_dataset(
data_cube_repository: DataCubeRepository,
Expand Down Expand Up @@ -589,6 +599,7 @@ def test_get_val_counts_info_multi_unit_single_measure_dataset():
/ "new"
/ "multi-unit-single-measure-dataset.csv-metadata.json"
)

data_cube_repository = get_data_cube_repository(csvw_metadata_json_path)

(dataset, qube_components, csv_url) = get_arguments_qb_dataset(data_cube_repository)
Expand Down Expand Up @@ -829,3 +840,45 @@ def test_get_concepts_hierarchy_info_hierarchy_with_depth_more_than_one():
assert isinstance(result.tree, Tree)
assert result.tree.depth() == 2
assert len(result.tree.all_nodes_itr()) == 10


def test_melt_works_when_column_titled_value_exists():
"""
When a df with a column titled "Value" is passed to the melt function in
pandas 2.0 a ValueError is produced because of the column title. This test
uses a dataset with a "Value" titled column and should be melted when
transform_dataset_to_canonical_shape is called. In this case, the "value_name
CharlesRendle marked this conversation as resolved.
Show resolved Hide resolved
parameter of melt() is given a different string (default is "Value") and then
we rename this column back to "Value" in the returned melted df.
"""
csvw_metadata_json_path = (
_test_case_base_dir
/ "multi-unit_single-measure"
/ "final-uk-greenhouse-gas-emissions-national-statistics-1990-to-2019.csv-metadata.json"
)

data_cube_repository = get_data_cube_repository(csvw_metadata_json_path)

(dataset, qube_components, csv_url) = get_arguments_qb_dataset(data_cube_repository)

(
canonical_shape_dataset,
measure_col,
unit_col,
) = transform_dataset_to_canonical_shape(
data_cube_repository,
dataset,
csv_url,
qube_components,
)

result: DatasetObservationsByMeasureUnitInfoResult = get_dataset_val_counts_info(
canonical_shape_dataset, measure_col, unit_col
)

assert result is not None
assert set(dataset.columns).issubset(set(canonical_shape_dataset.columns))
assert_frame_equal(
result.by_measure_and_unit_val_counts_df,
_expected_by_measure_and_unit_val_counts_df_with_column_titled_value,
)
Loading