Skip to content

Commit f447106

Browse files
jbrockmendelMateusz Górski
authored and
Mateusz Górski
committed
BUG: Series[timdelta64].var() should _not_ work (pandas-dev#28289)
1 parent d899041 commit f447106

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

doc/source/whatsnew/v1.0.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ Datetimelike
304304
- Bug in :class:`DataFrame` arithmetic operations when operating with a :class:`Series` with dtype `'timedelta64[ns]'` (:issue:`28049`)
305305
- Bug in :func:`pandas.core.groupby.generic.SeriesGroupBy.apply` raising ``ValueError`` when a column in the original DataFrame is a datetime and the column labels are not standard integers (:issue:`28247`)
306306
- Bug in :func:`pandas._config.localization.get_locales` where the ``locales -a`` encodes the locales list as windows-1252 (:issue:`23638`, :issue:`24760`, :issue:`27368`)
307+
- Bug in :meth:`Series.var` failing to raise ``TypeError`` when called with ``timedelta64[ns]`` dtype (:issue:`28289`)
308+
-
307309

308310
Timedelta
309311
^^^^^^^^^

pandas/core/nanops.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -705,11 +705,14 @@ def nanstd(values, axis=None, skipna=True, ddof=1, mask=None):
705705
>>> nanops.nanstd(s)
706706
1.0
707707
"""
708+
orig_dtype = values.dtype
709+
values, mask, dtype, dtype_max, fill_value = _get_values(values, skipna, mask=mask)
710+
708711
result = np.sqrt(nanvar(values, axis=axis, skipna=skipna, ddof=ddof, mask=mask))
709-
return _wrap_results(result, values.dtype)
712+
return _wrap_results(result, orig_dtype)
710713

711714

712-
@disallow("M8")
715+
@disallow("M8", "m8")
713716
@bottleneck_switch(ddof=1)
714717
def nanvar(values, axis=None, skipna=True, ddof=1, mask=None):
715718
"""

pandas/core/series.py

+1
Original file line numberDiff line numberDiff line change
@@ -3988,6 +3988,7 @@ def _reduce(
39883988
If we have an ndarray as a value, then simply perform the operation,
39893989
otherwise delegate to the object.
39903990
"""
3991+
39913992
delegate = self._values
39923993

39933994
if axis is not None:

pandas/tests/reductions/test_reductions.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,14 @@ def test_timedelta_ops(self):
300300
assert result[0] == expected
301301

302302
# invalid ops
303-
for op in ["skew", "kurt", "sem", "prod"]:
303+
for op in ["skew", "kurt", "sem", "prod", "var"]:
304304
msg = "reduction operation '{}' not allowed for this dtype"
305305
with pytest.raises(TypeError, match=msg.format(op)):
306306
getattr(td, op)()
307307

308+
with pytest.raises(TypeError, match=msg.format(op)):
309+
getattr(td.to_frame(), op)(numeric_only=False)
310+
308311
# GH#10040
309312
# make sure NaT is properly handled by median()
310313
s = Series([Timestamp("2015-02-03"), Timestamp("2015-02-07")])
@@ -636,8 +639,13 @@ def test_ops_consistency_on_empty(self, method):
636639
assert pd.isna(result)
637640

638641
# timedelta64[ns]
639-
result = getattr(Series(dtype="m8[ns]"), method)()
640-
assert result is pd.NaT
642+
tdser = Series([], dtype="m8[ns]")
643+
if method == "var":
644+
with pytest.raises(TypeError, match="operation 'var' not allowed"):
645+
getattr(tdser, method)()
646+
else:
647+
result = getattr(tdser, method)()
648+
assert result is pd.NaT
641649

642650
def test_nansum_buglet(self):
643651
ser = Series([1.0, np.nan], index=[0, 1])

0 commit comments

Comments
 (0)