Repository: lincc-frameworks/nested-pandas

Issue:
Cannot assign a new column to an empty NestedFrame
**Bug report**

```
from nested_pandas.datasets import generate_data

nf = generate_data(3, 10).iloc[:0]
nf['nested.x'] = []
```

```
ValueError                                Traceback (most recent call last)
Cell In[7], line 1
----> 1 nf['nested.x'] = []

File ~/projects/lincc-frameworks/nested-pandas/src/nested_pandas/nestedframe/core.py:214, in NestedFrame.__setitem__(self, key, value)
    212         new_nested_series = self[nested].nest.with_filled_field(field, value)
    213     else:
--> 214         new_nested_series = self[nested].nest.with_flat_field(field, value)
    215     return super().__setitem__(nested, new_nested_series)
    217 # Adding a new nested structure from a column
    218 # Allows statements like ndf["new_nested.t"] = ndf["nested.t"] - 5

File ~/projects/lincc-frameworks/nested-pandas/src/nested_pandas/series/accessor.py:169, in NestSeriesAccessor.with_flat_field(self, field, value)
    153 """Set the field from flat-array of values and return a new series
    154
    155 Parameters
   (...)
    166     The new series with the field set.
    167 """
    168 new_array = self._series.array.copy()
--> 169 new_array.set_flat_field(field, value)
    170 return pd.Series(new_array, copy=False, index=self._series.index, name=self._series.name)

File ~/projects/lincc-frameworks/nested-pandas/src/nested_pandas/series/ext_array.py:911, in NestedExtensionArray.set_flat_field(self, field, value, keep_dtype)
    904     raise TypeError(
    905         f"New values must be convertible to the existing element pyarrow type, {pa_type}. "
    906         "If you want to replace field with values of a new type, use series.nest.with_flat_field() "
    907         "or NestedExtensionArray.set_flat_field(..., keep_dtype=False) instead."
    908     ) from e
    910 if len(pa_array) != self.flat_length:
--> 911     raise ValueError("The input must be a struct_scalar or have the same length as the flat arrays")
    913 if isinstance(pa_array, pa.ChunkedArray):
    914     pa_array = pa_array.combine_chunks()

ValueError: The input must be a struct_scalar or have the same length as the flat arrays
```

**Before submitting**
Please check the following:

- [x] I have described the situation in which the bug arose, including what code was executed, information about my environment, and any applicable data others will need to reproduce the problem.
- [x] I have included available evidence of the unexpected behavior (including error messages, screenshots, and/or plots) as well as a description of what I expected instead.
- [ ] If I have a solution in mind, I have provided an explanation and/or pseudocode and/or task list.


Discussion hints:

Solution patch:
diff --git a/src/nested_pandas/series/ext_array.py b/src/nested_pandas/series/ext_array.py
index 43735db..7536c7f 100644
--- a/src/nested_pandas/series/ext_array.py
+++ b/src/nested_pandas/series/ext_array.py
@@ -798,7 +798,10 @@ class NestedExtensionArray(ExtensionArray):
     @property
     def flat_length(self) -> int:
         """Length of the flat arrays"""
-        return pa.compute.sum(self.list_lengths).as_py()
+        sum_result = pa.compute.sum(self.list_lengths).as_py()
+        if sum_result is None:
+            sum_result = 0
+        return sum_result
 
     @property
     def num_chunks(self) -> int:


Test patch:
diff --git a/tests/nested_pandas/nestedframe/test_nestedframe.py b/tests/nested_pandas/nestedframe/test_nestedframe.py
index 5d5918f..9d4faf6 100644
--- a/tests/nested_pandas/nestedframe/test_nestedframe.py
+++ b/tests/nested_pandas/nestedframe/test_nestedframe.py
@@ -1277,3 +1277,9 @@ def test_issue193():
     ndf.query("nested.flux / nested.t > 0")
     # This failed with numpy 1 with:
     # TypeError: Cannot interpret 'double[pyarrow]' as a data type
+
+
+def test_issue235():
+    """https://github.com/lincc-frameworks/nested-pandas/issues/235"""
+    nf = generate_data(3, 10).iloc[:0]
+    nf["nested.x"] = []