Skip to content

Commit b8ac04e

Browse files
committed
BUG: TimedelaIndex raising ValueError when boolean indexing (pandas-dev#14946)
Move tests
1 parent 0c8442c commit b8ac04e

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ Bug Fixes
380380

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

pandas/tests/indexing/test_indexing.py

+19
Original file line numberDiff line numberDiff line change
@@ -5498,6 +5498,25 @@ def test_none_coercion_mixed_dtypes(self):
54985498
tm.assert_frame_equal(start_dataframe, exp)
54995499

55005500

5501+
class TestTimedeltaIndexing(tm.TestCase):
5502+
5503+
def test_boolean_indexing(self):
5504+
# GH 14946
5505+
df = pd.DataFrame({'x': range(10)})
5506+
df.index = pd.to_timedelta(range(10), unit='s')
5507+
conditions = [df['x'] > 3, df['x'] == 3, df['x'] < 3]
5508+
expected_data = [[0, 1, 2, 3, 10, 10, 10, 10, 10, 10],
5509+
[0, 1, 2, 10, 4, 5, 6, 7, 8, 9],
5510+
[10, 10, 10, 3, 4, 5, 6, 7, 8, 9]]
5511+
for cond, data in zip(conditions, expected_data):
5512+
result = df.copy()
5513+
result.loc[cond, 'x'] = 10
5514+
expected = pd.DataFrame(data,
5515+
index=pd.to_timedelta(range(10), unit='s'),
5516+
columns=['x'])
5517+
tm.assert_frame_equal(expected, result)
5518+
5519+
55015520
if __name__ == '__main__':
55025521
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
55035522
exit=False)

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

0 commit comments

Comments
 (0)