Skip to content

Commit fc589c6

Browse files
committed
Merge pull request #3931 from jreback/time_indexing
BUG: (GH3925) partial string selection with seconds resolution
2 parents b03df73 + ca215ae commit fc589c6

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

RELEASE.rst

+5
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ pandas 0.11.1
254254
in the ``to_replace`` argument wasn't working (GH3907_)
255255
- Fixed ``__truediv__`` in Python 2.7 with ``numexpr`` installed to actually do true division when dividing
256256
two integer arrays with at least 10000 cells total (GH3764_)
257+
- Indexing with a string with seconds resolution not selecting from a time index (GH3925_)
257258

258259
.. _GH3164: https://github.com/pydata/pandas/issues/3164
259260
.. _GH2786: https://github.com/pydata/pandas/issues/2786
@@ -355,9 +356,13 @@ pandas 0.11.1
355356
.. _GH3907: https://github.com/pydata/pandas/issues/3907
356357
.. _GH3911: https://github.com/pydata/pandas/issues/3911
357358
.. _GH3912: https://github.com/pydata/pandas/issues/3912
359+
<<<<<<< HEAD
358360
.. _GH3764: https://github.com/pydata/pandas/issues/3764
359361
.. _GH3888: https://github.com/pydata/pandas/issues/3888
360362

363+
=======
364+
.. _GH3925: https://github.com/pydata/pandas/issues/3925
365+
>>>>>>> BUG: (GH3925) Indexing with a string with seconds resolution not selecting from a time index
361366

362367
pandas 0.11.0
363368
=============

pandas/core/indexing.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# pylint: disable=W0223
22

3+
from datetime import datetime
34
from pandas.core.common import _asarray_tuplesafe
45
from pandas.core.index import Index, MultiIndex, _ensure_index
56
import pandas.core.common as com

pandas/tseries/index.py

+14
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,13 @@ def _partial_date_slice(self, reso, parsed, use_lhs=True, use_rhs=True):
11021102
t1 = Timestamp(st, tz=self.tz)
11031103
t2 = Timestamp(Timestamp(st + offsets.Minute(),
11041104
tz=self.tz).value - 1)
1105+
elif (reso == 'second' and (
1106+
self._resolution == Resolution.RESO_SEC or not is_monotonic)):
1107+
st = datetime(parsed.year, parsed.month, parsed.day,
1108+
hour=parsed.hour, minute=parsed.minute, second=parsed.second)
1109+
t1 = Timestamp(st, tz=self.tz)
1110+
t2 = Timestamp(Timestamp(st + offsets.Second(),
1111+
tz=self.tz).value - 1)
11051112
else:
11061113
raise KeyError
11071114

@@ -1110,9 +1117,16 @@ def _partial_date_slice(self, reso, parsed, use_lhs=True, use_rhs=True):
11101117

11111118
if is_monotonic:
11121119

1120+
# we are out of range
1121+
if len(stamps) and (
1122+
(use_lhs and t1.value < stamps[0] and t2.value < stamps[0]) or (
1123+
(use_rhs and t1.value > stamps[-1] and t2.value > stamps[-1]))):
1124+
raise KeyError
1125+
11131126
# a monotonic (sorted) series can be sliced
11141127
left = stamps.searchsorted(t1.value, side='left') if use_lhs else None
11151128
right = stamps.searchsorted(t2.value, side='right') if use_rhs else None
1129+
11161130
return slice(left, right)
11171131

11181132
lhs_mask = (stamps>=t1.value) if use_lhs else True

pandas/tseries/tests/test_timeseries.py

+9
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,15 @@ def test_indexing(self):
251251
expected = ts['2013']
252252
assert_series_equal(expected,ts)
253253

254+
# GH 3925, indexing with a seconds resolution string / datetime object
255+
df = DataFrame(randn(5,5),columns=['open','high','low','close','volume'],index=date_range('2012-01-02 18:01:00',periods=5,tz='US/Central',freq='s'))
256+
expected = df.loc[[df.index[2]]]
257+
result = df['2012-01-02 18:01:02']
258+
self.assert_(result == expected)
259+
260+
# this is a single date, so will raise
261+
self.assertRaises(KeyError, df.__getitem__, df.index[2],)
262+
254263
def assert_range_equal(left, right):
255264
assert(left.equals(right))
256265
assert(left.freq == right.freq)

0 commit comments

Comments
 (0)