Skip to content

Commit

Permalink
Update xr version (#92)
Browse files Browse the repository at this point in the history
* update reqs to xarray v0.16.0

* remove infer_freq code; replace with xr.infer_freq
  • Loading branch information
bradyrx committed Jul 17, 2020
1 parent 0013fba commit 51de777
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 213 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -2,6 +2,14 @@
Changelog History
=================

esmtools v1.1.3 (2020-##-##)
============================

Internals/Minor Fixes
---------------------
- Update required ``xarray`` version to v0.16.0 to allow for use of
``xr.infer_freq``. (:pr:`92`) `Riley X. Brady`_.

esmtools v1.1.2 (2020-07-09)
============================

Expand Down
2 changes: 1 addition & 1 deletion ci/environment-dev-3.6.yml
Expand Up @@ -19,7 +19,7 @@ dependencies:
- bottleneck
- dask
- numpy
- xarray
- xarray>=0.16.0
# Package Management
- black
- coveralls
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Expand Up @@ -9,4 +9,4 @@ sphinx
sphinxcontrib-napoleon
sphinx_rtd_theme
toolz
xarray>=0.15.1
xarray>=0.16.0
211 changes: 1 addition & 210 deletions esmtools/timeutils.py
@@ -1,6 +1,5 @@
import cftime
import numpy as np
import pandas as pd
import xarray as xr
from xarray.core.common import contains_cftime_datetimes, is_np_datetime_like

Expand All @@ -27,7 +26,7 @@ def calendar(self):
@property
def freq(self):
if self.is_temporal:
return infer_freq(self._obj)
return xr.infer_freq(self._obj)
else:
return None

Expand Down Expand Up @@ -257,211 +256,3 @@ def leap_year(year, calendar='standard'):
):
leap = False
return leap


def infer_freq(index):
"""NOTE: This is pulled from xarray v0.15.2, which isn't released yet. I want
to avoid making a requirement the master unreleased branch. We'll switch this
simply to `xr.infer_freq()` once it's released."""
from xarray.core.dataarray import DataArray

if isinstance(index, (DataArray, pd.Series)):
dtype = np.asarray(index).dtype
if dtype == 'datetime64[ns]':
index = pd.DatetimeIndex(index.values)
elif dtype == 'timedelta64[ns]':
index = pd.TimedeltaIndex(index.values)
else:
index = xr.CFTimeIndex(index.values)

if isinstance(index, xr.CFTimeIndex):
inferer = _CFTimeFrequencyInferer(index)
return inferer.get_freq()

return pd.infer_freq(index)


_ONE_MICRO = 1
_ONE_MILLI = _ONE_MICRO * 1000
_ONE_SECOND = _ONE_MILLI * 1000
_ONE_MINUTE = 60 * _ONE_SECOND
_ONE_HOUR = 60 * _ONE_MINUTE
_ONE_DAY = 24 * _ONE_HOUR
_MONTH_ABBREVIATIONS = {
1: 'JAN',
2: 'FEB',
3: 'MAR',
4: 'APR',
5: 'MAY',
6: 'JUN',
7: 'JUL',
8: 'AUG',
9: 'SEP',
10: 'OCT',
11: 'NOV',
12: 'DEC',
}


class _CFTimeFrequencyInferer:
def __init__(self, index):
self.index = index
self.values = index.asi8

if len(index) < 3:
raise ValueError('Need at least 3 dates to infer frequency')

self.is_monotonic = (
self.index.is_monotonic_decreasing or self.index.is_monotonic_increasing
)

self._deltas = None
self._year_deltas = None
self._month_deltas = None

def get_freq(self):
if not self.is_monotonic or not self.index.is_unique:
return None

delta = self.deltas[0] # Smallest delta
if _is_multiple(delta, _ONE_DAY):
return self._infer_daily_rule()
# There is no possible intraday frequency with a non-unique delta
# Different from pandas: we don't need to manage DST and business offsets
# in cftime
elif not len(self.deltas) == 1:
return None

if _is_multiple(delta, _ONE_HOUR):
return _maybe_add_count('H', delta / _ONE_HOUR)
elif _is_multiple(delta, _ONE_MINUTE):
return _maybe_add_count('T', delta / _ONE_MINUTE)
elif _is_multiple(delta, _ONE_SECOND):
return _maybe_add_count('S', delta / _ONE_SECOND)
elif _is_multiple(delta, _ONE_MILLI):
return _maybe_add_count('L', delta / _ONE_MILLI)
else:
return _maybe_add_count('U', delta / _ONE_MICRO)

def _infer_daily_rule(self):
annual_rule = self._get_annual_rule()
if annual_rule:
nyears = self.year_deltas[0]
month = _MONTH_ABBREVIATIONS[self.index[0].month]
alias = f'{annual_rule}-{month}'
return _maybe_add_count(alias, nyears)

quartely_rule = self._get_quartely_rule()
if quartely_rule:
nquarters = self.month_deltas[0] / 3
mod_dict = {0: 12, 2: 11, 1: 10}
month = _MONTH_ABBREVIATIONS[mod_dict[self.index[0].month % 3]]
alias = f'{quartely_rule}-{month}'
return _maybe_add_count(alias, nquarters)

monthly_rule = self._get_monthly_rule()
if monthly_rule:
return _maybe_add_count(monthly_rule, self.month_deltas[0])

if len(self.deltas) == 1:
# Daily as there is no "Weekly" offsets with CFTime
days = self.deltas[0] / _ONE_DAY
return _maybe_add_count('D', days)

# CFTime has no business freq and no "week of month" (WOM)
return None

def _get_annual_rule(self):
if len(self.year_deltas) > 1:
return None

if len(np.unique(self.index.month)) > 1:
return None

return {'cs': 'AS', 'ce': 'A'}.get(month_anchor_check(self.index))

def _get_quartely_rule(self):
if len(self.month_deltas) > 1:
return None

if not self.month_deltas[0] % 3 == 0:
return None

return {'cs': 'QS', 'ce': 'Q'}.get(month_anchor_check(self.index))

def _get_monthly_rule(self):
if len(self.month_deltas) > 1:
return None

return {'cs': 'MS', 'ce': 'M'}.get(month_anchor_check(self.index))

@property
def deltas(self):
"""Sorted unique timedeltas as microseconds."""
if self._deltas is None:
self._deltas = _unique_deltas(self.values)
return self._deltas

@property
def year_deltas(self):
"""Sorted unique year deltas."""
if self._year_deltas is None:
self._year_deltas = _unique_deltas(self.index.year)
return self._year_deltas

@property
def month_deltas(self):
"""Sorted unique month deltas."""
if self._month_deltas is None:
self._month_deltas = _unique_deltas(self.index.year * 12 + self.index.month)
return self._month_deltas


def _is_multiple(us, mult: int):
"""Whether us is a multiple of mult"""
return us % mult == 0


def _maybe_add_count(base: str, count: float):
"""If count is greater than 1, add it to the base offset string"""
if count != 1:
assert count == int(count)
count = int(count)
return f'{count}{base}'
else:
return base


def month_anchor_check(dates):
"""Return the monthly offset string.
Return "cs" if all dates are the first days of the month,
"ce" if all dates are the last day of the month,
None otherwise.
Replicated pandas._libs.tslibs.resolution.month_position_check
but without business offset handling.
"""
calendar_end = True
calendar_start = True

for date in dates:
if calendar_start:
calendar_start &= date.day == 1

if calendar_end:
cal = date.day == date.daysinmonth
if calendar_end:
calendar_end &= cal
elif not calendar_start:
break

if calendar_end:
return 'ce'
elif calendar_start:
return 'cs'
else:
return None


def _unique_deltas(arr):
"""Sorted unique deltas of numpy array"""
return np.sort(np.unique(np.diff(arr)))
2 changes: 1 addition & 1 deletion requirements.txt
Expand Up @@ -3,5 +3,5 @@ numpy
scipy
statsmodels
tqdm
xarray>=0.15.1
xarray>=0.16.0
xskillscore

0 comments on commit 51de777

Please sign in to comment.