Skip to content

Commit 546a92b

Browse files
committed
Complete unwanted aggregated on df creation
Imitate prior behaviour wrt na and 0 values Add test case for mixed type dataframes Completes #1323
1 parent 5c2e675 commit 546a92b

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

TM1py/Utils/Utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,14 +623,13 @@ def build_dataframe_from_csv(
623623

624624
# create a unique row id within each (idx_cols + col_col) group
625625
df["_dup"] = df.groupby(idx_cols + [col_col]).cumcount()
626-
627626
df = (
628627
df.pivot(
629628
index=idx_cols + ["_dup"],
630629
columns=col_col,
631630
values=val_col,
632631
)
633-
.dropna(how="all")
632+
.fillna(0)
634633
.reset_index()
635634
.drop(columns="_dup")
636635
)

Tests/Utils_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ def test_build_dataframe_from_csv(self):
150150

151151
pd._testing.assert_frame_equal(expected_df, df, check_column_type=False)
152152

153+
def test_build_dataframe_from_csv_shaped_numbers_and_strings(self):
154+
raw_csv = "Region~Product~Measure~Value\r\n" "r1~p1~Revenue~1.0\r\n" "r1~p2~Revenue~3.0\r\n" "r1~p1~Comment~Great Product\r\n" "r1~p2~Comment~"
155+
df = build_dataframe_from_csv(raw_csv, dtype={"Revenue": float}, shaped=True)
156+
157+
expected_df = pd.DataFrame(
158+
{
159+
"Region": ["r1", "r1"],
160+
"Product": ["p1", "p2"],
161+
"Comment": ["Great Product", ""],
162+
"Revenue": [1.00000, 3.00000],
163+
}
164+
)
165+
166+
# explicit conversion for exact comparison
167+
expected_df["Revenue"] = expected_df["Revenue"].astype(float)
168+
df["Revenue"] = df["Revenue"].astype(float)
169+
170+
pd._testing.assert_frame_equal(expected_df, df)
171+
153172
def test_build_dataframe_from_csv_shaped(self):
154173
raw_csv = "Region~Product~Measure~Value\r\n" "r1~p1~Revenue~1.0\r\n" "r1~p2~Revenue~3.0\r\n" "r2~p2~Revenue~4.0"
155174
df = build_dataframe_from_csv(raw_csv, dtype={"Revenue": float}, shaped=True)

0 commit comments

Comments
 (0)