-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: GH11128 add weekday_name to DatetimeIndex and .dt #12803
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 1 commit
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 |
---|---|---|
|
@@ -469,6 +469,7 @@ These can be accessed like ``Series.dt.<property>``. | |
Series.dt.days_in_month | ||
Series.dt.tz | ||
Series.dt.freq | ||
Series.dt.weekday_name | ||
|
||
**Datetime Methods** | ||
|
||
|
@@ -1487,6 +1488,7 @@ Time/Date Components | |
DatetimeIndex.is_year_start | ||
DatetimeIndex.is_year_end | ||
DatetimeIndex.inferred_freq | ||
DatetimeIndex.weekday_name | ||
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. same here |
||
|
||
Selecting | ||
~~~~~~~~~ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ def test_ops_properties(self): | |
'is_month_start', 'is_month_end', | ||
'is_quarter_start', | ||
'is_quarter_end', 'is_year_start', | ||
'is_year_end'], | ||
'is_year_end', 'weekday_name'], | ||
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. there are addl tests in 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. Yes, and they were added to in my original PR. |
||
lambda x: isinstance(x, DatetimeIndex)) | ||
|
||
def test_ops_properties_basic(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -959,7 +959,7 @@ def test_nat_vector_field_access(self): | |
def test_nat_scalar_field_access(self): | ||
fields = ['year', 'quarter', 'month', 'day', 'hour', 'minute', | ||
'second', 'microsecond', 'nanosecond', 'week', 'dayofyear', | ||
'days_in_month', 'daysinmonth', 'dayofweek'] | ||
'days_in_month', 'daysinmonth', 'dayofweek', 'weekday_name'] | ||
for field in fields: | ||
result = getattr(NaT, field) | ||
self.assertTrue(np.isnan(result)) | ||
|
@@ -1852,7 +1852,7 @@ def test_timestamp_fields(self): | |
fields = ['dayofweek', 'dayofyear', 'week', 'weekofyear', 'quarter', | ||
'days_in_month', 'is_month_start', 'is_month_end', | ||
'is_quarter_start', 'is_quarter_end', 'is_year_start', | ||
'is_year_end'] | ||
'is_year_end', 'weekday_name'] | ||
for f in fields: | ||
expected = getattr(idx, f)[-1] | ||
result = getattr(Timestamp(idx[-1]), f) | ||
|
@@ -3541,6 +3541,15 @@ def test_datetimeindex_accessors(self): | |
self.assertEqual(dti.is_year_end[0], False) | ||
self.assertEqual(dti.is_year_end[364], True) | ||
|
||
# GH 11128 | ||
self.assertEqual(dti.weekday_name[4], u'Monday') | ||
self.assertEqual(dti.weekday_name[5], u'Tuesday') | ||
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 a test for |
||
self.assertEqual(dti.weekday_name[6], u'Wednesday') | ||
self.assertEqual(dti.weekday_name[7], u'Thursday') | ||
self.assertEqual(dti.weekday_name[8], u'Friday') | ||
self.assertEqual(dti.weekday_name[9], u'Saturday') | ||
self.assertEqual(dti.weekday_name[10], u'Sunday') | ||
|
||
self.assertEqual(len(dti.year), 365) | ||
self.assertEqual(len(dti.month), 365) | ||
self.assertEqual(len(dti.day), 365) | ||
|
@@ -3558,6 +3567,7 @@ def test_datetimeindex_accessors(self): | |
self.assertEqual(len(dti.is_quarter_end), 365) | ||
self.assertEqual(len(dti.is_year_start), 365) | ||
self.assertEqual(len(dti.is_year_end), 365) | ||
self.assertEqual(len(dti.weekday_name), 365) | ||
|
||
dti = DatetimeIndex(freq='BQ-FEB', start=datetime(1998, 1, 1), | ||
periods=4) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -398,6 +398,11 @@ class Timestamp(_Timestamp): | |
def dayofweek(self): | ||
return self.weekday() | ||
|
||
@property | ||
def weekday_name(self): | ||
out = get_date_name_field(np.array([self.value], dtype=np.int64), 'weekday_name') | ||
return out[0] | ||
|
||
@property | ||
def dayofyear(self): | ||
return self._get_field('doy') | ||
|
@@ -667,7 +672,7 @@ class NaTType(_NaT): | |
|
||
fields = ['year', 'quarter', 'month', 'day', 'hour', | ||
'minute', 'second', 'millisecond', 'microsecond', 'nanosecond', | ||
'week', 'dayofyear', 'days_in_month', 'daysinmonth', 'dayofweek'] | ||
'week', 'dayofyear', 'days_in_month', 'daysinmonth', 'dayofweek', 'weekday_name'] | ||
for field in fields: | ||
prop = property(fget=lambda self: np.nan) | ||
setattr(NaTType, field, prop) | ||
|
@@ -4390,6 +4395,38 @@ def get_start_end_field(ndarray[int64_t] dtindex, object field, object freqstr=N | |
|
||
raise ValueError("Field %s not supported" % field) | ||
|
||
@cython.wraparound(False) | ||
@cython.boundscheck(False) | ||
def get_date_name_field(ndarray[int64_t] dtindex, object field): | ||
''' | ||
Given a int64-based datetime index, return array of strings of date | ||
name based on requested field (e.g. weekday_name) | ||
''' | ||
cdef: | ||
_TSObject ts | ||
Py_ssize_t i, count = 0 | ||
ndarray[object] out | ||
pandas_datetimestruct dts | ||
int dow | ||
|
||
_dayname = np.array( | ||
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], | ||
dtype=np.str ) | ||
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.
|
||
|
||
count = len(dtindex) | ||
out = np.empty(count, dtype=object) | ||
|
||
if field == 'weekday_name': | ||
for i in range(count): | ||
if dtindex[i] == NPY_NAT: out[i] = np.nan; continue | ||
|
||
pandas_datetime_to_datetimestruct(dtindex[i], PANDAS_FR_ns, &dts) | ||
dow = dayofweek(dts.year, dts.month, dts.day) | ||
out[i] = _dayname[dow] | ||
return out | ||
|
||
raise ValueError("Field %s not supported" % field) | ||
|
||
|
||
cdef inline int m8_weekday(int64_t val): | ||
ts = convert_to_tsobject(val, None, None) | ||
|
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 move after
.weekday