From 43d1fcdaf60e917ff4d8510ddd8f32ec7653e075 Mon Sep 17 00:00:00 2001 From: Andrew Rosenfeld Date: Fri, 26 Feb 2016 18:34:08 +0000 Subject: [PATCH] ENH: add missing methods to .dt for Period, resolves #8848 --- doc/source/whatsnew/v0.18.0.txt | 1 + pandas/tests/series/test_datetime_values.py | 4 ++-- pandas/tseries/period.py | 11 ++++++++++- pandas/tseries/tests/test_period.py | 10 ++++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index 8a48314de5f77..6e9574b6ee4f7 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -431,6 +431,7 @@ Other enhancements - ``pivot_table()`` now accepts most iterables for the ``values`` parameter (:issue:`12017`) - Added Google ``BigQuery`` service account authentication support, which enables authentication on remote servers. (:issue:`11881`). For further details see :ref:`here ` - ``HDFStore`` is now iterable: ``for k in store`` is equivalent to ``for k in store.keys()`` (:issue:`12221`). +- Add missing methods/fields to .dt for Period (:issue:`8848`) - The entire codebase has been ``PEP``-ified (:issue:`12096`) .. _whatsnew_0180.api_breaking: diff --git a/pandas/tests/series/test_datetime_values.py b/pandas/tests/series/test_datetime_values.py index c6593d403ffcc..cd1fd92f241cf 100644 --- a/pandas/tests/series/test_datetime_values.py +++ b/pandas/tests/series/test_datetime_values.py @@ -32,8 +32,8 @@ def test_dt_namespace_accessor(self): 'weekofyear', 'week', 'dayofweek', 'weekday', 'dayofyear', 'quarter', 'freq', 'days_in_month', 'daysinmonth'] - ok_for_period = ok_for_base + ['qyear'] - ok_for_period_methods = ['strftime'] + ok_for_period = ok_for_base + ['qyear', 'start_time', 'end_time'] + ok_for_period_methods = ['strftime', 'to_timestamp', 'asfreq'] ok_for_dt = ok_for_base + ['date', 'time', 'microsecond', 'nanosecond', 'is_month_start', 'is_month_end', 'is_quarter_start', 'is_quarter_end', diff --git a/pandas/tseries/period.py b/pandas/tseries/period.py index a25bb525c9970..e14996517fa65 100644 --- a/pandas/tseries/period.py +++ b/pandas/tseries/period.py @@ -156,7 +156,8 @@ class PeriodIndex(DatelikeOps, DatetimeIndexOpsMixin, Int64Index): _datetimelike_ops = ['year', 'month', 'day', 'hour', 'minute', 'second', 'weekofyear', 'week', 'dayofweek', 'weekday', 'dayofyear', 'quarter', 'qyear', 'freq', - 'days_in_month', 'daysinmonth'] + 'days_in_month', 'daysinmonth', + 'to_timestamp', 'asfreq', 'start_time', 'end_time'] _is_numeric_dtype = False _infer_as_myclass = True @@ -498,6 +499,14 @@ def to_datetime(self, dayfirst=False): 'days_in_month', 11, "The number of days in the month") daysinmonth = days_in_month + @property + def start_time(self): + return self.to_timestamp(how='start') + + @property + def end_time(self): + return self.to_timestamp(how='end') + def _get_object_array(self): freq = self.freq return np.array([Period._from_ordinal(ordinal=x, freq=freq) diff --git a/pandas/tseries/tests/test_period.py b/pandas/tseries/tests/test_period.py index 8a876272dfdef..ee6f1a8b0e2db 100644 --- a/pandas/tseries/tests/test_period.py +++ b/pandas/tseries/tests/test_period.py @@ -2030,6 +2030,16 @@ def test_to_timestamp_pi_mult(self): ['2011-02-28', 'NaT', '2011-03-31'], name='idx') self.assert_index_equal(result, expected) + def test_start_time(self): + index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31') + expected_index = date_range('2016-01-01', end='2016-05-31', freq='MS') + self.assertTrue(index.start_time.equals(expected_index)) + + def test_end_time(self): + index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31') + expected_index = date_range('2016-01-01', end='2016-05-31', freq='M') + self.assertTrue(index.end_time.equals(expected_index)) + def test_as_frame_columns(self): rng = period_range('1/1/2000', periods=5) df = DataFrame(randn(10, 5), columns=rng)