Repository: astropy/astropy

Issue:
Grouped table aggregation by sum returns incorrect values for zero-length tables
### Description

Table aggregation by sum does not correctly handle zero-length arrays. Instead, it prints a warning and drops the numerical columns.

Zero-length arrays are not a problem for Numpy...

```pycon
>>> np.sum([])
np.float64(0.0)
>>> np.add.reduce([])
np.float64(0.0)
```

### Expected behavior

The code sample below should produce the following output (and no warnings):

```
<Table length=0>
action duration
 str1  float64 
------ --------
```

### How to Reproduce

```pycon
>>> from astropy.table import Table
>>> import numpy as np
>>> table = Table({
...     "action": np.asarray([], dtype=str),
...     "duration": np.asarray([], dtype=float)
... })
>>> table.group_by("action").groups.aggregate(np.sum)
WARNING: Cannot aggregate column 'duration' with type 'float64': index 0 out-of-bounds in add.reduceat [0, 0) [astropy.table.groups]
<Table length=0>
action
 str1 
------
```

### Versions

```
macOS-14.7.1-arm64-arm-64bit
Python 3.11.10 (main, Sep  7 2024, 05:47:53) [Clang 15.0.0 (clang-1500.1.0.2.5)]
astropy 6.1.5
Numpy 2.1.3
pyerfa 2.0.1.4
Scipy 1.14.1
Matplotlib 3.9.2
```


Discussion:

Patch:
diff --git a/astropy/table/groups.py b/astropy/table/groups.py
index a60ebda827..8b8bdf5154 100644
--- a/astropy/table/groups.py
+++ b/astropy/table/groups.py
@@ -254,7 +254,7 @@ class ColumnGroups(BaseGroups):
     @property
     def indices(self):
         # If the parent column is in a table then use group indices from table
-        if self.parent_table:
+        if self.parent_table is not None:
             return self.parent_table.groups.indices
         else:
             if self._indices is None:
@@ -265,7 +265,7 @@ class ColumnGroups(BaseGroups):
     @property
     def keys(self):
         # If the parent column is in a table then use group indices from table
-        if self.parent_table:
+        if self.parent_table is not None:
             return self.parent_table.groups.keys
         else:
             return self._keys
diff --git a/docs/changes/table/17385.bugfix.rst b/docs/changes/table/17385.bugfix.rst
new file mode 100644
index 0000000000..c31e40a9b2
--- /dev/null
+++ b/docs/changes/table/17385.bugfix.rst
@@ -0,0 +1,1 @@
+Fixed table aggregate with empty columns when float is present.


Test patch:
diff --git a/astropy/table/tests/test_groups.py b/astropy/table/tests/test_groups.py
index 7f3065fd37..0ab8676b44 100644
--- a/astropy/table/tests/test_groups.py
+++ b/astropy/table/tests/test_groups.py
@@ -576,6 +576,19 @@ def test_table_aggregate_reduceat(T1):
     assert tga.pformat() == [" a ", "---", "  0", "  1", "  2"]
 
 
+def test_table_aggregate_reduceat_empty():
+    for masked in (False, True):
+        tg = Table(
+            {
+                "action": np.asarray([], dtype=str),
+                "duration": np.asarray([], dtype=float),
+            },
+            masked=masked,
+        )
+        tga = tg.group_by("action").groups.aggregate(np.sum)
+        assert tga.pformat() == ["action duration", "------ --------"]
+
+
 def test_column_aggregate(T1):
     """
     Aggregate a single table column