-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: GH11128 add weekday_name to DatetimeIndex and .dt #11813
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 |
---|---|---|
|
@@ -494,6 +494,10 @@ class Timestamp(_Timestamp): | |
def is_year_end(self): | ||
return self._get_start_end_field('is_year_end') | ||
|
||
@property | ||
def weekday_name(self): | ||
return self._get_date_name_field('weekday_name') | ||
|
||
def tz_localize(self, tz, ambiguous='raise'): | ||
""" | ||
Convert naive Timestamp to local time zone, or remove | ||
|
@@ -1098,6 +1102,10 @@ cdef class _Timestamp(datetime): | |
out = get_start_end_field(np.array([self.value], dtype=np.int64), field, freqstr, month_kw) | ||
return out[0] | ||
|
||
cpdef _get_date_name_field(self, field): | ||
out = get_date_name_field(np.array([self.value], dtype=np.int64), field) | ||
return out[0] | ||
|
||
property asm8: | ||
def __get__(self): | ||
return np.datetime64(self.value, 'ns') | ||
|
@@ -4363,6 +4371,39 @@ 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 ) | ||
|
||
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] = -1; continue | ||
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. put 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. needs some tests for 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. We're returning strings, isn't an empty string better for NaT? or str(np.nan)? 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. no 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. tests go in |
||
|
||
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) | ||
return ts_dayofweek(ts) | ||
|
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.
I would just do this in the above property, not need to have another function
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.
Yes, we can just do:
return ["Monday","Tue...][self.weekday()]
in the property, no?
Although, I did find f_weekday in LocaleTime,.. not sure how to access that...
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.
not sure what you mean. the code can be moved directly w/o creating another function