Skip to content

Commit d988d11

Browse files
committed
ENH: partial date slicing with DatetimeIndex, close #1044
1 parent 9385b62 commit d988d11

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

pandas/core/index.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,10 +1510,8 @@ def snap(self, freq='S'):
15101510
s = t1
15111511
snapped[i] = np.datetime64(s)
15121512

1513-
dti = DatetimeIndex(snapped) # we know it conforms; this skips check
1514-
dti.offset = freq
1515-
1516-
return dti
1513+
# we know it conforms; skip check
1514+
return DatetimeIndex(snapped, freq=freq, verify_integrity=False)
15171515

15181516
def shift(self, n, freq=None):
15191517
"""
@@ -1739,16 +1737,39 @@ def get_loc(self, key):
17391737
return self._engine.get_loc(key)
17401738
except KeyError:
17411739
try:
1742-
asdt, parsed, reso = datetools.parse_time_string(key)
1743-
key = asdt
1744-
loc = self._partial_date_slice(reso, parsed)
1745-
return loc
1746-
except TypeError:
1740+
return self._get_string_slice(key)
1741+
except (TypeError, KeyError):
17471742
pass
1743+
1744+
return self._engine.get_loc(to_timestamp(key))
1745+
1746+
def _get_string_slice(self, key):
1747+
asdt, parsed, reso = datetools.parse_time_string(key)
1748+
key = asdt
1749+
loc = self._partial_date_slice(reso, parsed)
1750+
return loc
1751+
1752+
def slice_locs(self, start=None, end=None):
1753+
"""
1754+
Index.slice_locs, customized to handle partial ISO-8601 string slicing
1755+
"""
1756+
if isinstance(start, basestring) or isinstance(end, basestring):
1757+
try:
1758+
if start:
1759+
start_loc = self._get_string_slice(start).start
1760+
else:
1761+
start_loc = 0
1762+
1763+
if end:
1764+
end_loc = self._get_string_slice(end).stop
1765+
else:
1766+
end_loc = len(self)
1767+
1768+
return start_loc, end_loc
17481769
except KeyError:
17491770
pass
17501771

1751-
return self._engine.get_loc(to_timestamp(key))
1772+
return Index.slice_locs(self, start, end)
17521773

17531774
def __getitem__(self, key):
17541775
"""Override numpy.ndarray's __getitem__ method to work as desired"""

pandas/tests/test_timeseries.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,13 +829,28 @@ def test_slice_quarter(self):
829829

830830
def test_slice_month(self):
831831
dti = DatetimeIndex(freq='D', start=datetime(2005,1,1), periods=500)
832-
833832
s = Series(np.arange(len(dti)), index=dti)
834833
self.assertEquals(len(s['2005-11']), 30)
835834

836835
df = DataFrame(np.random.rand(len(dti), 5), index=dti)
837836
self.assertEquals(len(df.ix['2005-11']), 30)
838837

838+
def test_partial_slice(self):
839+
rng = DatetimeIndex(freq='D', start=datetime(2005,1,1), periods=500)
840+
s = Series(np.arange(len(rng)), index=rng)
841+
842+
result = s['2005-05':'2006-02']
843+
expected = s['20050501':'20060228']
844+
assert_series_equal(result, expected)
845+
846+
result = s['2005-05':]
847+
expected = s['20050501':]
848+
assert_series_equal(result, expected)
849+
850+
result = s[:'2006-02']
851+
expected = s[:'20060228']
852+
assert_series_equal(result, expected)
853+
839854
def test_datetimeindex_constructor(self):
840855
arr = ['1/1/2005', '1/2/2005', 'Jn 3, 2005', '2005-01-04']
841856
self.assertRaises(Exception, DatetimeIndex, arr)

0 commit comments

Comments
 (0)