Skip to content

Commit f28da17

Browse files
authored
Update describe for edge case (#323)
* update nested describe for edge case * pre-commit-ci fix * update variable name
1 parent 53c988e commit f28da17

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/nested_pandas/nestedframe/core.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,13 @@ def describe(self, exclude_nest: bool = False, percentiles=None, include=None, e
965965
if not result:
966966
raise ValueError(f"All columns in {check} failed.\n" + "\n".join(errors))
967967

968+
if include is None and exclude is None:
969+
# try only get the numeric columns and drop the others
970+
numeric_dtypes = [r.select_dtypes(include=[np.number]) for r in result]
971+
non_empty_numeric_dtypes = [r for r in numeric_dtypes if not r.empty]
972+
if non_empty_numeric_dtypes:
973+
result = non_empty_numeric_dtypes
974+
968975
return NestedFrame(pd.concat(result, axis=1))
969976

970977
def eval(self, expr: str, *, inplace: bool = False, **kwargs) -> Any | None:

tests/nested_pandas/nestedframe/test_nestedframe.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,9 @@ def test_describe():
14841484
"""Test NestedFrame.describe gives correct results with and without the nested columns"""
14851485
base_mix = NestedFrame(data={"a": [1, 2, 3], "b": ["2", "4", "6"], "c": ["x", "y", "z"]}, index=[0, 1, 2])
14861486
base_num = NestedFrame(data={"a": [1, 2, 3], "b": [2, 3, 2], "c": [55, 55, 55]}, index=[0, 1, 2])
1487+
base_obj = NestedFrame(
1488+
data={"a": ["a", "b", "c"], "b": ["2", "4", "6"], "c": ["x", "y", "z"]}, index=[0, 1, 2]
1489+
)
14871490

14881491
nested_num = pd.DataFrame(
14891492
data={"d": [10, 11, 20, 21, 3, 31, 32], "y": [1, 2, 3, 4, 5, 6, 7]}, index=[0, 0, 1, 1, 1, 2, 2]
@@ -1626,6 +1629,14 @@ def test_describe():
16261629
with pytest.raises(ValueError):
16271630
base2.describe(include=object)
16281631

1632+
# edge case: object base with numeric nest
1633+
base_obj = base_obj.add_nested(nested_mix, "nested_mix").add_nested(nested_num, "nested_num")
1634+
r18 = base_obj.describe()
1635+
assert isinstance(r18, NestedFrame)
1636+
assert r18.shape[1] == 3
1637+
assert "nested_mix.f" in r18.columns
1638+
assert "top" not in r18.index
1639+
16291640

16301641
def test_eval():
16311642
"""

0 commit comments

Comments
 (0)