Skip to content

Commit b6c48bb

Browse files
author
Jean-Mathieu Deschenes
committed
BUG: ValueError when setting values using a Boolean indexing on TimeDeltaIndex (pandas-dev#14946)
1 parent f79bc7a commit b6c48bb

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

pandas/tests/indexing/test_indexing.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from pandas.core.common import PerformanceWarning, UnsortedIndexError
2727

2828
import pandas.util.testing as tm
29-
from pandas import date_range
29+
from pandas import (date_range, to_timedelta)
3030

3131

3232
_verbose = False
@@ -100,7 +100,7 @@ class TestIndexing(tm.TestCase):
100100
_multiprocess_can_split_ = True
101101

102102
_objs = set(['series', 'frame', 'panel'])
103-
_typs = set(['ints', 'labels', 'mixed', 'ts', 'floats', 'empty', 'ts_rev'])
103+
_typs = set(['ints', 'labels', 'mixed', 'ts', 'floats', 'empty', 'ts_rev', 'dt'])
104104

105105
def setUp(self):
106106

@@ -150,6 +150,13 @@ def setUp(self):
150150
self.series_empty = Series({})
151151
self.panel_empty = Panel({})
152152

153+
self.series_dt = Series(np.random.randn(4),
154+
index=to_timedelta(range(4), unit='s'))
155+
self.frame_dt = DataFrame(np.random.randn(4, 4),
156+
index=to_timedelta(range(4), unit='s'))
157+
self.panel_dt = Panel(np.random.randn(4, 4, 4),
158+
items=to_timedelta(range(4), unit='s'))
159+
153160
# form agglomerates
154161
for o in self._objs:
155162

@@ -723,6 +730,15 @@ def test_loc_setitem_slice(self):
723730
dtype='uint64')
724731
tm.assert_frame_equal(df2, expected)
725732

733+
# GH 14946
734+
# Setting time_delta
735+
736+
df = DataFrame({'x': range(5)}, index=to_timedelta(range(5)))
737+
test_series = df.copy()['x']
738+
test_series[test_series < 2] = 10
739+
df.loc[df['x'] < 2, 'x'] = 10
740+
tm.assert_series_equal(test_series, df['x'])
741+
726742
def test_ix_loc_setitem_consistency(self):
727743

728744
# GH 5771
@@ -1214,7 +1230,7 @@ def test_loc_getitem_bool(self):
12141230
# boolean indexers
12151231
b = [True, False, True, False]
12161232
self.check_result('bool', 'loc', b, 'ix', b,
1217-
typs=['ints', 'labels', 'mixed', 'ts', 'floats'])
1233+
typs=['ints', 'labels', 'mixed', 'ts', 'floats', 'dt'])
12181234
self.check_result('bool', 'loc', b, 'ix', b, typs=['empty'],
12191235
fails=KeyError)
12201236

pandas/tseries/tdi.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
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,
18+
_values_from_object,
19+
is_bool_indexer)
1820

1921
from pandas.core.index import Index, Int64Index
2022
import pandas.compat as compat
@@ -673,7 +675,8 @@ def get_loc(self, key, method=None, tolerance=None):
673675
-------
674676
loc : int
675677
"""
676-
678+
if is_bool_indexer(key):
679+
raise TypeError
677680
if isnull(key):
678681
key = tslib.NaT
679682

0 commit comments

Comments
 (0)