From b8ac04ebc84086b6dbe59f02384a9fd00a2b5ecf Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Tue, 24 Jan 2017 22:13:25 -0800 Subject: [PATCH] BUG: TimedelaIndex raising ValueError when boolean indexing (#14946) Move tests --- doc/source/whatsnew/v0.20.0.txt | 1 + pandas/tests/indexing/test_indexing.py | 19 +++++++++++++++++++ pandas/tseries/tdi.py | 5 ++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 86fa919fec304..a7d78dff44d71 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -380,6 +380,7 @@ Bug Fixes - Bug in ``Index`` power operations with reversed operands (:issue:`14973`) - Bug in ``TimedeltaIndex`` addition where overflow was being allowed without error (:issue:`14816`) +- Bug in ``TimedeltaIndex`` raising a ``ValueError`` when boolean indexing with ``loc`` (:issue:`14946`) - Bug in ``astype()`` where ``inf`` values were incorrectly converted to integers. Now raises error now with ``astype()`` for Series and DataFrames (:issue:`14265`) - Bug in ``DataFrame(..).apply(to_numeric)`` when values are of type decimal.Decimal. (:issue:`14827`) - Bug in ``describe()`` when passing a numpy array which does not contain the median to the ``percentiles`` keyword argument (:issue:`14908`) diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index e44471efb9871..a6eb3c8a891e7 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -5498,6 +5498,25 @@ def test_none_coercion_mixed_dtypes(self): tm.assert_frame_equal(start_dataframe, exp) +class TestTimedeltaIndexing(tm.TestCase): + + def test_boolean_indexing(self): + # GH 14946 + df = pd.DataFrame({'x': range(10)}) + df.index = pd.to_timedelta(range(10), unit='s') + conditions = [df['x'] > 3, df['x'] == 3, df['x'] < 3] + expected_data = [[0, 1, 2, 3, 10, 10, 10, 10, 10, 10], + [0, 1, 2, 10, 4, 5, 6, 7, 8, 9], + [10, 10, 10, 3, 4, 5, 6, 7, 8, 9]] + for cond, data in zip(conditions, expected_data): + result = df.copy() + result.loc[cond, 'x'] = 10 + expected = pd.DataFrame(data, + index=pd.to_timedelta(range(10), unit='s'), + columns=['x']) + tm.assert_frame_equal(expected, result) + + if __name__ == '__main__': nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], exit=False) diff --git a/pandas/tseries/tdi.py b/pandas/tseries/tdi.py index 2e050445f788a..c62e3fc40d4af 100644 --- a/pandas/tseries/tdi.py +++ b/pandas/tseries/tdi.py @@ -14,7 +14,7 @@ _ensure_int64) from pandas.types.missing import isnull from pandas.types.generic import ABCSeries -from pandas.core.common import _maybe_box, _values_from_object +from pandas.core.common import _maybe_box, _values_from_object, is_bool_indexer from pandas.core.index import Index, Int64Index import pandas.compat as compat @@ -673,6 +673,9 @@ def get_loc(self, key, method=None, tolerance=None): loc : int """ + if is_bool_indexer(key): + raise TypeError + if isnull(key): key = tslib.NaT