Skip to content

BUG: DataFrame.std(skipna=False) with td64 dtype #37392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ Numeric
- Bug in :class:`DataFrame` arithmetic ops incorrectly accepting keyword arguments (:issue:`36843`)
- Bug in :class:`IntervalArray` comparisons with :class:`Series` not returning :class:`Series` (:issue:`36908`)
- Bug in :class:`DataFrame` allowing arithmetic operations with list of array-likes with undefined results. Behavior changed to raising ``ValueError`` (:issue:`36702`)
- Bug in :meth:`DataFrame.std`` with ``timedelta64`` dtype and ``skipna=False`` (:issue:`37392`)

Conversion
^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def _maybe_get_mask(
# Boolean data cannot contain nulls, so signal via mask being None
return None

if skipna:
if skipna or needs_i8_conversion(values.dtype):
mask = isna(values)

return mask
Expand Down
12 changes: 10 additions & 2 deletions pandas/tests/arrays/test_timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pandas as pd
import pandas._testing as tm
from pandas.core import nanops
from pandas.core.arrays import TimedeltaArray


Expand Down Expand Up @@ -288,12 +289,19 @@ def test_std(self):
assert isinstance(result, pd.Timedelta)
assert result == expected

result = nanops.nanstd(np.asarray(arr), skipna=True)
assert isinstance(result, pd.Timedelta)
assert result == expected

result = arr.std(skipna=False)
assert result is pd.NaT

result = tdi.std(skipna=False)
assert result is pd.NaT

result = nanops.nanstd(np.asarray(arr), skipna=False)
assert result is pd.NaT

def test_median(self):
tdi = pd.TimedeltaIndex(["0H", "3H", "NaT", "5H06m", "0H", "2H"])
arr = tdi.array
Expand All @@ -307,8 +315,8 @@ def test_median(self):
assert isinstance(result, pd.Timedelta)
assert result == expected

result = arr.std(skipna=False)
result = arr.median(skipna=False)
assert result is pd.NaT

result = tdi.std(skipna=False)
result = tdi.median(skipna=False)
assert result is pd.NaT
16 changes: 16 additions & 0 deletions pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,22 @@ def test_operators_timedelta64(self):
assert df["off1"].dtype == "timedelta64[ns]"
assert df["off2"].dtype == "timedelta64[ns]"

def test_std_timedelta64_skipna_false(self):
# GH#37392
tdi = pd.timedelta_range("1 Day", periods=10)
df = DataFrame({"A": tdi, "B": tdi})
df.iloc[-2, -1] = pd.NaT

result = df.std(skipna=False)
expected = Series(
[df["A"].std(), pd.NaT], index=["A", "B"], dtype="timedelta64[ns]"
)
tm.assert_series_equal(result, expected)

result = df.std(axis=1, skipna=False)
expected = Series([pd.Timedelta(0)] * 8 + [pd.NaT, pd.Timedelta(0)])
tm.assert_series_equal(result, expected)

def test_sum_corner(self):
empty_frame = DataFrame()

Expand Down