Skip to content

Commit 6c255d0

Browse files
committed
API: make min/max on empty datetime df consistent with datetime series (pandas-dev#33704)
1 parent 1940fce commit 6c255d0

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

pandas/core/nanops.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,7 @@ def _na_for_min_count(
384384
else:
385385
assert axis is not None # assertion to make mypy happy
386386
result_shape = values.shape[:axis] + values.shape[axis + 1 :]
387-
result = np.empty(result_shape, dtype=values.dtype)
388-
result.fill(fill_value)
387+
result = np.full(result_shape, fill_value)
389388
return result
390389

391390

pandas/tests/arithmetic/test_datetime64.py

+15
Original file line numberDiff line numberDiff line change
@@ -2471,3 +2471,18 @@ def test_dt64arr_addsub_object_dtype_2d():
24712471
assert result2.shape == (4, 1)
24722472
assert result2.freq is None
24732473
assert (result2.asi8 == 0).all()
2474+
2475+
2476+
def test_sum_empty_df_series():
2477+
# Calling the following defined sum function returned an error for dataframes but
2478+
# returned NaT for series. # Check that the API is consistent in this sense when
2479+
# operating on empty Series/DataFrames. See GH:33704 for more information
2480+
df = pd.DataFrame(dict(x=pd.to_datetime([])))
2481+
series = pd.Series(pd.to_datetime([]))
2482+
assert (df.min().x is NaT) == (series.min() is NaT)
2483+
assert (df.max().x is NaT) == (series.max() is NaT)
2484+
2485+
df = pd.DataFrame(dict(x=[np.nan]))
2486+
series = pd.Series([np.nan])
2487+
assert np.isnan(df.min().x) == np.isnan(series.min())
2488+
assert np.isnan(df.max().x) == np.isnan(series.max())

0 commit comments

Comments
 (0)