Repository: lincc-frameworks/nested-pandas

Issue:
Improve support for `reduce` returning nested information
**Feature request**
We expect users that are applying their functions via reduce to sometimes want more complex outputs. For example, if I'm applying Lombscargle to all lightcurves in my nested dataset, I may want a result that has a max power column, a frequency of max power column, and a nested column holding the full periodogram. Presently, we don't really support this as we primarily want the user to return a dictionary-style output, which will limit the user towards producing list columns rather than nested columns, for example:

```
from nested_pandas.datasets import generate_data
import numpy as np

ndf = generate_data(3,20)

def complex_output(flux):
    return {"max_flux":np.max(flux), "flux_quantiles":np.quantile(flux, [0.1,0.2,0.3,0.4,0.5])}

ndf.reduce(complex_output, "nested.flux")

	max_flux	flux_quantiles
0	98.744076	[15.293187217097268, 21.834338973710633, 25.02...
1	98.502034	[6.337989346945357, 8.019180689729948, 9.69707...
2	99.269021	[12.42551556001139, 15.901779148332189, 26.199...
```

With #131 it would be easier to convert this to a nested output, but I think ideally `reduce` would have a more native ability to produce nested structures for these types of functions. My thinking about how we might try to facilitate this, is to read more into the user defined dictionary output to determine the nestedframe. For example, a user could instead specify this `reduce` function:

```
def complex_output(flux):
    return {"max_flux":np.max(flux), "quantiles":{"flux_quantiles":np.quantile(flux, [0.1,0.2,0.3,0.4,0.5])}}
```

The json-like nesting of dataframes would signal that the user would like a "quantiles" nested column with a "flux_quantiles" field. I'm not entirely sure on the full implementation plan, but this seems most intuitive from the users perspective.

Also consider #101 as an alternate interface.

**Before submitting**
Please check the following:

- [ ] I have described the purpose of the suggested change, specifying what I need the enhancement to accomplish, i.e. what problem it solves.
- [ ] I have included any relevant links, screenshots, environment information, and data relevant to implementing the requested feature, as well as pseudocode for how I want to access the new functionality.
- [ ] If I have ideas for how the new feature could be implemented, I have provided explanations and/or pseudocode and/or task lists for the steps.

Hints from issue comments:
@dougbrn: Noticing this is actually half a resubmit of #101, but each ticket proposes a different solution. We should talk about this at an upcoming meeting

Solution patch:
diff --git a/src/nested_pandas/nestedframe/core.py b/src/nested_pandas/nestedframe/core.py
index ab08717d..d86c1a4b 100644
--- a/src/nested_pandas/nestedframe/core.py
+++ b/src/nested_pandas/nestedframe/core.py
@@ -845,7 +845,7 @@ def sort_values(
                 return None
             return new_df
 
-    def reduce(self, func, *args, **kwargs) -> NestedFrame:  # type: ignore[override]
+    def reduce(self, func, *args, infer_nesting=True, **kwargs) -> NestedFrame:  # type: ignore[override]
         """
         Takes a function and applies it to each top-level row of the NestedFrame.
 
@@ -862,6 +862,12 @@ def reduce(self, func, *args, **kwargs) -> NestedFrame:  # type: ignore[override
         args : positional arguments
             Positional arguments to pass to the function, the first *args should be the names of the
             columns to apply the function to.
+        infer_nesting : bool, default True
+            If True, the function will pack output columns into nested
+            structures based on column names adhering to a nested naming
+            scheme. E.g. "nested.b" and "nested.c" will be packed into a column
+            called "nested" with columns "b" and "c". If False, all outputs
+            will be returned as base columns.
         kwargs : keyword arguments, optional
             Keyword arguments to pass to the function.
 
@@ -915,7 +921,30 @@ def reduce(self, func, *args, **kwargs) -> NestedFrame:  # type: ignore[override
                 iterators.append(self[layer].array.iter_field_lists(col))
 
         results = [func(*cols, *extra_args, **kwargs) for cols in zip(*iterators)]
-        return NestedFrame(results, index=self.index)
+        results_nf = NestedFrame(results, index=self.index)
+
+        if infer_nesting:
+            # find potential nested structures from columns
+            nested_cols = list(
+                np.unique(
+                    [
+                        column.split(".", 1)[0]
+                        for column in results_nf.columns
+                        if isinstance(column, str) and "." in column
+                    ]
+                )
+            )
+
+            # pack results into nested structures
+            for layer in nested_cols:
+                layer_cols = [col for col in results_nf.columns if col.startswith(f"{layer}.")]
+                rename_df = results_nf[layer_cols].rename(columns=lambda x: x.split(".", 1)[1])
+                nested_col = pack_lists(rename_df, name=layer)
+                results_nf = results_nf[
+                    [col for col in results_nf.columns if not col.startswith(f"{layer}.")]
+                ].join(nested_col)
+
+        return results_nf
 
     def to_parquet(self, path, by_layer=False, **kwargs) -> None:
         """Creates parquet file(s) with the data of a NestedFrame, either


Test patch:
diff --git a/tests/nested_pandas/nestedframe/test_nestedframe.py b/tests/nested_pandas/nestedframe/test_nestedframe.py
index fc9d3103..5d5918fd 100644
--- a/tests/nested_pandas/nestedframe/test_nestedframe.py
+++ b/tests/nested_pandas/nestedframe/test_nestedframe.py
@@ -1022,6 +1022,67 @@ def cols_allclose(col1, col2):
     )
 
 
+def test_reduce_infer_nesting():
+    """Test that nesting inference works in reduce"""
+
+    ndf = generate_data(3, 20, seed=1)
+
+    # Test simple case
+    def complex_output(flux):
+        return {
+            "max_flux": np.max(flux),
+            "lc.flux_quantiles": np.quantile(flux, [0.1, 0.2, 0.3, 0.4, 0.5]),
+        }
+
+    result = ndf.reduce(complex_output, "nested.flux")
+    assert list(result.columns) == ["max_flux", "lc"]
+    assert list(result.lc.nest.fields) == ["flux_quantiles"]
+
+    # Test multi-column nested output
+    def complex_output(flux):
+        return {
+            "max_flux": np.max(flux),
+            "lc.flux_quantiles": np.quantile(flux, [0.1, 0.2, 0.3, 0.4, 0.5]),
+            "lc.labels": [0.1, 0.2, 0.3, 0.4, 0.5],
+        }
+
+    result = ndf.reduce(complex_output, "nested.flux")
+    assert list(result.columns) == ["max_flux", "lc"]
+    assert list(result.lc.nest.fields) == ["flux_quantiles", "labels"]
+
+    # Test integer names
+    def complex_output(flux):
+        return np.max(flux), np.quantile(flux, [0.1, 0.2, 0.3, 0.4, 0.5]), [0.1, 0.2, 0.3, 0.4, 0.5]
+
+    result = ndf.reduce(complex_output, "nested.flux")
+    assert list(result.columns) == [0, 1, 2]
+
+    # Test multiple nested structures output
+    def complex_output(flux):
+        return {
+            "max_flux": np.max(flux),
+            "lc.flux_quantiles": np.quantile(flux, [0.1, 0.2, 0.3, 0.4, 0.5]),
+            "lc.labels": [0.1, 0.2, 0.3, 0.4, 0.5],
+            "meta.colors": ["green", "red", "blue"],
+        }
+
+    result = ndf.reduce(complex_output, "nested.flux")
+    assert list(result.columns) == ["max_flux", "lc", "meta"]
+    assert list(result.lc.nest.fields) == ["flux_quantiles", "labels"]
+    assert list(result.meta.nest.fields) == ["colors"]
+
+    # Test only nested structure output
+    def complex_output(flux):
+        return {
+            "lc.flux_quantiles": np.quantile(flux, [0.1, 0.2, 0.3, 0.4, 0.5]),
+            "lc.labels": [0.1, 0.2, 0.3, 0.4, 0.5],
+        }
+
+    result = ndf.reduce(complex_output, "nested.flux")
+    assert list(result.columns) == ["lc"]
+    assert list(result.lc.nest.fields) == ["flux_quantiles", "labels"]
+
+
 def test_scientific_notation():
     """
     Test that NestedFrame.query handles constants that are written in scientific notation.