-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API: Implement set_freq for DTI/TDI, deprecate freq setter #20892
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -250,6 +250,7 @@ class DatetimeIndex(DatelikeOps, TimelikeOps, DatetimeIndexOpsMixin, | |
normalize | ||
strftime | ||
snap | ||
set_freq | ||
tz_convert | ||
tz_localize | ||
round | ||
|
@@ -319,7 +320,7 @@ def _add_comparison_methods(cls): | |
_other_ops = ['date', 'time'] | ||
_datetimelike_ops = _field_ops + _object_ops + _bool_ops + _other_ops | ||
_datetimelike_methods = ['to_period', 'tz_localize', | ||
'tz_convert', | ||
'tz_convert', 'set_freq', | ||
'normalize', 'strftime', 'round', 'floor', | ||
'ceil', 'month_name', 'day_name'] | ||
|
||
|
@@ -460,7 +461,7 @@ def __new__(cls, data=None, | |
if freq_infer: | ||
inferred = subarr.inferred_freq | ||
if inferred: | ||
subarr.freq = to_offset(inferred) | ||
subarr._freq = to_offset(inferred) | ||
|
||
return subarr._deepcopy_if_needed(ref_to_data, copy) | ||
|
||
|
@@ -759,7 +760,7 @@ def _cached_range(cls, start=None, end=None, periods=None, freq=None, | |
arr = tools.to_datetime(list(xdr), box=False) | ||
|
||
cachedRange = DatetimeIndex._simple_new(arr) | ||
cachedRange.freq = freq | ||
cachedRange._freq = freq | ||
cachedRange = cachedRange.tz_localize(None) | ||
cachedRange.name = None | ||
drc[freq] = cachedRange | ||
|
@@ -794,7 +795,7 @@ def _cached_range(cls, start=None, end=None, periods=None, freq=None, | |
|
||
indexSlice = cachedRange[startLoc:endLoc] | ||
indexSlice.name = name | ||
indexSlice.freq = freq | ||
indexSlice._freq = freq | ||
|
||
return indexSlice | ||
|
||
|
@@ -1184,7 +1185,7 @@ def union(self, other): | |
result._tz = timezones.tz_standardize(this.tz) | ||
if (result.freq is None and | ||
(this.freq is not None or other.freq is not None)): | ||
result.freq = to_offset(result.inferred_freq) | ||
result._freq = to_offset(result.inferred_freq) | ||
return result | ||
|
||
def to_perioddelta(self, freq): | ||
|
@@ -1232,7 +1233,7 @@ def union_many(self, others): | |
this._tz = timezones.tz_standardize(tz) | ||
|
||
if this.freq is None: | ||
this.freq = to_offset(this.inferred_freq) | ||
this._freq = to_offset(this.inferred_freq) | ||
return this | ||
|
||
def join(self, other, how='left', level=None, return_indexers=False, | ||
|
@@ -1393,7 +1394,7 @@ def intersection(self, other): | |
result = Index.intersection(self, other) | ||
if isinstance(result, DatetimeIndex): | ||
if result.freq is None: | ||
result.freq = to_offset(result.inferred_freq) | ||
result._freq = to_offset(result.inferred_freq) | ||
return result | ||
|
||
elif (other.freq is None or self.freq is None or | ||
|
@@ -1404,7 +1405,7 @@ def intersection(self, other): | |
result = self._shallow_copy(result._values, name=result.name, | ||
tz=result.tz, freq=None) | ||
if result.freq is None: | ||
result.freq = to_offset(result.inferred_freq) | ||
result._freq = to_offset(result.inferred_freq) | ||
return result | ||
|
||
if len(self) == 0: | ||
|
@@ -1738,9 +1739,13 @@ def offset(self): | |
def offset(self, value): | ||
"""get/set the frequency of the Index""" | ||
msg = ('DatetimeIndex.offset has been deprecated and will be removed ' | ||
'in a future version; use DatetimeIndex.freq instead.') | ||
'in a future version; use DatetimeIndex.set_freq instead.') | ||
warnings.warn(msg, FutureWarning, stacklevel=2) | ||
self.freq = value | ||
if value is not None: | ||
value = to_offset(value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. call set_freq here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is for the deprecated |
||
self._validate_frequency(self, value) | ||
|
||
self._freq = value | ||
|
||
year = _field_accessor('year', 'Y', "The year of the datetime") | ||
month = _field_accessor('month', 'M', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -396,6 +396,7 @@ def test_misc(self): | |
assert len(dr) == 20 | ||
assert dr[0] == firstDate | ||
assert dr[-1] == end | ||
assert dr.freq == BDay() | ||
|
||
def test_date_parse_failure(self): | ||
badly_formed_date = '2007/100/1' | ||
|
@@ -416,7 +417,6 @@ def test_daterange_bug_456(self): | |
# GH #456 | ||
rng1 = bdate_range('12/5/2011', '12/5/2011') | ||
rng2 = bdate_range('12/2/2011', '12/5/2011') | ||
rng2.freq = BDay() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is redundant, as |
||
|
||
result = rng1.union(rng2) | ||
assert isinstance(result, DatetimeIndex) | ||
|
@@ -658,12 +658,12 @@ def test_misc(self): | |
assert len(dr) == 20 | ||
assert dr[0] == firstDate | ||
assert dr[-1] == end | ||
assert dr.freq == CDay() | ||
|
||
def test_daterange_bug_456(self): | ||
# GH #456 | ||
rng1 = bdate_range('12/5/2011', '12/5/2011', freq='C') | ||
rng2 = bdate_range('12/2/2011', '12/5/2011', freq='C') | ||
rng2.freq = CDay() | ||
|
||
result = rng1.union(rng2) | ||
assert isinstance(result, DatetimeIndex) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you make this available on the .dt accessors as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This currently does nothing when used with the
dt
accessor due to #20937; the underlying frequency gets changed withset_freq
, but uponSeries
construction the new frequency gets lost.I've pushed a commit to add this to the
dt
accessor, and xfailed the corresponding tests I've added. Can revert the commit if this is no longer deemed important.