Skip to content

Commit 3cd3baa

Browse files
authored
Merge pull request #121 from lincc-frameworks/fixed_column_ordering
Count Nested: Fixed Column Ordering
2 parents 307c904 + 90e6915 commit 3cd3baa

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/nested_pandas/utils/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ def count_nested(df, nested, by=None, join=True) -> NestedFrame:
3434
)
3535
else:
3636
# this may be able to be sped up using tolists() as well
37-
counts = df[nested].apply(lambda x: x[by].value_counts())
37+
counts = df[nested].apply(lambda x: x[by].value_counts(sort=False))
3838
counts = counts.rename(columns={colname: f"n_{nested}_{colname}" for colname in counts.columns})
39+
counts = counts.reindex(sorted(counts.columns), axis=1)
3940
if join:
4041
return df.join(counts)
4142
# else just return the counts NestedFrame

tests/nested_pandas/utils/test_utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_count_nested(join):
1515
data={
1616
"c": [0, 2, 4, 1, np.nan, 3, 1, 4, 1],
1717
"d": [5, 4, 7, 5, 3, 1, 9, 3, 4],
18-
"label": ["a", "a", "b", "b", "a", "a", "b", "a", "b"],
18+
"label": ["b", "a", "b", "b", "a", "a", "b", "a", "b"],
1919
},
2020
index=[0, 0, 0, 1, 1, 1, 2, 2, 2],
2121
)
@@ -27,8 +27,14 @@ def test_count_nested(join):
2727

2828
# Test count by
2929
label_counts = count_nested(base, "nested", by="label", join=join)
30-
assert all(label_counts["n_nested_a"].values == [2, 2, 1])
31-
assert all(label_counts["n_nested_b"].values == [1, 1, 2])
30+
31+
assert all(label_counts["n_nested_a"].values == [1, 2, 1])
32+
assert all(label_counts["n_nested_b"].values == [2, 1, 2])
33+
34+
# Make sure the ordering is alphabetical
35+
# https://github.com/lincc-frameworks/nested-pandas/issues/109
36+
assert label_counts.columns[-1] == "n_nested_b"
37+
assert label_counts.columns[-2] == "n_nested_a"
3238

3339
# Test join behavior
3440
if join:

0 commit comments

Comments
 (0)