Skip to content

Commit 43d1fcd

Browse files
ENH: add missing methods to .dt for Period, resolves pandas-dev#8848
1 parent fe584e7 commit 43d1fcd

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ Other enhancements
431431
- ``pivot_table()`` now accepts most iterables for the ``values`` parameter (:issue:`12017`)
432432
- Added Google ``BigQuery`` service account authentication support, which enables authentication on remote servers. (:issue:`11881`). For further details see :ref:`here <io.bigquery_authentication>`
433433
- ``HDFStore`` is now iterable: ``for k in store`` is equivalent to ``for k in store.keys()`` (:issue:`12221`).
434+
- Add missing methods/fields to .dt for Period (:issue:`8848`)
434435
- The entire codebase has been ``PEP``-ified (:issue:`12096`)
435436

436437
.. _whatsnew_0180.api_breaking:

pandas/tests/series/test_datetime_values.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def test_dt_namespace_accessor(self):
3232
'weekofyear', 'week', 'dayofweek', 'weekday',
3333
'dayofyear', 'quarter', 'freq', 'days_in_month',
3434
'daysinmonth']
35-
ok_for_period = ok_for_base + ['qyear']
36-
ok_for_period_methods = ['strftime']
35+
ok_for_period = ok_for_base + ['qyear', 'start_time', 'end_time']
36+
ok_for_period_methods = ['strftime', 'to_timestamp', 'asfreq']
3737
ok_for_dt = ok_for_base + ['date', 'time', 'microsecond', 'nanosecond',
3838
'is_month_start', 'is_month_end',
3939
'is_quarter_start', 'is_quarter_end',

pandas/tseries/period.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ class PeriodIndex(DatelikeOps, DatetimeIndexOpsMixin, Int64Index):
156156
_datetimelike_ops = ['year', 'month', 'day', 'hour', 'minute', 'second',
157157
'weekofyear', 'week', 'dayofweek', 'weekday',
158158
'dayofyear', 'quarter', 'qyear', 'freq',
159-
'days_in_month', 'daysinmonth']
159+
'days_in_month', 'daysinmonth',
160+
'to_timestamp', 'asfreq', 'start_time', 'end_time']
160161
_is_numeric_dtype = False
161162
_infer_as_myclass = True
162163

@@ -498,6 +499,14 @@ def to_datetime(self, dayfirst=False):
498499
'days_in_month', 11, "The number of days in the month")
499500
daysinmonth = days_in_month
500501

502+
@property
503+
def start_time(self):
504+
return self.to_timestamp(how='start')
505+
506+
@property
507+
def end_time(self):
508+
return self.to_timestamp(how='end')
509+
501510
def _get_object_array(self):
502511
freq = self.freq
503512
return np.array([Period._from_ordinal(ordinal=x, freq=freq)

pandas/tseries/tests/test_period.py

+10
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,16 @@ def test_to_timestamp_pi_mult(self):
20302030
['2011-02-28', 'NaT', '2011-03-31'], name='idx')
20312031
self.assert_index_equal(result, expected)
20322032

2033+
def test_start_time(self):
2034+
index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31')
2035+
expected_index = date_range('2016-01-01', end='2016-05-31', freq='MS')
2036+
self.assertTrue(index.start_time.equals(expected_index))
2037+
2038+
def test_end_time(self):
2039+
index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31')
2040+
expected_index = date_range('2016-01-01', end='2016-05-31', freq='M')
2041+
self.assertTrue(index.end_time.equals(expected_index))
2042+
20332043
def test_as_frame_columns(self):
20342044
rng = period_range('1/1/2000', periods=5)
20352045
df = DataFrame(randn(10, 5), columns=rng)

0 commit comments

Comments
 (0)