Skip to content

ENH: Standardized timeseries accessor names, #9606 #11489

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

Closed
wants to merge 1 commit into from
Closed
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: 4 additions & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,10 @@ These can be accessed like ``Series.dt.<property>``.
Series.dt.week
Series.dt.weekofyear
Copy link
Contributor

Choose a reason for hiding this comment

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

week_of_year as well

Copy link
Member

Choose a reason for hiding this comment

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

I think we should choose between week or week_of_year here. If we choose for week as the 'main' attribute, then I don't think it is needed to add week_of_year

But I don't really have a strong preference between week or week_of_year

Series.dt.dayofweek
Series.dt.day_of_week
Series.dt.weekday
Series.dt.dayofyear
Series.dt.day_of_year
Series.dt.quarter
Series.dt.is_month_start
Series.dt.is_month_end
Expand Down Expand Up @@ -1469,9 +1471,11 @@ Time/Date Components
DatetimeIndex.date
DatetimeIndex.time
DatetimeIndex.dayofyear
DatetimeIndex.day_of_year
DatetimeIndex.weekofyear
DatetimeIndex.week
DatetimeIndex.dayofweek
DatetimeIndex.day_of_week
DatetimeIndex.weekday
DatetimeIndex.quarter
DatetimeIndex.tz
Expand Down
4 changes: 2 additions & 2 deletions doc/source/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,10 @@ There are several time/date properties that one can access from ``Timestamp`` or
nanosecond,"The nanoseconds of the datetime"
date,"Returns datetime.date"
time,"Returns datetime.time"
dayofyear,"The ordinal day of year"
day_of_year,"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"
day_of_week,"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)"
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Enhancements
API changes
~~~~~~~~~~~

- day_of_year and day_of_week accessors for timeseries now exist in addition to dayofyear and dayofweek (standardizes naming conventions) (:issue:`9606`)
- min and max reductions on ``datetime64`` and ``timedelta64`` dtyped series now
result in ``NaT`` and not ``nan`` (:issue:`11245`).
- Regression from 0.16.2 for output formatting of long floats/nan, restored in (:issue:`11302`)
Expand Down
6 changes: 6 additions & 0 deletions pandas/src/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -959,9 +959,15 @@ cdef class Period(object):
property weekday:
def __get__(self):
return self.dayofweek
property day_of_week:
def __get__(self):
return self.dayofweek
property dayofyear:
def __get__(self):
return self._field(9)
property day_of_year:
def __get__(self):
return self.dayofyear
property quarter:
def __get__(self):
return self._field(2)
Expand Down
12 changes: 8 additions & 4 deletions pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ def _join_i8_wrapper(joinf, **kwargs):
_comparables = ['name', 'freqstr', 'tz']
_attributes = ['name', 'freq', 'tz']
_datetimelike_ops = ['year','month','day','hour','minute','second',
'weekofyear','week','dayofweek','weekday','dayofyear','quarter', 'days_in_month', 'daysinmonth',
'date','time','microsecond','nanosecond','is_month_start','is_month_end',
'is_quarter_start','is_quarter_end','is_year_start','is_year_end',
'tz','freq']
'weekofyear','week','dayofweek','day_of_week',
'weekday','dayofyear','day_of_year','quarter',
'days_in_month', 'daysinmonth','date',
'time','microsecond','nanosecond','is_month_start',
'is_month_end','is_quarter_start','is_quarter_end',
'is_year_start','is_year_end','tz','freq']
_is_numeric_dtype = False


Expand Down Expand Up @@ -1458,7 +1460,9 @@ def _set_freq(self, value):
dayofweek = _field_accessor('dayofweek', 'dow',
"The day of the week with Monday=0, Sunday=6")
weekday = dayofweek
day_of_week = dayofweek
dayofyear = _field_accessor('dayofyear', 'doy', "The ordinal day of the year")
day_of_year = dayofyear
quarter = _field_accessor('quarter', 'q', "The quarter of the date")
Copy link
Contributor

Choose a reason for hiding this comment

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

let's switch the impl to the new names (and make the original ones aliases)
that way they are easy to remove

days_in_month = _field_accessor('days_in_month', 'dim', "The number of days in the month\n\n.. versionadded:: 0.16.0")
daysinmonth = days_in_month
Expand Down
7 changes: 6 additions & 1 deletion pandas/tseries/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ class PeriodIndex(DatelikeOps, DatetimeIndexOpsMixin, Int64Index):
_typ = 'periodindex'
_attributes = ['name','freq']
_datetimelike_ops = ['year','month','day','hour','minute','second',
'weekofyear','week','dayofweek','weekday','dayofyear','quarter', 'qyear', 'freq', 'days_in_month', 'daysinmonth']
'weekofyear','week','dayofweek',
'day_of_week','weekday','dayofyear','day_of_year',
'quarter','qyear', 'freq', 'days_in_month',
'daysinmonth']
_is_numeric_dtype = False
freq = None

Expand Down Expand Up @@ -477,7 +480,9 @@ def to_datetime(self, dayfirst=False):
week = weekofyear
dayofweek = _field_accessor('dayofweek', 10, "The day of the week with Monday=0, Sunday=6")
weekday = dayofweek
day_of_week = dayofweek
dayofyear = day_of_year = _field_accessor('dayofyear', 9, "The ordinal day of the year")
day_of_year = dayofyear
quarter = _field_accessor('quarter', 2, "The quarter of the date")
qyear = _field_accessor('qyear', 1)
days_in_month = _field_accessor('days_in_month', 11, "The number of days in the month")
Expand Down
7 changes: 4 additions & 3 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,8 @@ def test_nat_vector_field_access(self):

fields = ['year', 'quarter', 'month', 'day', 'hour',
'minute', 'second', 'microsecond', 'nanosecond',
'week', 'dayofyear', 'days_in_month']
'week', 'dayofyear', 'day_of_year', 'day_of_week',
'days_in_month']
for field in fields:
result = getattr(idx, field)
expected = [getattr(x, field) if x is not NaT else np.nan
Expand All @@ -945,8 +946,8 @@ 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']
'week', 'dayofyear', 'day_of_year' 'days_in_month',
'daysinmonth', 'dayofweek', 'day_of_week']
for field in fields:
result = getattr(NaT, field)
self.assertTrue(np.isnan(result))
Expand Down
7 changes: 6 additions & 1 deletion pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,14 @@ class Timestamp(_Timestamp):
def dayofweek(self):
return self.weekday()

day_of_week = dayofweek

@property
def dayofyear(self):
return self._get_field('doy')

day_of_year = dayofyear

@property
def week(self):
return self._get_field('woy')
Expand Down Expand Up @@ -650,7 +654,8 @@ class NaTType(_NaT):

fields = ['year', 'quarter', 'month', 'day', 'hour',
'minute', 'second', 'millisecond', 'microsecond', 'nanosecond',
'week', 'dayofyear', 'days_in_month', 'daysinmonth', 'dayofweek']
'week', 'dayofyear', 'day_of_year', 'days_in_month', 'daysinmonth',
'dayofweek', 'day_of_week']
for field in fields:
prop = property(fget=lambda self: np.nan)
setattr(NaTType, field, prop)
Expand Down