Skip to content

BUG: Return local Timestamp.weekday_name attribute (#17354) #17377

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

Merged
merged 6 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ Conversion
- Fixed the return type of ``IntervalIndex.is_non_overlapping_monotonic`` to be a Python ``bool`` for consistency with similar attributes/methods. Previously returned a ``numpy.bool_``. (:issue:`17237`)
- Bug in ``IntervalIndex.is_non_overlapping_monotonic`` when intervals are closed on both sides and overlap at a point (:issue:`16560`)
- Bug in :func:`Series.fillna` returns frame when ``inplace=True`` and ``value`` is dict (:issue:`16156`)
- Bug in :attr:`Timestamp.weekday_name` returning a UTC-based weekday name when localized to a timezone (:issue:`17354`)

Indexing
^^^^^^^^
Expand Down
28 changes: 23 additions & 5 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,7 @@ class Timestamp(_Timestamp):

@property
def weekday_name(self):
out = get_date_name_field(
np.array([self.value], dtype=np.int64), 'weekday_name')
return out[0]
return self._get_named_field('weekday_name')

@property
def dayofyear(self):
Expand Down Expand Up @@ -1268,13 +1266,33 @@ cdef class _Timestamp(datetime):
# same timezone if specified)
return datetime.__sub__(self, other)

cpdef _get_field(self, field):
cdef int64_t _maybe_convert_value_to_local(self):
"""Convert UTC i8 value to local i8 value if tz exists"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to add a

cdef int64_t val

here in order to type it

cdef:
int64_t val
val = self.value
if self.tz is not None and not _is_utc(self.tz):
val = tz_convert_single(self.value, 'UTC', self.tz)
out = get_date_field(np.array([val], dtype=np.int64), field)
return val

cpdef _get_field(self, field):
cdef:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like this

int64_t val
ndarray[int64_t] date_array = np.empty(1, dtype=np.int64)
val = self._maybe_convert_value_to_local()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can type val as cdef: int64_t (same for _get_named_field)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you can type out in these (ndarray[int32_t]) i think

date_array[0] = val
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is overkill to type the input ndarray itself

it was out which was looking to type, though not a big deal, so just remove the date_array temp. ping on green.

out = get_date_field(date_array, field)
return int(out[0])

cpdef _get_named_field(self, field):
cdef:
int64_t val
ndarray[int64_t] date_array = np.empty(1, dtype=np.int64)
val = self._maybe_convert_value_to_local()
date_array[0] = val
out = get_date_name_field(date_array, field)
return out[0]

cpdef _get_start_end_field(self, field):
month_kw = self.freq.kwds.get(
'startingMonth', self.freq.kwds.get(
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/scalar/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,14 @@ def check(value, equal):
for end in ends:
assert getattr(ts, end)

@pytest.mark.parametrize('data, expected',
[(Timestamp('2017-08-28 23:00:00'), 'Monday'),
(Timestamp('2017-08-28 23:00:00', tz='EST'),
'Monday')])
def test_weekday_name(self, data, expected):
# GH 17354
assert data.weekday_name == expected

def test_pprint(self):
# GH12622
import pprint
Expand Down