Repository: pandas-dev/pandas
Pull request: TST: GH 27185 Test to check df with timedelta & Int NA sums correctly
- [x] closes #27185
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [NA] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [NA] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.

Open to feedback on how to improve the PR - not sure if this is exactly what the original issue was looking for.


Problem statement:
DataFrame.sum() with mix of timedelta and IntegerNA returns NA for IntNA
```python
In [43]: df = pd.DataFrame({"A": pd.to_timedelta([0, 1, None]), "B": pd.array([1, 2, None], dtype='Int32')})

In [44]: df.sum()
Out[44]:
A   00:00:00.000000
B               NaT
dtype: timedelta64[ns]
```

cc @jbrockmendel 

Not a blocker for the RC, but is a blocker for the release.

Hints from issue discussion:
Looking into this now. It's kinda messy, but I think there are maybe two bugs.

```python
In [4]: df = pd.DataFrame({'A': pd.Series([0, None], dtype='timedelta64[ns]'), 'B': pd.array([0, None], dtype='Int64')})

In [5]: df.sum()
> /Users/taugspurger/sandbox/pandas/pandas/core/apply.py(306)wrap_results()
-> if len(results) > 0 and is_sequence(results[0]):
(Pdb) results
{0: Timedelta('0 days 00:00:00'), 1: nan}
```

That looks fishy. `results[1]` (for the IntNA column) should be 0, not NaN. If we fix that, we'd fix the output. But that would leave a second buggy case where you have all-NA.

```python
(Pdb) n
> /Users/taugspurger/sandbox/pandas/pandas/core/apply.py(311)wrap_results()
-> result = self.obj._constructor_sliced(results)
(Pdb) n
> /Users/taugspurger/sandbox/pandas/pandas/core/apply.py(312)wrap_results()
-> result.index = self.res_index
(Pdb) result
0   0 days
1      NaT
dtype: timedelta64[ns]
```

I think we can fix this after the RC.
@jreback OK to move off 1.0? You added it back in July.
This looks fixed on master. Could use a test

```
In [1]: In [43]: df = pd.DataFrame({"A": pd.to_timedelta([0, 1, None]), "B": pd.
   ...: array([1, 2, None], dtype='Int32')})
   ...:
   ...: In [44]: df.sum()
Out[1]:
A    0 days 00:00:00.000000001
B                            3
dtype: object
```
take

Commit message: TST: GH 27185 Adding test to check df with timedelta and int obj sums as expected

Patch to review:
diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py
index 9d33e52709bd2..cd82e00838b16 100644
--- a/pandas/tests/reductions/test_reductions.py
+++ b/pandas/tests/reductions/test_reductions.py
@@ -198,6 +198,19 @@ def test_numpy_reduction_with_tz_aware_dtype(self, tz_aware_fixture, func):
         result = getattr(np, func)(expected, expected)
         tm.assert_series_equal(result, expected)
 
+    def test_nan_int_timedelta_sum(self):
+        # GH 27185
+        df = DataFrame(
+            {
+                "A": Series([1, 2, None], dtype="timedelta64[ns]"),
+                "B": Series([1, 2, None], dtype="Int64"),
+            }
+        )
+
+        result = df.sum()
+        assert result["A"] == Timedelta(3, "ns")
+        assert result["B"] == 3
+
 
 class TestIndexReductions:
     # Note: the name TestIndexReductions indicates these tests


Code review comments:
diff_hunk: @@ -198,6 +198,19 @@ def test_numpy_reduction_with_tz_aware_dtype(self, tz_aware_fixture, func):
         result = getattr(np, func)(expected, expected)
         tm.assert_series_equal(result, expected)
 
+    def test_nan_int_timedelta_sum(self):
+        # GH 27185
+        df = DataFrame(
+            {
+                "A": Series([1, 2, None], dtype="timedelta64[ns]"),
+                "B": Series([1, 2, None], dtype="Int64"),
+            }
+        )
+
+        result = df.sum()
+        assert result["A"] == Timedelta(3, "ns")
path: pandas/tests/reductions/test_reductions.py
text: @user1:
Please check the whole result object 

@author:
Pushed new commit w/ the changes requested - let me know what you think!

@author:
hey @user1 just checking in to see if there's anything I should update in the PR?

diff_hunk: @@ -198,6 +198,19 @@ def test_numpy_reduction_with_tz_aware_dtype(self, tz_aware_fixture, func):
         result = getattr(np, func)(expected, expected)
         tm.assert_series_equal(result, expected)
 
+    def test_nan_int_timedelta_sum(self):
+        # GH 27185
+        df = DataFrame(
+            {
+                "A": Series([1, 2, None], dtype="timedelta64[ns]"),
+                "B": Series([1, 2, None], dtype="Int64"),
path: pandas/tests/reductions/test_reductions.py
text: @user1:
Could you use NaT and NA instead of None?