Skip to content

Commit 271fc95

Browse files
committed
Merge pull request #3136 from jreback/GH2437_2
ENH: GH2437 added selection to an unordered timeseries
2 parents d5f1686 + 3a173f1 commit 271fc95

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

RELEASE.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ pandas 0.11.0
203203
- series.plot(kind='bar') now respects pylab color schem (GH3115_)
204204
- Fixed bug in reshape if not passed correct input, now raises TypeError (GH2719_)
205205
- Fix NameError issue on RESO_US (GH2787_)
206+
- Allow selection in an *unordered* timeseries to work similary
207+
to an *ordered* timeseries (GH2437_).
206208

207209
.. _GH2758: https://github.com/pydata/pandas/issues/2758
208210
.. _GH2809: https://github.com/pydata/pandas/issues/2809
@@ -229,6 +231,7 @@ pandas 0.11.0
229231
.. _GH2776: https://github.com/pydata/pandas/issues/2776
230232
.. _GH2778: https://github.com/pydata/pandas/issues/2778
231233
.. _GH2787: https://github.com/pydata/pandas/issues/2787
234+
.. _GH2437: https://github.com/pydata/pandas/issues/2437
232235
.. _GH2793: https://github.com/pydata/pandas/issues/2793
233236
.. _GH2795: https://github.com/pydata/pandas/issues/2795
234237
.. _GH2819: https://github.com/pydata/pandas/issues/2819

doc/source/v0.11.0.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ Enhancements
243243
- In ``HDFStore``, new keywords ``iterator=boolean``, and ``chunksize=number_in_a_chunk`` are
244244
provided to support iteration on ``select`` and ``select_as_multiple`` (GH3076_)
245245

246+
- You can now select timestamps from an *unordered* timeseries similarly to an *ordered* timeseries (GH2437_)
247+
246248
- ``Squeeze`` to possibly remove length 1 dimensions from an object.
247249

248250
.. ipython:: python
@@ -293,6 +295,7 @@ See the `full release notes
293295
<https://github.com/pydata/pandas/blob/master/RELEASE.rst>`__ or issue tracker
294296
on GitHub for a complete list.
295297

298+
.. _GH2437: https://github.com/pydata/pandas/issues/2437
296299
.. _GH2809: https://github.com/pydata/pandas/issues/2809
297300
.. _GH2810: https://github.com/pydata/pandas/issues/2810
298301
.. _GH2837: https://github.com/pydata/pandas/issues/2837

pandas/tseries/index.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,9 +1042,6 @@ def intersection(self, other):
10421042
return self._view_like(left_chunk)
10431043

10441044
def _partial_date_slice(self, reso, parsed):
1045-
if not self.is_monotonic:
1046-
raise TimeSeriesError('Partial indexing only valid for ordered '
1047-
'time series.')
10481045

10491046
if reso == 'year':
10501047
t1 = Timestamp(datetime(parsed.year, 1, 1), tz=self.tz)
@@ -1079,11 +1076,19 @@ def _partial_date_slice(self, reso, parsed):
10791076
tz=self.tz).value - 1)
10801077
else:
10811078
raise KeyError
1079+
10821080

10831081
stamps = self.asi8
1084-
left = stamps.searchsorted(t1.value, side='left')
1085-
right = stamps.searchsorted(t2.value, side='right')
1086-
return slice(left, right)
1082+
1083+
if self.is_monotonic:
1084+
1085+
# a monotonic (sorted) series can be sliced
1086+
left = stamps.searchsorted(t1.value, side='left')
1087+
right = stamps.searchsorted(t2.value, side='right')
1088+
return slice(left, right)
1089+
1090+
# try to find a the dates
1091+
return ((stamps>=t1.value) & (stamps<=t2.value)).nonzero()[0]
10871092

10881093
def _possibly_promote(self, other):
10891094
if other.inferred_type == 'date':

pandas/tseries/tests/test_timeseries.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import pandas.core.datetools as datetools
1919
import pandas.tseries.offsets as offsets
2020
import pandas.tseries.frequencies as fmod
21+
from pandas.tseries.index import TimeSeriesError
2122
import pandas as pd
2223

2324
from pandas.util.testing import assert_series_equal, assert_almost_equal
@@ -168,6 +169,32 @@ def test_indexing_over_size_cutoff(self):
168169
finally:
169170
_index._SIZE_CUTOFF = old_cutoff
170171

172+
def test_indexing_unordered(self):
173+
174+
# GH 2437
175+
from pandas import concat
176+
rng = date_range(start='2011-01-01', end='2011-01-15')
177+
ts = Series(randn(len(rng)), index=rng)
178+
ts2 = concat([ts[0:4],ts[-4:],ts[4:-4]])
179+
180+
for t in ts.index:
181+
s = str(t)
182+
expected = ts[t]
183+
result = ts2[t]
184+
self.assertTrue(expected == result)
185+
186+
result = ts2['2011'].sort_index()
187+
expected = ts['2011']
188+
assert_series_equal(result,expected)
189+
190+
# diff freq
191+
rng = date_range(datetime(2005, 1, 1), periods=20, freq='M')
192+
ts = Series(np.arange(len(rng)), index=rng)
193+
ts = ts.take(np.random.permutation(20))
194+
195+
result = ts['2005']
196+
for t in result.index:
197+
self.assertTrue(t.year == 2005)
171198

172199
def assert_range_equal(left, right):
173200
assert(left.equals(right))
@@ -2017,13 +2044,6 @@ def test_partial_slice_minutely(self):
20172044
self.assert_(s['2005-1-1 23:59:00'] == s.ix[0])
20182045
self.assertRaises(Exception, s.__getitem__, '2004-12-31 00:00:00')
20192046

2020-
def test_partial_not_monotonic(self):
2021-
rng = date_range(datetime(2005, 1, 1), periods=20, freq='M')
2022-
ts = Series(np.arange(len(rng)), index=rng)
2023-
ts = ts.take(np.random.permutation(20))
2024-
2025-
self.assertRaises(Exception, ts.__getitem__, '2005')
2026-
20272047
def test_date_range_normalize(self):
20282048
snap = datetime.today()
20292049
n = 50

0 commit comments

Comments
 (0)