def test_freq_group_match(period_code_item): freqstr, code = period_code_item str_group = resolution.get_freq_group(freqstr) code_group = resolution.get_freq_group(code) assert str_group == code_group == code // 1000 * 1000
def test_freq_group_match(period_code_item): freqstr, code = period_code_item str_group = resolution.get_freq_group(freqstr) code_group = resolution.get_freq_group(code) assert str_group == code_group == code // 1000 * 1000
def test_frequency_misc(self): assert (resolution.get_freq_group('T') == FreqGroup.FR_MIN) code, stride = get_freq_code(offsets.Hour()) assert code == FreqGroup.FR_HR code, stride = get_freq_code((5, 'T')) assert code == FreqGroup.FR_MIN assert stride == 5 offset = offsets.Hour() result = frequencies.to_offset(offset) assert result == offset result = frequencies.to_offset((5, 'T')) expected = offsets.Minute(5) assert result == expected with pytest.raises(ValueError, match='Invalid frequency'): get_freq_code((5, 'baz')) with pytest.raises(ValueError, match='Invalid frequency'): frequencies.to_offset('100foo') with pytest.raises(ValueError, match='Could not evaluate'): frequencies.to_offset(('', ''))
def test_frequency_misc(self): assert (resolution.get_freq_group('T') == frequencies.FreqGroup.FR_MIN) code, stride = frequencies.get_freq_code(offsets.Hour()) assert code == frequencies.FreqGroup.FR_HR code, stride = frequencies.get_freq_code((5, 'T')) assert code == frequencies.FreqGroup.FR_MIN assert stride == 5 offset = offsets.Hour() result = frequencies.to_offset(offset) assert result == offset result = frequencies.to_offset((5, 'T')) expected = offsets.Minute(5) assert result == expected with tm.assert_raises_regex(ValueError, 'Invalid frequency'): frequencies.get_freq_code((5, 'baz')) with tm.assert_raises_regex(ValueError, 'Invalid frequency'): frequencies.to_offset('100foo') with tm.assert_raises_regex(ValueError, 'Could not evaluate'): frequencies.to_offset(('', '')) with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): result = frequencies.get_standard_freq(offsets.Hour()) assert result == 'H'
def _get_string_slice(self, key: str, use_lhs: bool = True, use_rhs: bool = True): # TODO: Check for non-True use_lhs/use_rhs raw = key if not self.is_monotonic: raise ValueError( "Partial indexing only valid for ordered time series") parsed, reso = parse_time_string(key, self.freq) grp = resolution.Resolution.get_freq_group(reso) freqn = resolution.get_freq_group(self.freq) if not grp < freqn: # TODO: we used to also check for # reso in ["day", "hour", "minute", "second"] # why is that check not needed? raise TypeError(key) t1, t2 = self._parsed_string_to_bounds(reso, parsed) if len(self): if t2 < self.min() or t1 > self.max(): raise KeyError(raw) # Use asi8 searchsorted to avoid overhead of re-validating inputs return slice( self.asi8.searchsorted(t1.ordinal, side="left"), self.asi8.searchsorted(t2.ordinal, side="right"), )
def test_frequency_misc(self): assert (resolution.get_freq_group('T') == frequencies.FreqGroup.FR_MIN) code, stride = frequencies.get_freq_code(offsets.Hour()) assert code == frequencies.FreqGroup.FR_HR code, stride = frequencies.get_freq_code((5, 'T')) assert code == frequencies.FreqGroup.FR_MIN assert stride == 5 offset = offsets.Hour() result = frequencies.to_offset(offset) assert result == offset result = frequencies.to_offset((5, 'T')) expected = offsets.Minute(5) assert result == expected with tm.assert_raises_regex(ValueError, 'Invalid frequency'): frequencies.get_freq_code((5, 'baz')) with tm.assert_raises_regex(ValueError, 'Invalid frequency'): frequencies.to_offset('100foo') with tm.assert_raises_regex(ValueError, 'Could not evaluate'): frequencies.to_offset(('', '')) with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): result = frequencies.get_standard_freq(offsets.Hour()) assert result == 'H'
def test_frequency_misc(self): assert (resolution.get_freq_group('T') == frequencies.FreqGroup.FR_MIN) code, stride = frequencies.get_freq_code(offsets.Hour()) assert code == frequencies.FreqGroup.FR_HR code, stride = frequencies.get_freq_code((5, 'T')) assert code == frequencies.FreqGroup.FR_MIN assert stride == 5 offset = offsets.Hour() result = frequencies.to_offset(offset) assert result == offset result = frequencies.to_offset((5, 'T')) expected = offsets.Minute(5) assert result == expected with pytest.raises(ValueError, match='Invalid frequency'): frequencies.get_freq_code((5, 'baz')) with pytest.raises(ValueError, match='Invalid frequency'): frequencies.to_offset('100foo') with pytest.raises(ValueError, match='Could not evaluate'): frequencies.to_offset(('', ''))
def _validate_partial_date_slice(self, reso: str): grp = resolution.Resolution.get_freq_group(reso) freqn = resolution.get_freq_group(self.freq) if not grp < freqn: # TODO: we used to also check for # reso in ["day", "hour", "minute", "second"] # why is that check not needed? raise ValueError
def test_freq_code(self): assert get_freq('A') == 1000 assert get_freq('3A') == 1000 assert get_freq('-1A') == 1000 assert get_freq('Y') == 1000 assert get_freq('3Y') == 1000 assert get_freq('-1Y') == 1000 assert get_freq('W') == 4000 assert get_freq('W-MON') == 4001 assert get_freq('W-FRI') == 4005 for freqstr, code in compat.iteritems(_period_code_map): result = get_freq(freqstr) assert result == code result = resolution.get_freq_group(freqstr) assert result == code // 1000 * 1000 result = resolution.get_freq_group(code) assert result == code // 1000 * 1000
def test_freq_code(self): assert frequencies.get_freq('A') == 1000 assert frequencies.get_freq('3A') == 1000 assert frequencies.get_freq('-1A') == 1000 assert frequencies.get_freq('Y') == 1000 assert frequencies.get_freq('3Y') == 1000 assert frequencies.get_freq('-1Y') == 1000 assert frequencies.get_freq('W') == 4000 assert frequencies.get_freq('W-MON') == 4001 assert frequencies.get_freq('W-FRI') == 4005 for freqstr, code in compat.iteritems(_period_code_map): result = frequencies.get_freq(freqstr) assert result == code result = resolution.get_freq_group(freqstr) assert result == code // 1000 * 1000 result = resolution.get_freq_group(code) assert result == code // 1000 * 1000
def _get_string_slice(self, key): if not self.is_monotonic: raise ValueError('Partial indexing only valid for ' 'ordered time series') key, parsed, reso = parse_time_string(key, self.freq) grp = resolution.Resolution.get_freq_group(reso) freqn = resolution.get_freq_group(self.freq) if reso in ['day', 'hour', 'minute', 'second'] and not grp < freqn: raise KeyError(key) t1, t2 = self._parsed_string_to_bounds(reso, parsed) return slice(self.searchsorted(t1.ordinal, side='left'), self.searchsorted(t2.ordinal, side='right'))
def _get_string_slice(self, key): if not self.is_monotonic: raise ValueError( "Partial indexing only valid for ordered time series") key, parsed, reso = parse_time_string(key, self.freq) grp = resolution.Resolution.get_freq_group(reso) freqn = resolution.get_freq_group(self.freq) if reso in ["day", "hour", "minute", "second"] and not grp < freqn: raise KeyError(key) t1, t2 = self._parsed_string_to_bounds(reso, parsed) return slice(self.searchsorted(t1, side="left"), self.searchsorted(t2, side="right"))
def _get_string_slice(self, key): if not self.is_monotonic: raise ValueError('Partial indexing only valid for ' 'ordered time series') key, parsed, reso = parse_time_string(key, self.freq) grp = resolution.Resolution.get_freq_group(reso) freqn = resolution.get_freq_group(self.freq) if reso in ['day', 'hour', 'minute', 'second'] and not grp < freqn: raise KeyError(key) t1, t2 = self._parsed_string_to_bounds(reso, parsed) return slice(self.searchsorted(t1.ordinal, side='left'), self.searchsorted(t2.ordinal, side='right'))
def get_finder(freq): if isinstance(freq, str): freq = get_freq(freq) fgroup = resolution.get_freq_group(freq) if fgroup == FreqGroup.FR_ANN: return _annual_finder elif fgroup == FreqGroup.FR_QTR: return _quarterly_finder elif freq == FreqGroup.FR_MTH: return _monthly_finder elif (freq >= FreqGroup.FR_BUS) or fgroup == FreqGroup.FR_WK: return _daily_finder else: # pragma: no cover raise NotImplementedError(f"Unsupported frequency: {freq}")
def get_finder(freq): if isinstance(freq, str): freq = get_freq(freq) fgroup = resolution.get_freq_group(freq) if fgroup == FreqGroup.FR_ANN: return _annual_finder elif fgroup == FreqGroup.FR_QTR: return _quarterly_finder elif freq == FreqGroup.FR_MTH: return _monthly_finder elif ((freq >= FreqGroup.FR_BUS) or fgroup == FreqGroup.FR_WK): return _daily_finder else: # pragma: no cover errmsg = "Unsupported frequency: {freq}".format(freq=freq) raise NotImplementedError(errmsg)
def get_finder(freq): if isinstance(freq, compat.string_types): freq = frequencies.get_freq(freq) fgroup = resolution.get_freq_group(freq) if fgroup == FreqGroup.FR_ANN: return _annual_finder elif fgroup == FreqGroup.FR_QTR: return _quarterly_finder elif freq == FreqGroup.FR_MTH: return _monthly_finder elif ((freq >= FreqGroup.FR_BUS) or fgroup == FreqGroup.FR_WK): return _daily_finder else: # pragma: no cover errmsg = "Unsupported frequency: {freq}".format(freq=freq) raise NotImplementedError(errmsg)
def get_value(self, series, key): """ Fast lookup of value from 1-dimensional ndarray. Only use this if you know what you're doing """ s = com.values_from_object(series) try: return com.maybe_box(self, super(PeriodIndex, self).get_value(s, key), series, key) except (KeyError, IndexError): try: asdt, parsed, reso = parse_time_string(key, self.freq) grp = resolution.Resolution.get_freq_group(reso) freqn = resolution.get_freq_group(self.freq) vals = self._ndarray_values # if our data is higher resolution than requested key, slice if grp < freqn: iv = Period(asdt, freq=(grp, 1)) ord1 = iv.asfreq(self.freq, how='S').ordinal ord2 = iv.asfreq(self.freq, how='E').ordinal if ord2 < vals[0] or ord1 > vals[-1]: raise KeyError(key) pos = np.searchsorted(self._ndarray_values, [ord1, ord2]) key = slice(pos[0], pos[1] + 1) return series[key] elif grp == freqn: key = Period(asdt, freq=self.freq).ordinal return com.maybe_box(self, self._engine.get_value(s, key), series, key) else: raise KeyError(key) except TypeError: pass period = Period(key, self.freq) key = period.value if isna(period) else period.ordinal return com.maybe_box(self, self._engine.get_value(s, key), series, key)
def get_value(self, series, key): """ Fast lookup of value from 1-dimensional ndarray. Only use this if you know what you're doing """ s = com.values_from_object(series) try: return com.maybe_box(self, super(PeriodIndex, self).get_value(s, key), series, key) except (KeyError, IndexError): try: asdt, parsed, reso = parse_time_string(key, self.freq) grp = resolution.Resolution.get_freq_group(reso) freqn = resolution.get_freq_group(self.freq) vals = self._ndarray_values # if our data is higher resolution than requested key, slice if grp < freqn: iv = Period(asdt, freq=(grp, 1)) ord1 = iv.asfreq(self.freq, how='S').ordinal ord2 = iv.asfreq(self.freq, how='E').ordinal if ord2 < vals[0] or ord1 > vals[-1]: raise KeyError(key) pos = np.searchsorted(self._ndarray_values, [ord1, ord2]) key = slice(pos[0], pos[1] + 1) return series[key] elif grp == freqn: key = Period(asdt, freq=self.freq).ordinal return com.maybe_box(self, self._engine.get_value(s, key), series, key) else: raise KeyError(key) except TypeError: pass period = Period(key, self.freq) key = period.value if isna(period) else period.ordinal return com.maybe_box(self, self._engine.get_value(s, key), series, key)
def _get_string_slice(self, key: str, use_lhs: bool = True, use_rhs: bool = True): # TODO: Check for non-True use_lhs/use_rhs parsed, reso = parse_time_string(key, self.freq) grp = resolution.Resolution.get_freq_group(reso) freqn = resolution.get_freq_group(self.freq) if not grp < freqn: # TODO: we used to also check for # reso in ["day", "hour", "minute", "second"] # why is that check not needed? raise ValueError(key) t1, t2 = self._parsed_string_to_bounds(reso, parsed) i8vals = self.asi8 if self.is_monotonic: # we are out of range if len(self) and ((use_lhs and t1 < self[0] and t2 < self[0]) or ((use_rhs and t1 > self[-1] and t2 > self[-1]))): raise KeyError(key) # TODO: does this depend on being monotonic _increasing_? # If so, DTI will also be affected. # a monotonic (sorted) series can be sliced # Use asi8.searchsorted to avoid re-validating Periods left = i8vals.searchsorted(t1.ordinal, side="left") if use_lhs else None right = i8vals.searchsorted(t2.ordinal, side="right") if use_rhs else None return slice(left, right) else: lhs_mask = (i8vals >= t1.ordinal) if use_lhs else True rhs_mask = (i8vals <= t2.ordinal) if use_rhs else True # try to find a the dates return (lhs_mask & rhs_mask).nonzero()[0]
def get_value(self, series, key): """ Fast lookup of value from 1-dimensional ndarray. Only use this if you know what you're doing """ if is_integer(key): return series.iat[key] if isinstance(key, str): asdt, reso = parse_time_string(key, self.freq) grp = resolution.Resolution.get_freq_group(reso) freqn = resolution.get_freq_group(self.freq) vals = self._ndarray_values # if our data is higher resolution than requested key, slice if grp < freqn: iv = Period(asdt, freq=(grp, 1)) ord1 = iv.asfreq(self.freq, how="S").ordinal ord2 = iv.asfreq(self.freq, how="E").ordinal if ord2 < vals[0] or ord1 > vals[-1]: raise KeyError(key) pos = np.searchsorted(self._ndarray_values, [ord1, ord2]) key = slice(pos[0], pos[1] + 1) return series[key] elif grp == freqn: key = Period(asdt, freq=self.freq) loc = self.get_loc(key) return series.iloc[loc] else: raise KeyError(key) elif isinstance(key, Period) or key is NaT: ordinal = key.ordinal if key is not NaT else NaT.value loc = self._engine.get_loc(ordinal) return series[loc] # slice, PeriodIndex, np.ndarray, List[Period] value = Index.get_value(self, series, key) return com.maybe_box(self, value, series, key)
def get_value(self, series, key): """ Fast lookup of value from 1-dimensional ndarray. Only use this if you know what you're doing """ if is_integer(key): return series.iat[key] if isinstance(key, str): try: loc = self._get_string_slice(key) return series[loc] except (TypeError, ValueError): pass asdt, reso = parse_time_string(key, self.freq) grp = resolution.Resolution.get_freq_group(reso) freqn = resolution.get_freq_group(self.freq) # _get_string_slice will handle cases where grp < freqn assert grp >= freqn if grp == freqn: key = Period(asdt, freq=self.freq) loc = self.get_loc(key) return series.iloc[loc] else: raise KeyError(key) elif isinstance(key, Period) or key is NaT: ordinal = key.ordinal if key is not NaT else NaT.value loc = self._engine.get_loc(ordinal) return series[loc] # slice, PeriodIndex, np.ndarray, List[Period] value = Index.get_value(self, series, key) return com.maybe_box(self, value, series, key)
def _daily_finder(vmin, vmax, freq): periodsperday = -1 if freq >= FreqGroup.FR_HR: if freq == FreqGroup.FR_NS: periodsperday = 24 * 60 * 60 * 1000000000 elif freq == FreqGroup.FR_US: periodsperday = 24 * 60 * 60 * 1000000 elif freq == FreqGroup.FR_MS: periodsperday = 24 * 60 * 60 * 1000 elif freq == FreqGroup.FR_SEC: periodsperday = 24 * 60 * 60 elif freq == FreqGroup.FR_MIN: periodsperday = 24 * 60 elif freq == FreqGroup.FR_HR: periodsperday = 24 else: # pragma: no cover raise ValueError(f"unexpected frequency: {freq}") periodsperyear = 365 * periodsperday periodspermonth = 28 * periodsperday elif freq == FreqGroup.FR_BUS: periodsperyear = 261 periodspermonth = 19 elif freq == FreqGroup.FR_DAY: periodsperyear = 365 periodspermonth = 28 elif resolution.get_freq_group(freq) == FreqGroup.FR_WK: periodsperyear = 52 periodspermonth = 3 else: # pragma: no cover raise ValueError("unexpected frequency") # save this for later usage vmin_orig = vmin (vmin, vmax) = ( Period(ordinal=int(vmin), freq=freq), Period(ordinal=int(vmax), freq=freq), ) span = vmax.ordinal - vmin.ordinal + 1 dates_ = period_range(start=vmin, end=vmax, freq=freq) # Initialize the output info = np.zeros(span, dtype=[("val", np.int64), ("maj", bool), ("min", bool), ("fmt", "|S20")]) info["val"][:] = dates_.asi8 info["fmt"][:] = "" info["maj"][[0, -1]] = True # .. and set some shortcuts info_maj = info["maj"] info_min = info["min"] info_fmt = info["fmt"] def first_label(label_flags): if (label_flags[0] == 0) and (label_flags.size > 1) and ((vmin_orig % 1) > 0.0): return label_flags[1] else: return label_flags[0] # Case 1. Less than a month if span <= periodspermonth: day_start = period_break(dates_, "day") month_start = period_break(dates_, "month") def _hour_finder(label_interval, force_year_start): _hour = dates_.hour _prev_hour = (dates_ - 1 * dates_.freq).hour hour_start = (_hour - _prev_hour) != 0 info_maj[day_start] = True info_min[hour_start & (_hour % label_interval == 0)] = True year_start = period_break(dates_, "year") info_fmt[hour_start & (_hour % label_interval == 0)] = "%H:%M" info_fmt[day_start] = "%H:%M\n%d-%b" info_fmt[year_start] = "%H:%M\n%d-%b\n%Y" if force_year_start and not has_level_label(year_start, vmin_orig): info_fmt[first_label(day_start)] = "%H:%M\n%d-%b\n%Y" def _minute_finder(label_interval): hour_start = period_break(dates_, "hour") _minute = dates_.minute _prev_minute = (dates_ - 1 * dates_.freq).minute minute_start = (_minute - _prev_minute) != 0 info_maj[hour_start] = True info_min[minute_start & (_minute % label_interval == 0)] = True year_start = period_break(dates_, "year") info_fmt = info["fmt"] info_fmt[minute_start & (_minute % label_interval == 0)] = "%H:%M" info_fmt[day_start] = "%H:%M\n%d-%b" info_fmt[year_start] = "%H:%M\n%d-%b\n%Y" def _second_finder(label_interval): minute_start = period_break(dates_, "minute") _second = dates_.second _prev_second = (dates_ - 1 * dates_.freq).second second_start = (_second - _prev_second) != 0 info["maj"][minute_start] = True info["min"][second_start & (_second % label_interval == 0)] = True year_start = period_break(dates_, "year") info_fmt = info["fmt"] info_fmt[second_start & (_second % label_interval == 0)] = "%H:%M:%S" info_fmt[day_start] = "%H:%M:%S\n%d-%b" info_fmt[year_start] = "%H:%M:%S\n%d-%b\n%Y" if span < periodsperday / 12000.0: _second_finder(1) elif span < periodsperday / 6000.0: _second_finder(2) elif span < periodsperday / 2400.0: _second_finder(5) elif span < periodsperday / 1200.0: _second_finder(10) elif span < periodsperday / 800.0: _second_finder(15) elif span < periodsperday / 400.0: _second_finder(30) elif span < periodsperday / 150.0: _minute_finder(1) elif span < periodsperday / 70.0: _minute_finder(2) elif span < periodsperday / 24.0: _minute_finder(5) elif span < periodsperday / 12.0: _minute_finder(15) elif span < periodsperday / 6.0: _minute_finder(30) elif span < periodsperday / 2.5: _hour_finder(1, False) elif span < periodsperday / 1.5: _hour_finder(2, False) elif span < periodsperday * 1.25: _hour_finder(3, False) elif span < periodsperday * 2.5: _hour_finder(6, True) elif span < periodsperday * 4: _hour_finder(12, True) else: info_maj[month_start] = True info_min[day_start] = True year_start = period_break(dates_, "year") info_fmt = info["fmt"] info_fmt[day_start] = "%d" info_fmt[month_start] = "%d\n%b" info_fmt[year_start] = "%d\n%b\n%Y" if not has_level_label(year_start, vmin_orig): if not has_level_label(month_start, vmin_orig): info_fmt[first_label(day_start)] = "%d\n%b\n%Y" else: info_fmt[first_label(month_start)] = "%d\n%b\n%Y" # Case 2. Less than three months elif span <= periodsperyear // 4: month_start = period_break(dates_, "month") info_maj[month_start] = True if freq < FreqGroup.FR_HR: info["min"] = True else: day_start = period_break(dates_, "day") info["min"][day_start] = True week_start = period_break(dates_, "week") year_start = period_break(dates_, "year") info_fmt[week_start] = "%d" info_fmt[month_start] = "\n\n%b" info_fmt[year_start] = "\n\n%b\n%Y" if not has_level_label(year_start, vmin_orig): if not has_level_label(month_start, vmin_orig): info_fmt[first_label(week_start)] = "\n\n%b\n%Y" else: info_fmt[first_label(month_start)] = "\n\n%b\n%Y" # Case 3. Less than 14 months ............... elif span <= 1.15 * periodsperyear: year_start = period_break(dates_, "year") month_start = period_break(dates_, "month") week_start = period_break(dates_, "week") info_maj[month_start] = True info_min[week_start] = True info_min[year_start] = False info_min[month_start] = False info_fmt[month_start] = "%b" info_fmt[year_start] = "%b\n%Y" if not has_level_label(year_start, vmin_orig): info_fmt[first_label(month_start)] = "%b\n%Y" # Case 4. Less than 2.5 years ............... elif span <= 2.5 * periodsperyear: year_start = period_break(dates_, "year") quarter_start = period_break(dates_, "quarter") month_start = period_break(dates_, "month") info_maj[quarter_start] = True info_min[month_start] = True info_fmt[quarter_start] = "%b" info_fmt[year_start] = "%b\n%Y" # Case 4. Less than 4 years ................. elif span <= 4 * periodsperyear: year_start = period_break(dates_, "year") month_start = period_break(dates_, "month") info_maj[year_start] = True info_min[month_start] = True info_min[year_start] = False month_break = dates_[month_start].month jan_or_jul = month_start[(month_break == 1) | (month_break == 7)] info_fmt[jan_or_jul] = "%b" info_fmt[year_start] = "%b\n%Y" # Case 5. Less than 11 years ................ elif span <= 11 * periodsperyear: year_start = period_break(dates_, "year") quarter_start = period_break(dates_, "quarter") info_maj[year_start] = True info_min[quarter_start] = True info_min[year_start] = False info_fmt[year_start] = "%Y" # Case 6. More than 12 years ................ else: year_start = period_break(dates_, "year") year_break = dates_[year_start].year nyears = span / periodsperyear (min_anndef, maj_anndef) = _get_default_annual_spacing(nyears) major_idx = year_start[(year_break % maj_anndef == 0)] info_maj[major_idx] = True minor_idx = year_start[(year_break % min_anndef == 0)] info_min[minor_idx] = True info_fmt[major_idx] = "%Y" return info
def test_freq_group(self): assert resolution.get_freq_group('A') == 1000 assert resolution.get_freq_group('3A') == 1000 assert resolution.get_freq_group('-1A') == 1000 assert resolution.get_freq_group('A-JAN') == 1000 assert resolution.get_freq_group('A-MAY') == 1000 assert resolution.get_freq_group('Y') == 1000 assert resolution.get_freq_group('3Y') == 1000 assert resolution.get_freq_group('-1Y') == 1000 assert resolution.get_freq_group('Y-JAN') == 1000 assert resolution.get_freq_group('Y-MAY') == 1000 assert resolution.get_freq_group(offsets.YearEnd()) == 1000 assert resolution.get_freq_group(offsets.YearEnd(month=1)) == 1000 assert resolution.get_freq_group(offsets.YearEnd(month=5)) == 1000 assert resolution.get_freq_group('W') == 4000 assert resolution.get_freq_group('W-MON') == 4000 assert resolution.get_freq_group('W-FRI') == 4000 assert resolution.get_freq_group(offsets.Week()) == 4000 assert resolution.get_freq_group(offsets.Week(weekday=1)) == 4000 assert resolution.get_freq_group(offsets.Week(weekday=5)) == 4000
def get_loc(self, key, method=None, tolerance=None): """ Get integer location for requested label. Parameters ---------- key : Period, NaT, str, or datetime String or datetime key must be parseable as Period. Returns ------- loc : int or ndarray[int64] Raises ------ KeyError Key is not present in the index. TypeError If key is listlike or otherwise not hashable. """ orig_key = key if not is_scalar(key): raise InvalidIndexError(key) if isinstance(key, str): try: loc = self._get_string_slice(key) return loc except (TypeError, ValueError): pass try: asdt, reso = parse_time_string(key, self.freq) except DateParseError as err: # A string with invalid format raise KeyError(f"Cannot interpret '{key}' as period") from err grp = resolution.Resolution.get_freq_group(reso) freqn = resolution.get_freq_group(self.freq) # _get_string_slice will handle cases where grp < freqn assert grp >= freqn if grp == freqn: key = Period(asdt, freq=self.freq) loc = self.get_loc(key, method=method, tolerance=tolerance) return loc elif method is None: raise KeyError(key) else: key = asdt elif is_integer(key): # Period constructor will cast to string, which we dont want raise KeyError(key) try: key = Period(key, freq=self.freq) except ValueError as err: # we cannot construct the Period raise KeyError(orig_key) from err try: return Index.get_loc(self, key, method, tolerance) except KeyError as err: raise KeyError(orig_key) from err
def _daily_finder(vmin, vmax, freq): periodsperday = -1 if freq >= FreqGroup.FR_HR: if freq == FreqGroup.FR_NS: periodsperday = 24 * 60 * 60 * 1000000000 elif freq == FreqGroup.FR_US: periodsperday = 24 * 60 * 60 * 1000000 elif freq == FreqGroup.FR_MS: periodsperday = 24 * 60 * 60 * 1000 elif freq == FreqGroup.FR_SEC: periodsperday = 24 * 60 * 60 elif freq == FreqGroup.FR_MIN: periodsperday = 24 * 60 elif freq == FreqGroup.FR_HR: periodsperday = 24 else: # pragma: no cover raise ValueError("unexpected frequency: {freq}".format(freq=freq)) periodsperyear = 365 * periodsperday periodspermonth = 28 * periodsperday elif freq == FreqGroup.FR_BUS: periodsperyear = 261 periodspermonth = 19 elif freq == FreqGroup.FR_DAY: periodsperyear = 365 periodspermonth = 28 elif resolution.get_freq_group(freq) == FreqGroup.FR_WK: periodsperyear = 52 periodspermonth = 3 else: # pragma: no cover raise ValueError("unexpected frequency") # save this for later usage vmin_orig = vmin (vmin, vmax) = (Period(ordinal=int(vmin), freq=freq), Period(ordinal=int(vmax), freq=freq)) span = vmax.ordinal - vmin.ordinal + 1 dates_ = period_range(start=vmin, end=vmax, freq=freq) # Initialize the output info = np.zeros(span, dtype=[('val', np.int64), ('maj', bool), ('min', bool), ('fmt', '|S20')]) info['val'][:] = dates_._ndarray_values info['fmt'][:] = '' info['maj'][[0, -1]] = True # .. and set some shortcuts info_maj = info['maj'] info_min = info['min'] info_fmt = info['fmt'] def first_label(label_flags): if (label_flags[0] == 0) and (label_flags.size > 1) and \ ((vmin_orig % 1) > 0.0): return label_flags[1] else: return label_flags[0] # Case 1. Less than a month if span <= periodspermonth: day_start = period_break(dates_, 'day') month_start = period_break(dates_, 'month') def _hour_finder(label_interval, force_year_start): _hour = dates_.hour _prev_hour = (dates_ - 1 * dates_.freq).hour hour_start = (_hour - _prev_hour) != 0 info_maj[day_start] = True info_min[hour_start & (_hour % label_interval == 0)] = True year_start = period_break(dates_, 'year') info_fmt[hour_start & (_hour % label_interval == 0)] = '%H:%M' info_fmt[day_start] = '%H:%M\n%d-%b' info_fmt[year_start] = '%H:%M\n%d-%b\n%Y' if force_year_start and not has_level_label(year_start, vmin_orig): info_fmt[first_label(day_start)] = '%H:%M\n%d-%b\n%Y' def _minute_finder(label_interval): hour_start = period_break(dates_, 'hour') _minute = dates_.minute _prev_minute = (dates_ - 1 * dates_.freq).minute minute_start = (_minute - _prev_minute) != 0 info_maj[hour_start] = True info_min[minute_start & (_minute % label_interval == 0)] = True year_start = period_break(dates_, 'year') info_fmt = info['fmt'] info_fmt[minute_start & (_minute % label_interval == 0)] = '%H:%M' info_fmt[day_start] = '%H:%M\n%d-%b' info_fmt[year_start] = '%H:%M\n%d-%b\n%Y' def _second_finder(label_interval): minute_start = period_break(dates_, 'minute') _second = dates_.second _prev_second = (dates_ - 1 * dates_.freq).second second_start = (_second - _prev_second) != 0 info['maj'][minute_start] = True info['min'][second_start & (_second % label_interval == 0)] = True year_start = period_break(dates_, 'year') info_fmt = info['fmt'] info_fmt[second_start & (_second % label_interval == 0)] = '%H:%M:%S' info_fmt[day_start] = '%H:%M:%S\n%d-%b' info_fmt[year_start] = '%H:%M:%S\n%d-%b\n%Y' if span < periodsperday / 12000.0: _second_finder(1) elif span < periodsperday / 6000.0: _second_finder(2) elif span < periodsperday / 2400.0: _second_finder(5) elif span < periodsperday / 1200.0: _second_finder(10) elif span < periodsperday / 800.0: _second_finder(15) elif span < periodsperday / 400.0: _second_finder(30) elif span < periodsperday / 150.0: _minute_finder(1) elif span < periodsperday / 70.0: _minute_finder(2) elif span < periodsperday / 24.0: _minute_finder(5) elif span < periodsperday / 12.0: _minute_finder(15) elif span < periodsperday / 6.0: _minute_finder(30) elif span < periodsperday / 2.5: _hour_finder(1, False) elif span < periodsperday / 1.5: _hour_finder(2, False) elif span < periodsperday * 1.25: _hour_finder(3, False) elif span < periodsperday * 2.5: _hour_finder(6, True) elif span < periodsperday * 4: _hour_finder(12, True) else: info_maj[month_start] = True info_min[day_start] = True year_start = period_break(dates_, 'year') info_fmt = info['fmt'] info_fmt[day_start] = '%d' info_fmt[month_start] = '%d\n%b' info_fmt[year_start] = '%d\n%b\n%Y' if not has_level_label(year_start, vmin_orig): if not has_level_label(month_start, vmin_orig): info_fmt[first_label(day_start)] = '%d\n%b\n%Y' else: info_fmt[first_label(month_start)] = '%d\n%b\n%Y' # Case 2. Less than three months elif span <= periodsperyear // 4: month_start = period_break(dates_, 'month') info_maj[month_start] = True if freq < FreqGroup.FR_HR: info['min'] = True else: day_start = period_break(dates_, 'day') info['min'][day_start] = True week_start = period_break(dates_, 'week') year_start = period_break(dates_, 'year') info_fmt[week_start] = '%d' info_fmt[month_start] = '\n\n%b' info_fmt[year_start] = '\n\n%b\n%Y' if not has_level_label(year_start, vmin_orig): if not has_level_label(month_start, vmin_orig): info_fmt[first_label(week_start)] = '\n\n%b\n%Y' else: info_fmt[first_label(month_start)] = '\n\n%b\n%Y' # Case 3. Less than 14 months ............... elif span <= 1.15 * periodsperyear: year_start = period_break(dates_, 'year') month_start = period_break(dates_, 'month') week_start = period_break(dates_, 'week') info_maj[month_start] = True info_min[week_start] = True info_min[year_start] = False info_min[month_start] = False info_fmt[month_start] = '%b' info_fmt[year_start] = '%b\n%Y' if not has_level_label(year_start, vmin_orig): info_fmt[first_label(month_start)] = '%b\n%Y' # Case 4. Less than 2.5 years ............... elif span <= 2.5 * periodsperyear: year_start = period_break(dates_, 'year') quarter_start = period_break(dates_, 'quarter') month_start = period_break(dates_, 'month') info_maj[quarter_start] = True info_min[month_start] = True info_fmt[quarter_start] = '%b' info_fmt[year_start] = '%b\n%Y' # Case 4. Less than 4 years ................. elif span <= 4 * periodsperyear: year_start = period_break(dates_, 'year') month_start = period_break(dates_, 'month') info_maj[year_start] = True info_min[month_start] = True info_min[year_start] = False month_break = dates_[month_start].month jan_or_jul = month_start[(month_break == 1) | (month_break == 7)] info_fmt[jan_or_jul] = '%b' info_fmt[year_start] = '%b\n%Y' # Case 5. Less than 11 years ................ elif span <= 11 * periodsperyear: year_start = period_break(dates_, 'year') quarter_start = period_break(dates_, 'quarter') info_maj[year_start] = True info_min[quarter_start] = True info_min[year_start] = False info_fmt[year_start] = '%Y' # Case 6. More than 12 years ................ else: year_start = period_break(dates_, 'year') year_break = dates_[year_start].year nyears = span / periodsperyear (min_anndef, maj_anndef) = _get_default_annual_spacing(nyears) major_idx = year_start[(year_break % maj_anndef == 0)] info_maj[major_idx] = True minor_idx = year_start[(year_break % min_anndef == 0)] info_min[minor_idx] = True info_fmt[major_idx] = '%Y' return info
def test_freq_group(self): assert resolution.get_freq_group('A') == 1000 assert resolution.get_freq_group('3A') == 1000 assert resolution.get_freq_group('-1A') == 1000 assert resolution.get_freq_group('A-JAN') == 1000 assert resolution.get_freq_group('A-MAY') == 1000 assert resolution.get_freq_group('Y') == 1000 assert resolution.get_freq_group('3Y') == 1000 assert resolution.get_freq_group('-1Y') == 1000 assert resolution.get_freq_group('Y-JAN') == 1000 assert resolution.get_freq_group('Y-MAY') == 1000 assert resolution.get_freq_group(offsets.YearEnd()) == 1000 assert resolution.get_freq_group(offsets.YearEnd(month=1)) == 1000 assert resolution.get_freq_group(offsets.YearEnd(month=5)) == 1000 assert resolution.get_freq_group('W') == 4000 assert resolution.get_freq_group('W-MON') == 4000 assert resolution.get_freq_group('W-FRI') == 4000 assert resolution.get_freq_group(offsets.Week()) == 4000 assert resolution.get_freq_group(offsets.Week(weekday=1)) == 4000 assert resolution.get_freq_group(offsets.Week(weekday=5)) == 4000
def test_freq_group(freqstr, expected): assert resolution.get_freq_group(freqstr) == expected
def get_loc(self, key, method=None, tolerance=None): """ Get integer location for requested label. Parameters ---------- key : Period, NaT, str, or datetime String or datetime key must be parseable as Period. Returns ------- loc : int or ndarray[int64] Raises ------ KeyError Key is not present in the index. TypeError If key is listlike or otherwise not hashable. """ if isinstance(key, str): try: loc = self._get_string_slice(key) return loc except (TypeError, ValueError): pass try: asdt, reso = parse_time_string(key, self.freq) except DateParseError: # A string with invalid format raise KeyError(f"Cannot interpret '{key}' as period") grp = resolution.Resolution.get_freq_group(reso) freqn = resolution.get_freq_group(self.freq) # _get_string_slice will handle cases where grp < freqn assert grp >= freqn if grp == freqn: key = Period(asdt, freq=self.freq) loc = self.get_loc(key, method=method, tolerance=tolerance) return loc elif method is None: raise KeyError(key) else: key = asdt elif is_integer(key): # Period constructor will cast to string, which we dont want raise KeyError(key) try: key = Period(key, freq=self.freq) except ValueError: # we cannot construct the Period # as we have an invalid type if is_list_like(key): raise TypeError(f"'{key}' is an invalid key") raise KeyError(key) ordinal = key.ordinal if key is not NaT else key.value try: return self._engine.get_loc(ordinal) except KeyError: try: if tolerance is not None: tolerance = self._convert_tolerance( tolerance, np.asarray(key)) return self._int64index.get_loc(ordinal, method, tolerance) except KeyError: raise KeyError(key)
def test_freq_group(freqstr, expected): assert resolution.get_freq_group(freqstr) == expected