Skip to content
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

Add DatetimeAccessor for accessing datetime fields via .dt attribute #1356

Merged
merged 23 commits into from
Apr 29, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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 xarray/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .core.accessors import DatetimeAccessor
from .core.alignment import align, broadcast, broadcast_arrays
from .core.common import full_like, zeros_like, ones_like
from .core.combine import concat, auto_combine
Expand Down
104 changes: 104 additions & 0 deletions xarray/core/accessors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from .common import is_datetime_like
from .extensions import register_dataarray_accessor

from pandas import tslib as libts

@register_dataarray_accessor('dt')
Copy link
Member

Choose a reason for hiding this comment

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

Another thought: since this isn't being defined outside of xarray, it might actually make sense to import this class from datarray.py and simply add it to the DataArray class directly:

class DataArray:
    ...
    dt = property(DatatimeAccessor)

That has the advantage of being more transparent about whether it comes from.

class DatetimeAccessor(object):
"""Access datetime fields for DataArrays with datetime-like dtypes.

Similar to pandas, fields can be accessed through the `.dt` attribute
for applicable DataArrays:

>>> ds = xarray.Dataset({'time': pd.date_range(start='2000/01/01',
... freq='D', periods=100)})
>>> ds.time.dt
<xarray.core.accessors.DatetimeAccessor at 0x10c369f60>
>>> ds.time.dt.dayofyear[5]
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 this should be [:5], not [5]?

<xarray.DataArray 'dayofyear' (time: 5)>
array([1, 2, 3, 4, 5], dtype=int32)
Coordinates:
* time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 ...

All of the pandas fields are accessible here. Note that these fields are not
calendar-aware; if your datetimes are encoded with a non-Gregorian calendar
(e.g. a 360-day calendar), then some fields like `dayofyear` may not be
Copy link
Member

Choose a reason for hiding this comment

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

add "using netcdftime" just to be super explicit about where special calendars come from.

accurate.

"""
def __init__(self, xarray_obj):
if not is_datetime_like(xarray_obj.dtype):
raise TypeError("'dt' accessor only available for "
"DataArray with datetime64 dtype")
Copy link
Member

Choose a reason for hiding this comment

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

add "or timedelta64 dtype"

self._obj = xarray_obj
self._dt = None

_field_ops = ['year', 'month', 'day', 'hour', 'minute', 'second',
Copy link
Member

Choose a reason for hiding this comment

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

you don't need this variable anymore.

'weekofyear', 'week', 'weekday', 'dayofweek',
'dayofyear', 'quarter', 'days_in_month',
'daysinmonth', 'microsecond',
'nanosecond']

@property
def dt(self):
"""Attribute to cache a view of the underlying datetime-like
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we want to expose this as public facing API. So I would get ride of this property, probably.

array for passing to pandas.tslib for date_field operations
"""
if self._dt is None:
datetimes_asi8 = self._obj.values.view('i8')
self._dt = datetimes_asi8
return self._dt

# Modified from https://github.com/pandas-dev/pandas/pandas/tseries/index.py#L59
def _tslib_field_accessor(name, field, docstring=None):
def f(self):
from .dataarray import DataArray
values = self.dt
result = libts.get_date_field(values, field)
Copy link
Member

Choose a reason for hiding this comment

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

This is using private pandas API, so it's liable to break in the future. Instead, let's wrap things in a pandas.Series. Something like this should do the trick:

def get_dt_field(array: np.ndarray, name: str):
    series = pd.Series(array.ravel())
    field_values = getattr(series.dt, name).values
    return field_values.reshape(array.shape)

return DataArray(result, name=name,
Copy link
Member

Choose a reason for hiding this comment

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

can also just do type(self._obj) here instead of DataArray, and the result will probably work better on subclasses

coords=self._obj.coords, dims=self._obj.dims)

f.__name__ = name
f.__doc__ = docstring
return property(f)

year = _tslib_field_accessor('year', 'Y', "The year of the datetime")
month = _tslib_field_accessor(
'month', 'M', "The month as January=1, December=12"
)
day = _tslib_field_accessor('day', 'D', "The days of the datetime")
hour = _tslib_field_accessor('hour', 'h', "The hours of the datetime")
minute = _tslib_field_accessor('minute', 'm', "The minutes of the datetime")
second = _tslib_field_accessor('second', 's', "The seconds of the datetime")
microsecond = _tslib_field_accessor(
'microsecond', 'us', "The microseconds of the datetime"
)
nanosecond = _tslib_field_accessor(
'nanosecond', 'ns', "The nanoseconds of the datetime"
)
weekofyear = _tslib_field_accessor(
'weekofyear', 'woy', "The week ordinal of the year"
)
week = weekofyear
dayofweek = _tslib_field_accessor(
'dayofweek', 'dow', "The day of the week with Monday=0, Sunday=6"
)
weekday = dayofweek

weekday_name = _tslib_field_accessor(
'weekday_name', 'weekday_name',
"The name of day in a week (ex: Friday)"
)

dayofyear = _tslib_field_accessor(
'dayofyear', 'doy', "The ordinal day of the year"
)
quarter = _tslib_field_accessor('quarter', 'q', "The quarter of the date")
days_in_month = _tslib_field_accessor(
'days_in_month', 'dim', "The number of days in the month"
)
daysinmonth = days_in_month
7 changes: 7 additions & 0 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,10 @@ def ones_like(other, dtype=None):
"""Shorthand for full_like(other, 1, dtype)
"""
return full_like(other, 1, dtype)


def is_datetime_like(dtype):
"""Check if a dtype is a subclass of the numpy datetime types
"""
return (np.issubdtype(dtype, np.datetime64) or
np.issubdtype(dtype, np.timedelta64))
43 changes: 43 additions & 0 deletions xarray/tests/test_accessors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
try:
import cPickle as pickle
except ImportError:
import pickle

import xarray as xr
import numpy as np
import pandas as pd

from . import TestCase


class TestDatetimeAccessor(TestCase):
def setUp(self):
nt = 10000
data = np.random.rand(10, 10, nt)
lons = np.linspace(0, 11, 10)
lats = np.linspace(0, 20, 10)
self.times = pd.date_range(start="2000/01/01", freq='H', periods=nt)

self.data = xr.DataArray(data, coords=[lons, lats, self.times],
dims=['lon', 'lat', 'time'], name='data')

def test_field_access(self):
years = self.times.year
months = self.times.month
days = self.times.day
hours = self.times.hour

self.assertArrayEqual(years, self.data.time.dt.year)
Copy link
Member

Choose a reason for hiding this comment

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

Use self.assertDataArrayEqual to check that fields provide data arrays with the appropriate metadata.

self.assertArrayEqual(months, self.data.time.dt.month)
self.assertArrayEqual(days, self.data.time.dt.day)
self.assertArrayEqual(hours, self.data.time.dt.hour)

def test_not_datetime_type(self):
nontime_data = self.data.copy()
nontime_data['time'].values = \
Copy link
Member

Choose a reason for hiding this comment

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

please avoid explicit backlashes for continuation if possible, per PEP8.

np.arange(len(self.data.time)).astype('int8')
with self.assertRaisesRegexp(TypeError, 'dt'):
nontime_data.time.dt.year
Copy link
Member

Choose a reason for hiding this comment

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

missing newline on last line