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

STY: F-strings and repr #29938

Merged
merged 2 commits into from
Dec 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 3 additions & 4 deletions pandas/tests/tseries/offsets/test_fiscal.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ def test_get_offset():

for name, expected in pairs:
offset = get_offset(name)
assert (
offset == expected
), "Expected {name!r} to yield {expected!r} (actual: {offset!r})".format(
name=name, expected=expected, offset=offset
assert offset == expected, (
f"Expected {repr(name)} to yield {repr(expected)} "
f"(actual: {repr(offset)})"
)


Expand Down
11 changes: 5 additions & 6 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3969,10 +3969,9 @@ def test_get_offset():

for name, expected in pairs:
offset = get_offset(name)
assert (
offset == expected
), "Expected {name!r} to yield {expected!r} (actual: {offset!r})".format(
name=name, expected=expected, offset=offset
assert offset == expected, (
f"Expected {repr(name)} to yield {repr(expected)} "
f"(actual: {repr(offset)})"
)


Expand Down Expand Up @@ -4170,9 +4169,9 @@ def _test_offset(self, offset_name, offset_n, tstart, expected_utc_offset):

def _make_timestamp(self, string, hrs_offset, tz):
if hrs_offset >= 0:
offset_string = "{hrs:02d}00".format(hrs=hrs_offset)
offset_string = f"{hrs_offset:02d}00"
else:
offset_string = "-{hrs:02d}00".format(hrs=-1 * hrs_offset)
offset_string = f"-{(hrs_offset * -1):02}00"
return Timestamp(string + offset_string).tz_convert(tz)

def test_springforward_plural(self):
Expand Down
107 changes: 38 additions & 69 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ def apply_index(self, i):
kwd = set(kwds) - relativedelta_fast
raise NotImplementedError(
"DateOffset with relativedelta "
"keyword(s) {kwd} not able to be "
"applied vectorized".format(kwd=kwd)
f"keyword(s) {kwd} not able to be "
"applied vectorized"
)

def isAnchored(self):
Expand All @@ -379,7 +379,7 @@ def _repr_attrs(self):
continue
elif attr not in exclude:
value = getattr(self, attr)
attrs.append("{attr}={value}".format(attr=attr, value=value))
attrs.append(f"{attr}={value}")

out = ""
if attrs:
Expand Down Expand Up @@ -449,7 +449,7 @@ def freqstr(self):
return repr(self)

if self.n != 1:
fstr = "{n}{code}".format(n=self.n, code=code)
fstr = f"{self.n}{code}"
else:
fstr = code

Expand All @@ -467,15 +467,15 @@ def _offset_str(self):

@property
def nanos(self):
raise ValueError("{name} is a non-fixed frequency".format(name=self))
raise ValueError(f"{self} is a non-fixed frequency")


class SingleConstructorOffset(DateOffset):
@classmethod
def _from_name(cls, suffix=None):
# default _from_name calls cls with no args
if suffix:
raise ValueError("Bad freq suffix {suffix}".format(suffix=suffix))
raise ValueError(f"Bad freq suffix {suffix}")
return cls()


Expand Down Expand Up @@ -513,7 +513,7 @@ def offset(self):

def _repr_attrs(self):
if self.offset:
attrs = ["offset={offset!r}".format(offset=self.offset)]
attrs = [f"offset={repr(self.offset)}"]
else:
attrs = None
out = ""
Expand Down Expand Up @@ -966,10 +966,10 @@ def _onOffset(self, dt):
def _repr_attrs(self):
out = super()._repr_attrs()
hours = ",".join(
"{}-{}".format(st.strftime("%H:%M"), en.strftime("%H:%M"))
f'{st.strftime("%H:%M")}-{en.strftime("%H:%M")}'
for st, en in zip(self.start, self.end)
)
attrs = ["{prefix}={hours}".format(prefix=self._prefix, hours=hours)]
attrs = [f"{self._prefix}={hours}"]
out += ": " + ", ".join(attrs)
return out

Expand Down Expand Up @@ -1113,7 +1113,7 @@ def name(self):
return self.rule_code
else:
month = ccalendar.MONTH_ALIASES[self.n]
return "{code}-{month}".format(code=self.rule_code, month=month)
return f"{self.code_rule}-{month}"

def onOffset(self, dt):
if self.normalize and not _is_normalized(dt):
Expand Down Expand Up @@ -1296,9 +1296,10 @@ def __init__(self, n=1, normalize=False, day_of_month=None):
else:
object.__setattr__(self, "day_of_month", int(day_of_month))
if not self._min_day_of_month <= self.day_of_month <= 27:
msg = "day_of_month must be {min}<=day_of_month<=27, got {day}"
raise ValueError(
msg.format(min=self._min_day_of_month, day=self.day_of_month)
"day_of_month must be "
f"{self._min_day_of_month}<=day_of_month<=27, "
f"got {self.day_of_month}"
)

@classmethod
Expand All @@ -1307,7 +1308,7 @@ def _from_name(cls, suffix=None):

@property
def rule_code(self):
suffix = "-{day_of_month}".format(day_of_month=self.day_of_month)
suffix = f"-{self.day_of_month}"
return self._prefix + suffix

@apply_wraps
Expand Down Expand Up @@ -1527,9 +1528,7 @@ def __init__(self, n=1, normalize=False, weekday=None):

if self.weekday is not None:
if self.weekday < 0 or self.weekday > 6:
raise ValueError(
"Day must be 0<=day<=6, got {day}".format(day=self.weekday)
)
raise ValueError(f"Day must be 0<=day<=6, got {self.weekday}")

def isAnchored(self):
return self.n == 1 and self.weekday is not None
Expand All @@ -1541,9 +1540,7 @@ def apply(self, other):

if not isinstance(other, datetime):
raise TypeError(
"Cannot add {typ} to {cls}".format(
typ=type(other).__name__, cls=type(self).__name__
)
f"Cannot add {type(other).__name__} to {type(self).__name__}"
)

k = self.n
Expand Down Expand Up @@ -1621,7 +1618,7 @@ def rule_code(self):
suffix = ""
if self.weekday is not None:
weekday = ccalendar.int_to_weekday[self.weekday]
suffix = "-{weekday}".format(weekday=weekday)
suffix = f"-{weekday}"
return self._prefix + suffix

@classmethod
Expand Down Expand Up @@ -1690,13 +1687,9 @@ def __init__(self, n=1, normalize=False, week=0, weekday=0):
object.__setattr__(self, "week", week)

if self.weekday < 0 or self.weekday > 6:
raise ValueError(
"Day must be 0<=day<=6, got {day}".format(day=self.weekday)
)
raise ValueError(f"Day must be 0<=day<=6, got {self.weekday}")
if self.week < 0 or self.week > 3:
raise ValueError(
"Week must be 0<=week<=3, got {week}".format(week=self.week)
)
raise ValueError(f"Week must be 0<=week<=3, got {self.week}")

def _get_offset_day(self, other):
"""
Expand All @@ -1719,16 +1712,12 @@ def _get_offset_day(self, other):
@property
def rule_code(self):
weekday = ccalendar.int_to_weekday.get(self.weekday, "")
return "{prefix}-{week}{weekday}".format(
prefix=self._prefix, week=self.week + 1, weekday=weekday
)
return f"{self._prefix}-{self.week + 1}{weekday}"

@classmethod
def _from_name(cls, suffix=None):
if not suffix:
raise ValueError(
"Prefix {prefix!r} requires a suffix.".format(prefix=cls._prefix)
)
raise ValueError(f"Prefix {repr(cls._prefix)} requires a suffix.")
# TODO: handle n here...
# only one digit weeks (1 --> week 0, 2 --> week 1, etc.)
week = int(suffix[0]) - 1
Expand Down Expand Up @@ -1768,9 +1757,7 @@ def __init__(self, n=1, normalize=False, weekday=0):
raise ValueError("N cannot be 0")

if self.weekday < 0 or self.weekday > 6:
raise ValueError(
"Day must be 0<=day<=6, got {day}".format(day=self.weekday)
)
raise ValueError(f"Day must be 0<=day<=6, got {self.weekday}")

def _get_offset_day(self, other):
"""
Expand All @@ -1794,14 +1781,12 @@ def _get_offset_day(self, other):
@property
def rule_code(self):
weekday = ccalendar.int_to_weekday.get(self.weekday, "")
return "{prefix}-{weekday}".format(prefix=self._prefix, weekday=weekday)
return f"{self._prefix}-{weekday}"

@classmethod
def _from_name(cls, suffix=None):
if not suffix:
raise ValueError(
"Prefix {prefix!r} requires a suffix.".format(prefix=cls._prefix)
)
raise ValueError(f"Prefix {repr(cls._prefix)} requires a suffix.")
# TODO: handle n here...
weekday = ccalendar.weekday_to_int[suffix]
return cls(weekday=weekday)
Expand Down Expand Up @@ -1847,7 +1832,7 @@ def _from_name(cls, suffix=None):
@property
def rule_code(self):
month = ccalendar.MONTH_ALIASES[self.startingMonth]
return "{prefix}-{month}".format(prefix=self._prefix, month=month)
return f"{self._prefix}-{month}"

@apply_wraps
def apply(self, other):
Expand Down Expand Up @@ -1990,7 +1975,7 @@ def _from_name(cls, suffix=None):
@property
def rule_code(self):
month = ccalendar.MONTH_ALIASES[self.month]
return "{prefix}-{month}".format(prefix=self._prefix, month=month)
return f"{self._prefix}-{month}"


class BYearEnd(YearOffset):
Expand Down Expand Up @@ -2104,9 +2089,7 @@ def __init__(
raise ValueError("N cannot be 0")

if self.variation not in ["nearest", "last"]:
raise ValueError(
"{variation} is not a valid variation".format(variation=self.variation)
)
raise ValueError(f"{self.variation} is not a valid variation")

def isAnchored(self):
return (
Expand Down Expand Up @@ -2211,7 +2194,7 @@ def get_year_end(self, dt):
def rule_code(self):
prefix = self._prefix
suffix = self.get_rule_code_suffix()
return "{prefix}-{suffix}".format(prefix=prefix, suffix=suffix)
return f"{prefix}-{suffix}"

def _get_suffix_prefix(self):
if self.variation == "nearest":
Expand All @@ -2223,9 +2206,7 @@ def get_rule_code_suffix(self):
prefix = self._get_suffix_prefix()
month = ccalendar.MONTH_ALIASES[self.startingMonth]
weekday = ccalendar.int_to_weekday[self.weekday]
return "{prefix}-{month}-{weekday}".format(
prefix=prefix, month=month, weekday=weekday
)
return f"{prefix}-{month}-{weekday}"

@classmethod
def _parse_suffix(cls, varion_code, startingMonth_code, weekday_code):
Expand All @@ -2234,9 +2215,7 @@ def _parse_suffix(cls, varion_code, startingMonth_code, weekday_code):
elif varion_code == "L":
variation = "last"
else:
raise ValueError(
"Unable to parse varion_code: {code}".format(code=varion_code)
)
raise ValueError(f"Unable to parse varion_code: {varion_code}")

startingMonth = ccalendar.MONTH_TO_CAL_NUM[startingMonth_code]
weekday = ccalendar.weekday_to_int[weekday_code]
Expand Down Expand Up @@ -2461,9 +2440,7 @@ def onOffset(self, dt):
def rule_code(self):
suffix = self._offset.get_rule_code_suffix()
qtr = self.qtr_with_extra_week
return "{prefix}-{suffix}-{qtr}".format(
prefix=self._prefix, suffix=suffix, qtr=qtr
)
return f"{self._prefix}-{suffix}-{qtr}"

@classmethod
def _from_name(cls, *args):
Expand Down Expand Up @@ -2532,12 +2509,11 @@ def f(self, other):
except AttributeError:
# comparing with a non-Tick object
raise TypeError(
"Invalid comparison between {cls} and {typ}".format(
cls=type(self).__name__, typ=type(other).__name__
)
f"Invalid comparison between {type(self).__name__} "
f"and {type(other).__name__}"
)

f.__name__ = "__{opname}__".format(opname=op.__name__)
f.__name__ = f"__{op.__name__}__"
return f


Expand Down Expand Up @@ -2572,8 +2548,7 @@ def __add__(self, other):
return NotImplemented
except OverflowError:
raise OverflowError(
"the add operation between {self} and {other} "
"will overflow".format(self=self, other=other)
f"the add operation between {self} and {other} will overflow"
)

def __eq__(self, other) -> bool:
Expand Down Expand Up @@ -2645,9 +2620,7 @@ def apply(self, other):
elif isinstance(other, type(self)):
return type(self)(self.n + other.n)

raise ApplyTypeError(
"Unhandled type: {type_str}".format(type_str=type(other).__name__)
)
raise ApplyTypeError(f"Unhandled type: {type(other).__name__}")

def isAnchored(self):
return False
Expand Down Expand Up @@ -2783,9 +2756,7 @@ def generate_range(start=None, end=None, periods=None, offset=BDay()):
# faster than cur + offset
next_date = offset.apply(cur)
if next_date <= cur:
raise ValueError(
"Offset {offset} did not increment date".format(offset=offset)
)
raise ValueError(f"Offset {offset} did not increment date")
cur = next_date
else:
while cur >= end:
Expand All @@ -2799,9 +2770,7 @@ def generate_range(start=None, end=None, periods=None, offset=BDay()):
# faster than cur + offset
next_date = offset.apply(cur)
if next_date >= cur:
raise ValueError(
"Offset {offset} did not decrement date".format(offset=offset)
)
raise ValueError(f"Offset {offset} did not decrement date")
cur = next_date


Expand Down
Loading