Skip to content

Commit 1584265

Browse files
committed
BUG: TimedelaIndex raising ValueError when boolean indexing (#14946)
1 parent 64d7670 commit 1584265

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ Bug Fixes
378378

379379
- Bug in ``Index`` power operations with reversed operands (:issue:`14973`)
380380
- Bug in ``TimedeltaIndex`` addition where overflow was being allowed without error (:issue:`14816`)
381+
- Bug in ``TimedeltaIndex`` raising a ``ValueError`` when boolean indexing with ``loc`` (:issue:`14946`)
381382
- Bug in ``astype()`` where ``inf`` values were incorrectly converted to integers. Now raises error now with ``astype()`` for Series and DataFrames (:issue:`14265`)
382383
- Bug in ``DataFrame(..).apply(to_numeric)`` when values are of type decimal.Decimal. (:issue:`14827`)
383384
- Bug in ``describe()`` when passing a numpy array which does not contain the median to the ``percentiles`` keyword argument (:issue:`14908`)

pandas/tseries/tdi.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
_ensure_int64)
1515
from pandas.types.missing import isnull
1616
from pandas.types.generic import ABCSeries
17-
from pandas.core.common import _maybe_box, _values_from_object
17+
from pandas.core.common import _maybe_box, _values_from_object, is_bool_indexer
1818

1919
from pandas.core.index import Index, Int64Index
2020
import pandas.compat as compat
@@ -673,6 +673,9 @@ def get_loc(self, key, method=None, tolerance=None):
673673
loc : int
674674
"""
675675

676+
if is_bool_indexer(key):
677+
raise TypeError
678+
676679
if isnull(key):
677680
key = tslib.NaT
678681

pandas/tseries/tests/test_timedeltas.py

+16
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,22 @@ def test_add_overflow(self):
20522052
to_timedelta(['7 seconds', pd.NaT, '4 hours']))
20532053
tm.assert_index_equal(result, exp)
20542054

2055+
def test_boolean_indexing(self):
2056+
# GH 14946
2057+
df = pd.DataFrame({'x': range(10)})
2058+
df.index = pd.to_timedelta(range(10), unit='s')
2059+
conditions = [df['x'] > 3, df['x'] == 3, df['x'] < 3]
2060+
expected_data = [[0, 1, 2, 3, 10, 10, 10, 10, 10, 10],
2061+
[0, 1, 2, 10, 4, 5, 6, 7, 8, 9],
2062+
[10, 10, 10, 3, 4, 5, 6, 7, 8, 9]]
2063+
for cond, data in zip(conditions, expected_data):
2064+
result = df.copy()
2065+
result.loc[cond, 'x'] = 10
2066+
expected = pd.DataFrame(data,
2067+
index=pd.to_timedelta(range(10), unit='s'),
2068+
columns=['x'])
2069+
assert_frame_equal(expected, result)
2070+
20552071

20562072
if __name__ == '__main__':
20572073
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)