Skip to content

Commit

Permalink
CLN: replace %s syntax with .format in pandas.tseries (#17290)
Browse files Browse the repository at this point in the history
  • Loading branch information
jschendel authored and jreback committed Aug 19, 2017
1 parent 7818486 commit 4e9c0d1
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 84 deletions.
38 changes: 21 additions & 17 deletions pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,17 @@ def _get_freq_str(base, mult=1):
need_suffix = ['QS', 'BQ', 'BQS', 'YS', 'AS', 'BY', 'BA', 'BYS', 'BAS']
for __prefix in need_suffix:
for _m in tslib._MONTHS:
_offset_to_period_map['%s-%s' % (__prefix, _m)] = \
_offset_to_period_map[__prefix]
_alias = '{prefix}-{month}'.format(prefix=__prefix, month=_m)
_offset_to_period_map[_alias] = _offset_to_period_map[__prefix]
for __prefix in ['A', 'Q']:
for _m in tslib._MONTHS:
_alias = '%s-%s' % (__prefix, _m)
_alias = '{prefix}-{month}'.format(prefix=__prefix, month=_m)
_offset_to_period_map[_alias] = _alias

_days = ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN']
for _d in _days:
_offset_to_period_map['W-%s' % _d] = 'W-%s' % _d
_alias = 'W-{day}'.format(day=_d)
_offset_to_period_map[_alias] = _alias


def get_period_alias(offset_str):
Expand Down Expand Up @@ -587,7 +588,7 @@ def _base_and_stride(freqstr):
groups = opattern.match(freqstr)

if not groups:
raise ValueError("Could not evaluate %s" % freqstr)
raise ValueError("Could not evaluate {freq}".format(freq=freqstr))

stride = groups.group(1)

Expand Down Expand Up @@ -775,8 +776,8 @@ def infer_freq(index, warn=True):
if not (is_datetime64_dtype(values) or
is_timedelta64_dtype(values) or
values.dtype == object):
raise TypeError("cannot infer freq from a non-convertible "
"dtype on a Series of {0}".format(index.dtype))
raise TypeError("cannot infer freq from a non-convertible dtype "
"on a Series of {dtype}".format(dtype=index.dtype))
index = values

if is_period_arraylike(index):
Expand All @@ -789,7 +790,7 @@ def infer_freq(index, warn=True):
if isinstance(index, pd.Index) and not isinstance(index, pd.DatetimeIndex):
if isinstance(index, (pd.Int64Index, pd.Float64Index)):
raise TypeError("cannot infer freq from a non-convertible index "
"type {0}".format(type(index)))
"type {type}".format(type=type(index)))
index = index.values

if not isinstance(index, pd.DatetimeIndex):
Expand Down Expand Up @@ -956,15 +957,17 @@ def _infer_daily_rule(self):
if annual_rule:
nyears = self.ydiffs[0]
month = _month_aliases[self.rep_stamp.month]
return _maybe_add_count('%s-%s' % (annual_rule, month), nyears)
alias = '{prefix}-{month}'.format(prefix=annual_rule, month=month)
return _maybe_add_count(alias, nyears)

quarterly_rule = self._get_quarterly_rule()
if quarterly_rule:
nquarters = self.mdiffs[0] / 3
mod_dict = {0: 12, 2: 11, 1: 10}
month = _month_aliases[mod_dict[self.rep_stamp.month % 3]]
return _maybe_add_count('%s-%s' % (quarterly_rule, month),
nquarters)
alias = '{prefix}-{month}'.format(prefix=quarterly_rule,
month=month)
return _maybe_add_count(alias, nquarters)

monthly_rule = self._get_monthly_rule()
if monthly_rule:
Expand All @@ -974,8 +977,8 @@ def _infer_daily_rule(self):
days = self.deltas[0] / _ONE_DAY
if days % 7 == 0:
# Weekly
alias = _weekday_rule_aliases[self.rep_stamp.weekday()]
return _maybe_add_count('W-%s' % alias, days / 7)
day = _weekday_rule_aliases[self.rep_stamp.weekday()]
return _maybe_add_count('W-{day}'.format(day=day), days / 7)
else:
return _maybe_add_count('D', days)

Expand Down Expand Up @@ -1048,7 +1051,7 @@ def _get_wom_rule(self):
week = week_of_months[0] + 1
wd = _weekday_rule_aliases[weekdays[0]]

return 'WOM-%d%s' % (week, wd)
return 'WOM-{week}{weekday}'.format(week=week, weekday=wd)


class _TimedeltaFrequencyInferer(_FrequencyInferer):
Expand All @@ -1058,15 +1061,16 @@ def _infer_daily_rule(self):
days = self.deltas[0] / _ONE_DAY
if days % 7 == 0:
# Weekly
alias = _weekday_rule_aliases[self.rep_stamp.weekday()]
return _maybe_add_count('W-%s' % alias, days / 7)
wd = _weekday_rule_aliases[self.rep_stamp.weekday()]
alias = 'W-{weekday}'.format(weekday=wd)
return _maybe_add_count(alias, days / 7)
else:
return _maybe_add_count('D', days)


def _maybe_add_count(base, count):
if count != 1:
return '%d%s' % (count, base)
return '{count}{base}'.format(count=int(count), base=base)
else:
return base

Expand Down
14 changes: 7 additions & 7 deletions pandas/tseries/holiday.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,16 @@ class from pandas.tseries.offsets
def __repr__(self):
info = ''
if self.year is not None:
info += 'year=%s, ' % self.year
info += 'month=%s, day=%s, ' % (self.month, self.day)
info += 'year={year}, '.format(year=self.year)
info += 'month={mon}, day={day}, '.format(mon=self.month, day=self.day)

if self.offset is not None:
info += 'offset=%s' % self.offset
info += 'offset={offset}'.format(offset=self.offset)

if self.observance is not None:
info += 'observance=%s' % self.observance
info += 'observance={obs}'.format(obs=self.observance)

repr = 'Holiday: %s (%s)' % (self.name, info)
repr = 'Holiday: {name} ({info})'.format(name=self.name, info=info)
return repr

def dates(self, start_date, end_date, return_name=False):
Expand Down Expand Up @@ -374,8 +374,8 @@ def holidays(self, start=None, end=None, return_name=False):
DatetimeIndex of holidays
"""
if self.rules is None:
raise Exception('Holiday Calendar %s does not have any '
'rules specified' % self.name)
raise Exception('Holiday Calendar {name} does not have any '
'rules specified'.format(name=self.name))

if start is None:
start = AbstractHolidayCalendar.start_date
Expand Down
Loading

0 comments on commit 4e9c0d1

Please sign in to comment.