From a23db0c458be6288a34d4dceaa4e79d7d0bacc0e Mon Sep 17 00:00:00 2001 From: jreback Date: Wed, 8 May 2013 13:24:22 -0400 Subject: [PATCH] BUG: Fixed bug in selecting month/quarter/year from a series would not select the time element on the last day (GH3546_) --- RELEASE.rst | 3 +++ pandas/tseries/index.py | 6 +++--- pandas/tseries/tests/test_timeseries.py | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/RELEASE.rst b/RELEASE.rst index 69cfd1eb99d7e..487c18cdb679b 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -79,6 +79,8 @@ pandas 0.11.1 - Fix to_csv to handle non-unique columns (GH3495_) - Fixed bug in groupby with empty series referencing a variable before assignment. (GH3510_) - Fixed bug in mixed-frame assignment with aligned series (GH3492_) + - Fixed bug in selecting month/quarter/year from a series would not select the time element + on the last day (GH3546_) .. _GH3164: https://github.com/pydata/pandas/issues/3164 .. _GH2786: https://github.com/pydata/pandas/issues/2786 @@ -100,6 +102,7 @@ pandas 0.11.1 .. _GH3457: https://github.com/pydata/pandas/issues/3457 .. _GH3477: https://github.com/pydata/pandas/issues/3457 .. _GH3461: https://github.com/pydata/pandas/issues/3461 +.. _GH3546: https://github.com/pydata/pandas/issues/3546 .. _GH3468: https://github.com/pydata/pandas/issues/3468 .. _GH3448: https://github.com/pydata/pandas/issues/3448 .. _GH3449: https://github.com/pydata/pandas/issues/3449 diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py index d9625a3d5e549..6bccf323f8654 100644 --- a/pandas/tseries/index.py +++ b/pandas/tseries/index.py @@ -1075,16 +1075,16 @@ def _partial_date_slice(self, reso, parsed, use_lhs=True, use_rhs=True): if reso == 'year': t1 = Timestamp(datetime(parsed.year, 1, 1), tz=self.tz) - t2 = Timestamp(datetime(parsed.year, 12, 31), tz=self.tz) + t2 = Timestamp(datetime(parsed.year, 12, 31, 23, 59, 59, 999999), tz=self.tz) elif reso == 'month': d = tslib.monthrange(parsed.year, parsed.month)[1] t1 = Timestamp(datetime(parsed.year, parsed.month, 1), tz=self.tz) - t2 = Timestamp(datetime(parsed.year, parsed.month, d), tz=self.tz) + t2 = Timestamp(datetime(parsed.year, parsed.month, d, 23, 59, 59, 999999), tz=self.tz) elif reso == 'quarter': qe = (((parsed.month - 1) + 2) % 12) + 1 # two months ahead d = tslib.monthrange(parsed.year, qe)[1] # at end of month t1 = Timestamp(datetime(parsed.year, parsed.month, 1), tz=self.tz) - t2 = Timestamp(datetime(parsed.year, qe, d), tz=self.tz) + t2 = Timestamp(datetime(parsed.year, qe, d, 23, 59, 59, 999999), tz=self.tz) elif (reso == 'day' and (self._resolution < Resolution.RESO_DAY or not is_monotonic)): st = datetime(parsed.year, parsed.month, parsed.day) t1 = Timestamp(st, tz=self.tz) diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index c83d4ba131a42..e52d9c9c8b777 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -236,6 +236,22 @@ def test_indexing(self): result = df['2001']['A'] assert_series_equal(expected,result) + # GH3546 (not including times on the last day) + idx = date_range(start='2013-05-31 00:00', end='2013-05-31 23:00', freq='H') + ts = Series(range(len(idx)), index=idx) + expected = ts['2013-05'] + assert_series_equal(expected,ts) + + idx = date_range(start='2013-05-31 00:00', end='2013-05-31 23:59', freq='S') + ts = Series(range(len(idx)), index=idx) + expected = ts['2013-05'] + assert_series_equal(expected,ts) + + idx = [ Timestamp('2013-05-31 00:00'), Timestamp(datetime(2013,5,31,23,59,59,999999))] + ts = Series(range(len(idx)), index=idx) + expected = ts['2013'] + assert_series_equal(expected,ts) + def assert_range_equal(left, right): assert(left.equals(right)) assert(left.freq == right.freq)