Skip to content

ENH: Timestamp.month_name, day_name support non-nano #46959

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 2 commits into from
May 7, 2022
Merged
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
4 changes: 2 additions & 2 deletions asv_bench/benchmarks/tslibs/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class TimeGetStartEndField:

def setup(self, size, side, period, freqstr, month_kw):
arr = np.random.randint(0, 10, size=size, dtype="i8")
self.dt64data = arr.view("M8[ns]")
self.i8data = arr

self.attrname = f"is_{period}_{side}"

def time_get_start_end_field(self, size, side, period, freqstr, month_kw):
get_start_end_field(self.dt64data, self.attrname, freqstr, month_kw=month_kw)
get_start_end_field(self.i8data, self.attrname, freqstr, month_kw=month_kw)
4 changes: 3 additions & 1 deletion pandas/_libs/tslibs/fields.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ def get_date_name_field(
dtindex: npt.NDArray[np.int64], # const int64_t[:]
field: str,
locale: str | None = ...,
reso: int = ..., # NPY_DATETIMEUNIT
) -> npt.NDArray[np.object_]: ...
def get_start_end_field(
dt64values: npt.NDArray[np.datetime64],
dtindex: npt.NDArray[np.int64],
field: str,
freqstr: str | None = ...,
month_kw: int = ...,
reso: int = ..., # NPY_DATETIMEUNIT
) -> npt.NDArray[np.bool_]: ...
def get_date_field(
dtindex: npt.NDArray[np.int64], # const int64_t[:]
Expand Down
30 changes: 20 additions & 10 deletions pandas/_libs/tslibs/fields.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ from pandas._libs.tslibs.ccalendar cimport (
from pandas._libs.tslibs.nattype cimport NPY_NAT
from pandas._libs.tslibs.np_datetime cimport (
NPY_DATETIMEUNIT,
NPY_FR_ns,
dt64_to_dtstruct,
get_unit_from_dtype,
npy_datetimestruct,
Expand Down Expand Up @@ -139,13 +140,18 @@ def month_position_check(fields, weekdays) -> str | None:

@cython.wraparound(False)
@cython.boundscheck(False)
def get_date_name_field(const int64_t[:] dtindex, str field, object locale=None):
def get_date_name_field(
const int64_t[:] dtindex,
str field,
object locale=None,
NPY_DATETIMEUNIT reso=NPY_FR_ns,
):
"""
Given a int64-based datetime index, return array of strings of date
name based on requested field (e.g. day_name)
"""
cdef:
Py_ssize_t i, count = len(dtindex)
Py_ssize_t i, count = dtindex.shape[0]
ndarray[object] out, names
npy_datetimestruct dts
int dow
Expand All @@ -163,7 +169,7 @@ def get_date_name_field(const int64_t[:] dtindex, str field, object locale=None)
out[i] = np.nan
continue

dt64_to_dtstruct(dtindex[i], &dts)
pandas_datetime_to_datetimestruct(dtindex[i], reso, &dts)
dow = dayofweek(dts.year, dts.month, dts.day)
out[i] = names[dow].capitalize()

Expand All @@ -178,7 +184,7 @@ def get_date_name_field(const int64_t[:] dtindex, str field, object locale=None)
out[i] = np.nan
continue

dt64_to_dtstruct(dtindex[i], &dts)
pandas_datetime_to_datetimestruct(dtindex[i], reso, &dts)
out[i] = names[dts.month].capitalize()

else:
Expand All @@ -201,35 +207,39 @@ cdef inline bint _is_on_month(int month, int compare_month, int modby) nogil:

@cython.wraparound(False)
@cython.boundscheck(False)
def get_start_end_field(ndarray dt64values, str field,
str freqstr=None, int month_kw=12):
def get_start_end_field(
const int64_t[:] dtindex,
str field,
str freqstr=None,
int month_kw=12,
NPY_DATETIMEUNIT reso=NPY_FR_ns,
):
"""
Given an int64-based datetime index return array of indicators
of whether timestamps are at the start/end of the month/quarter/year
(defined by frequency).

Parameters
----------
dt64values : ndarray[datetime64], any resolution
dtindex : ndarray[int64]
field : str
frestr : str or None, default None
month_kw : int, default 12
reso : NPY_DATETIMEUNIT, default NPY_FR_ns

Returns
-------
ndarray[bool]
"""
cdef:
Py_ssize_t i
int count = dt64values.size
int count = dtindex.shape[0]
bint is_business = 0
int end_month = 12
int start_month = 1
ndarray[int8_t] out
npy_datetimestruct dts
int compare_month, modby
ndarray dtindex = dt64values.view("i8")
NPY_DATETIMEUNIT reso = get_unit_from_dtype(dt64values.dtype)

out = np.zeros(count, dtype='int8')

Expand Down
12 changes: 4 additions & 8 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ cdef class _Timestamp(ABCTimestamp):
dict kwds
ndarray[uint8_t, cast=True] out
int month_kw
str unit

if freq:
kwds = freq.kwds
Expand All @@ -499,9 +498,8 @@ cdef class _Timestamp(ABCTimestamp):

val = self._maybe_convert_value_to_local()

unit = npy_unit_to_abbrev(self._reso)
out = get_start_end_field(np.array([val], dtype=f"M8[{unit}]"),
field, freqstr, month_kw)
out = get_start_end_field(np.array([val], dtype=np.int64),
field, freqstr, month_kw, self._reso)
return out[0]

cdef _warn_on_field_deprecation(self, freq, str field):
Expand Down Expand Up @@ -661,12 +659,10 @@ cdef class _Timestamp(ABCTimestamp):
int64_t val
object[::1] out

if self._reso != NPY_FR_ns:
raise NotImplementedError(self._reso)

val = self._maybe_convert_value_to_local()

out = get_date_name_field(np.array([val], dtype=np.int64),
field, locale=locale)
field, locale=locale, reso=self._reso)
return out[0]

def day_name(self, locale=None) -> str:
Expand Down
18 changes: 14 additions & 4 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@
tz_convert_from_utc,
tzconversion,
)
from pandas._libs.tslibs.np_datetime import py_get_unit_from_dtype
from pandas._typing import npt
from pandas.errors import (
OutOfBoundsDatetime,
PerformanceWarning,
)
from pandas.util._decorators import cache_readonly
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import validate_inclusive

Expand Down Expand Up @@ -131,7 +133,7 @@ def f(self):
month_kw = kwds.get("startingMonth", kwds.get("month", 12))

result = fields.get_start_end_field(
values.view(self._ndarray.dtype), field, self.freqstr, month_kw
values, field, self.freqstr, month_kw, reso=self._reso
)
else:
result = fields.get_date_field(values, field)
Expand All @@ -140,7 +142,7 @@ def f(self):
return result

if field in self._object_ops:
result = fields.get_date_name_field(values, field)
result = fields.get_date_name_field(values, field, reso=self._reso)
result = self._maybe_mask_results(result, fill_value=None)

else:
Expand Down Expand Up @@ -544,6 +546,10 @@ def _check_compatible_with(self, other, setitem: bool = False):
# -----------------------------------------------------------------
# Descriptive Properties

@cache_readonly
def _reso(self):
return py_get_unit_from_dtype(self._ndarray.dtype)

def _box_func(self, x: np.datetime64) -> Timestamp | NaTType:
# GH#42228
value = x.view("i8")
Expand Down Expand Up @@ -1270,7 +1276,9 @@ def month_name(self, locale=None):
"""
values = self._local_timestamps()

result = fields.get_date_name_field(values, "month_name", locale=locale)
result = fields.get_date_name_field(
values, "month_name", locale=locale, reso=self._reso
)
result = self._maybe_mask_results(result, fill_value=None)
return result

Expand Down Expand Up @@ -1313,7 +1321,9 @@ def day_name(self, locale=None):
"""
values = self._local_timestamps()

result = fields.get_date_name_field(values, "day_name", locale=locale)
result = fields.get_date_name_field(
values, "day_name", locale=locale, reso=self._reso
)
result = self._maybe_mask_results(result, fill_value=None)
return result

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,14 @@ def test_start_end_fields(self, ts):
assert not ts.is_month_end
assert not ts.is_month_end

def test_day_name(self, dt64, ts):
alt = Timestamp(dt64)
assert ts.day_name() == alt.day_name()

def test_month_name(self, dt64, ts):
alt = Timestamp(dt64)
assert ts.month_name() == alt.month_name()

def test_repr(self, dt64, ts):
alt = Timestamp(dt64)

Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/tslibs/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ def test_get_date_field_readonly(dtindex):


def test_get_start_end_field_readonly(dtindex):
dt64values = dtindex.view("M8[ns]")
dt64values.flags.writeable = False

result = fields.get_start_end_field(dt64values, "is_month_start", None)
result = fields.get_start_end_field(dtindex, "is_month_start", None)
expected = np.array([True, False, False, False, False], dtype=np.bool_)
tm.assert_numpy_array_equal(result, expected)

Expand Down