Skip to content

Commit 9f635cd

Browse files
yui-knkjreback
authored andcommitted
BUG: Cast a key to NaT before get loc from Index
closes #13603 Author: yui-knk <[email protected]> Closes #13687 from yui-knk/fix_13603 and squashes the following commits: 0960395 [yui-knk] BUG: Cast a key to NaT before get loc from Index
1 parent 5a52171 commit 9f635cd

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ Bug Fixes
584584
- Bug in ``pd.read_csv()`` with ``engine=='c'`` in which fields were not properly cast to float when quoting was specified as non-numeric (:issue:`13411`)
585585
- Bug in ``pd.pivot_table()`` where ``margins_name`` is ignored when ``aggfunc`` is a list (:issue:`13354`)
586586
- Bug in ``pd.Series.str.zfill``, ``center``, ``ljust``, ``rjust``, and ``pad`` when passing non-integers, did not raise ``TypeError`` (:issue:`13598`)
587+
- Bug in checking for any null objects in a ``TimedeltaIndex``, which always returned ``True`` (:issue:`13603`)
587588

588589

589590
- Bug in ``Series`` arithmetic raises ``TypeError`` if it contains datetime-like as ``object`` dtype (:issue:`13043`)

pandas/tseries/tdi.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,10 @@ def get_loc(self, key, method=None, tolerance=None):
697697
-------
698698
loc : int
699699
"""
700+
701+
if isnull(key):
702+
key = tslib.NaT
703+
700704
if tolerance is not None:
701705
# try converting tolerance now, so errors don't get swallowed by
702706
# the try/except clauses below
@@ -754,7 +758,7 @@ def _maybe_cast_slice_bound(self, label, side, kind):
754758
def _get_string_slice(self, key, use_lhs=True, use_rhs=True):
755759
freq = getattr(self, 'freqstr',
756760
getattr(self, 'inferred_freq', None))
757-
if is_integer(key) or is_float(key):
761+
if is_integer(key) or is_float(key) or key is tslib.NaT:
758762
self._invalid_indexer('slice', key)
759763
loc = self._partial_td_slice(key, freq, use_lhs=use_lhs,
760764
use_rhs=use_rhs)

pandas/tseries/tests/test_timedeltas.py

+19
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ class TestTimedeltas(tm.TestCase):
3030
def setUp(self):
3131
pass
3232

33+
def test_get_loc_nat(self):
34+
tidx = TimedeltaIndex(['1 days 01:00:00', 'NaT', '2 days 01:00:00'])
35+
36+
self.assertEqual(tidx.get_loc(pd.NaT), 1)
37+
self.assertEqual(tidx.get_loc(None), 1)
38+
self.assertEqual(tidx.get_loc(float('nan')), 1)
39+
self.assertEqual(tidx.get_loc(np.nan), 1)
40+
41+
def test_contains(self):
42+
# Checking for any NaT-like objects
43+
# GH 13603
44+
td = to_timedelta(range(5), unit='d') + pd.offsets.Hour(1)
45+
for v in [pd.NaT, None, float('nan'), np.nan]:
46+
self.assertFalse((v in td))
47+
48+
td = to_timedelta([pd.NaT])
49+
for v in [pd.NaT, None, float('nan'), np.nan]:
50+
self.assertTrue((v in td))
51+
3352
def test_construction(self):
3453

3554
expected = np.timedelta64(10, 'D').astype('m8[ns]').view('i8')

0 commit comments

Comments
 (0)