Skip to content

ENH: add missing methods to .dt for Period, resolves #8848 #12463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <io.bigquery_authentication>`
- ``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:
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/test_datetime_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
11 changes: 10 additions & 1 deletion pandas/tseries/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down