diff --git a/doc/source/api.rst b/doc/source/api.rst index bfd1c92d14acd..a19dedb37d57e 100644 --- a/doc/source/api.rst +++ b/doc/source/api.rst @@ -499,8 +499,10 @@ These can be accessed like ``Series.dt.``. Series.dt.week Series.dt.weekofyear 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 @@ -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 diff --git a/doc/source/timeseries.rst b/doc/source/timeseries.rst index 1d21f96a7d539..c824395ea4cd4 100644 --- a/doc/source/timeseries.rst +++ b/doc/source/timeseries.rst @@ -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)" diff --git a/doc/source/whatsnew/v0.17.1.txt b/doc/source/whatsnew/v0.17.1.txt index 28129287d51af..36c304c8456d4 100755 --- a/doc/source/whatsnew/v0.17.1.txt +++ b/doc/source/whatsnew/v0.17.1.txt @@ -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`) diff --git a/pandas/src/period.pyx b/pandas/src/period.pyx index cfc50afc8f9f3..1a9279853a132 100644 --- a/pandas/src/period.pyx +++ b/pandas/src/period.pyx @@ -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) diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py index 868057c675594..ca499e31ab101 100644 --- a/pandas/tseries/index.py +++ b/pandas/tseries/index.py @@ -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 @@ -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") 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 diff --git a/pandas/tseries/period.py b/pandas/tseries/period.py index 888c50e86b7b2..42e204e32d02a 100644 --- a/pandas/tseries/period.py +++ b/pandas/tseries/period.py @@ -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 @@ -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") diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index 230016f00374f..4b55e759e2dbe 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -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 @@ -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)) diff --git a/pandas/tslib.pyx b/pandas/tslib.pyx index afb15badf433c..1edc92bbc38b6 100644 --- a/pandas/tslib.pyx +++ b/pandas/tslib.pyx @@ -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') @@ -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)