-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API: add is_beg_month/quarter/year, is_end_month/quarter/year accessors (#4565) #4823
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -408,6 +408,39 @@ regularity will result in a ``DatetimeIndex`` (but frequency is lost): | |
|
||
.. _timeseries.offsets: | ||
|
||
Time/Date Components | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
There are several time/date properties that one can access from ``Timestamp`` or a collection of timestamps like a ``DateTimeIndex``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gr8! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe at least add that they are also available as Series/DataFrame attributes when it has a DatetimeIndex? And you copy paste relevant section of whatsnew to here I think: https://github.com/pydata/pandas/blob/master/doc/source/v0.14.0.txt#L72 (but can also for later PR) |
||
|
||
.. csv-table:: | ||
:header: "Property", "Description" | ||
:widths: 15, 65 | ||
|
||
year, "The year of the datetime" | ||
month,"The month of the datetime" | ||
day,"The days of the datetime" | ||
hour,"The hour of the datetime" | ||
minute,"The minutes of the datetime" | ||
second,"The seconds of the datetime" | ||
microsecond,"The microseconds of the datetime" | ||
nanosecond,"The nanoseconds of the datetime" | ||
date,"Returns datetime.date" | ||
time,"Returns datetime.time" | ||
dayofyear,"The ordinal day of year" | ||
weekofyear,"The week ordinal of the year" | ||
week,"The week ordinal of the year" | ||
dayofweek,"The day of the week with Monday=0, Sunday=6" | ||
weekday,"The day of the week with Monday=0, Sunday=6" | ||
quarter,"Quarter of the date: Jan=Mar = 1, Apr-Jun = 2, etc." | ||
is_month_start,"Logical indicating if first day of month (defined by frequency)" | ||
is_month_end,"Logical indicating if last day of month (defined by frequency)" | ||
is_quarter_start,"Logical indicating if first day of quarter (defined by frequency)" | ||
is_quarter_end,"Logical indicating if last day of quarter (defined by frequency)" | ||
is_year_start,"Logical indicating if first day of year (defined by frequency)" | ||
is_year_end,"Logical indicating if last day of year (defined by frequency)" | ||
|
||
|
||
DateOffset objects | ||
------------------ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1473,7 +1473,7 @@ def test_timestamp_fields(self): | |
# extra fields from DatetimeIndex like quarter and week | ||
idx = tm.makeDateIndex(100) | ||
|
||
fields = ['dayofweek', 'dayofyear', 'week', 'weekofyear', 'quarter'] | ||
fields = ['dayofweek', 'dayofyear', 'week', 'weekofyear', 'quarter', 'is_month_start', 'is_month_end', 'is_quarter_start', 'is_quarter_end', 'is_year_start', 'is_year_end'] | ||
for f in fields: | ||
expected = getattr(idx, f)[-1] | ||
result = getattr(Timestamp(idx[-1]), f) | ||
|
@@ -2192,7 +2192,7 @@ def test_join_with_period_index(self): | |
|
||
class TestDatetime64(tm.TestCase): | ||
""" | ||
Also test supoprt for datetime64[ns] in Series / DataFrame | ||
Also test support for datetime64[ns] in Series / DataFrame | ||
""" | ||
|
||
def setUp(self): | ||
|
@@ -2202,37 +2202,115 @@ def setUp(self): | |
|
||
def test_datetimeindex_accessors(self): | ||
dti = DatetimeIndex( | ||
freq='Q-JAN', start=datetime(1997, 12, 31), periods=100) | ||
freq='D', start=datetime(1998, 1, 1), periods=365) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add basically a copy of this test with a different freq as well (maybe something like MonthEnd)? |
||
|
||
self.assertEquals(dti.year[0], 1998) | ||
self.assertEquals(dti.month[0], 1) | ||
self.assertEquals(dti.day[0], 31) | ||
self.assertEquals(dti.day[0], 1) | ||
self.assertEquals(dti.hour[0], 0) | ||
self.assertEquals(dti.minute[0], 0) | ||
self.assertEquals(dti.second[0], 0) | ||
self.assertEquals(dti.microsecond[0], 0) | ||
self.assertEquals(dti.dayofweek[0], 5) | ||
self.assertEquals(dti.dayofweek[0], 3) | ||
|
||
self.assertEquals(dti.dayofyear[0], 31) | ||
self.assertEquals(dti.dayofyear[1], 120) | ||
self.assertEquals(dti.dayofyear[0], 1) | ||
self.assertEquals(dti.dayofyear[120], 121) | ||
|
||
self.assertEquals(dti.weekofyear[0], 5) | ||
self.assertEquals(dti.weekofyear[1], 18) | ||
self.assertEquals(dti.weekofyear[0], 1) | ||
self.assertEquals(dti.weekofyear[120], 18) | ||
|
||
self.assertEquals(dti.quarter[0], 1) | ||
self.assertEquals(dti.quarter[1], 2) | ||
|
||
self.assertEquals(len(dti.year), 100) | ||
self.assertEquals(len(dti.month), 100) | ||
self.assertEquals(len(dti.day), 100) | ||
self.assertEquals(len(dti.hour), 100) | ||
self.assertEquals(len(dti.minute), 100) | ||
self.assertEquals(len(dti.second), 100) | ||
self.assertEquals(len(dti.microsecond), 100) | ||
self.assertEquals(len(dti.dayofweek), 100) | ||
self.assertEquals(len(dti.dayofyear), 100) | ||
self.assertEquals(len(dti.weekofyear), 100) | ||
self.assertEquals(len(dti.quarter), 100) | ||
self.assertEquals(dti.quarter[120], 2) | ||
|
||
self.assertEquals(dti.is_month_start[0], True) | ||
self.assertEquals(dti.is_month_start[1], False) | ||
self.assertEquals(dti.is_month_start[31], True) | ||
self.assertEquals(dti.is_quarter_start[0], True) | ||
self.assertEquals(dti.is_quarter_start[90], True) | ||
self.assertEquals(dti.is_year_start[0], True) | ||
self.assertEquals(dti.is_year_start[364], False) | ||
self.assertEquals(dti.is_month_end[0], False) | ||
self.assertEquals(dti.is_month_end[30], True) | ||
self.assertEquals(dti.is_month_end[31], False) | ||
self.assertEquals(dti.is_month_end[364], True) | ||
self.assertEquals(dti.is_quarter_end[0], False) | ||
self.assertEquals(dti.is_quarter_end[30], False) | ||
self.assertEquals(dti.is_quarter_end[89], True) | ||
self.assertEquals(dti.is_quarter_end[364], True) | ||
self.assertEquals(dti.is_year_end[0], False) | ||
self.assertEquals(dti.is_year_end[364], True) | ||
|
||
self.assertEquals(len(dti.year), 365) | ||
self.assertEquals(len(dti.month), 365) | ||
self.assertEquals(len(dti.day), 365) | ||
self.assertEquals(len(dti.hour), 365) | ||
self.assertEquals(len(dti.minute), 365) | ||
self.assertEquals(len(dti.second), 365) | ||
self.assertEquals(len(dti.microsecond), 365) | ||
self.assertEquals(len(dti.dayofweek), 365) | ||
self.assertEquals(len(dti.dayofyear), 365) | ||
self.assertEquals(len(dti.weekofyear), 365) | ||
self.assertEquals(len(dti.quarter), 365) | ||
self.assertEquals(len(dti.is_month_start), 365) | ||
self.assertEquals(len(dti.is_month_end), 365) | ||
self.assertEquals(len(dti.is_quarter_start), 365) | ||
self.assertEquals(len(dti.is_quarter_end), 365) | ||
self.assertEquals(len(dti.is_year_start), 365) | ||
self.assertEquals(len(dti.is_year_end), 365) | ||
|
||
dti = DatetimeIndex( | ||
freq='BQ-FEB', start=datetime(1998, 1, 1), periods=4) | ||
|
||
self.assertEquals(sum(dti.is_quarter_start), 0) | ||
self.assertEquals(sum(dti.is_quarter_end), 4) | ||
self.assertEquals(sum(dti.is_year_start), 0) | ||
self.assertEquals(sum(dti.is_year_end), 1) | ||
|
||
# Ensure is_start/end accessors throw ValueError for CustomBusinessDay, CBD requires np >= 1.7 | ||
if not _np_version_under1p7: | ||
bday_egypt = offsets.CustomBusinessDay(weekmask='Sun Mon Tue Wed Thu') | ||
dti = date_range(datetime(2013, 4, 30), periods=5, freq=bday_egypt) | ||
self.assertRaises(ValueError, lambda: dti.is_month_start) | ||
|
||
dti = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03']) | ||
|
||
self.assertEquals(dti.is_month_start[0], 1) | ||
|
||
tests = [ | ||
(Timestamp('2013-06-01', offset='M').is_month_start, 1), | ||
(Timestamp('2013-06-01', offset='BM').is_month_start, 0), | ||
(Timestamp('2013-06-03', offset='M').is_month_start, 0), | ||
(Timestamp('2013-06-03', offset='BM').is_month_start, 1), | ||
(Timestamp('2013-02-28', offset='Q-FEB').is_month_end, 1), | ||
(Timestamp('2013-02-28', offset='Q-FEB').is_quarter_end, 1), | ||
(Timestamp('2013-02-28', offset='Q-FEB').is_year_end, 1), | ||
(Timestamp('2013-03-01', offset='Q-FEB').is_month_start, 1), | ||
(Timestamp('2013-03-01', offset='Q-FEB').is_quarter_start, 1), | ||
(Timestamp('2013-03-01', offset='Q-FEB').is_year_start, 1), | ||
(Timestamp('2013-03-31', offset='QS-FEB').is_month_end, 1), | ||
(Timestamp('2013-03-31', offset='QS-FEB').is_quarter_end, 0), | ||
(Timestamp('2013-03-31', offset='QS-FEB').is_year_end, 0), | ||
(Timestamp('2013-02-01', offset='QS-FEB').is_month_start, 1), | ||
(Timestamp('2013-02-01', offset='QS-FEB').is_quarter_start, 1), | ||
(Timestamp('2013-02-01', offset='QS-FEB').is_year_start, 1), | ||
(Timestamp('2013-06-30', offset='BQ').is_month_end, 0), | ||
(Timestamp('2013-06-30', offset='BQ').is_quarter_end, 0), | ||
(Timestamp('2013-06-30', offset='BQ').is_year_end, 0), | ||
(Timestamp('2013-06-28', offset='BQ').is_month_end, 1), | ||
(Timestamp('2013-06-28', offset='BQ').is_quarter_end, 1), | ||
(Timestamp('2013-06-28', offset='BQ').is_year_end, 0), | ||
(Timestamp('2013-06-30', offset='BQS-APR').is_month_end, 0), | ||
(Timestamp('2013-06-30', offset='BQS-APR').is_quarter_end, 0), | ||
(Timestamp('2013-06-30', offset='BQS-APR').is_year_end, 0), | ||
(Timestamp('2013-06-28', offset='BQS-APR').is_month_end, 1), | ||
(Timestamp('2013-06-28', offset='BQS-APR').is_quarter_end, 1), | ||
(Timestamp('2013-03-29', offset='BQS-APR').is_year_end, 1), | ||
(Timestamp('2013-11-01', offset='AS-NOV').is_year_start, 1), | ||
(Timestamp('2013-10-31', offset='AS-NOV').is_year_end, 1)] | ||
|
||
for ts, value in tests: | ||
self.assertEquals(ts, value) | ||
|
||
|
||
def test_nanosecond_field(self): | ||
dti = DatetimeIndex(np.arange(10)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you also add into v0.14.0.txt (put right below the section where I show #4551), where the index methods on series are defined, otherwise good to go