Skip to content

Commit

Permalink
CLN: changed .format to f string in construction.py, dtypes/base.py, …
Browse files Browse the repository at this point in the history
…and dtypes/cast.py (#30223)
  • Loading branch information
katieyounglove authored and jreback committed Dec 12, 2019
1 parent ed6fd2f commit ce14abf
Showing 3 changed files with 21 additions and 30 deletions.
4 changes: 2 additions & 2 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
@@ -265,8 +265,8 @@ def array(
)

if lib.is_scalar(data):
msg = "Cannot pass scalar '{}' to 'pandas.array'."
raise ValueError(msg.format(data))
msg = f"Cannot pass scalar '{data}' to 'pandas.array'."
raise ValueError(msg)

if dtype is None and isinstance(
data, (ABCSeries, ABCIndexClass, ABCExtensionArray)
12 changes: 4 additions & 8 deletions pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
@@ -231,17 +231,13 @@ def construct_from_string(cls, string: str):
... if match:
... return cls(**match.groupdict())
... else:
... raise TypeError("Cannot construct a '{}' from "
... "'{}'".format(cls.__name__, string))
... raise TypeError(f"Cannot construct a '{cls.__name__}' from
... " "'{string}'")
"""
if not isinstance(string, str):
raise TypeError("Expects a string, got {typ}".format(typ=type(string)))
raise TypeError(f"Expects a string, got {type(string).__name__}")
if string != cls.name:
raise TypeError(
"Cannot construct a '{cls}' from '{string}'".format(
cls=cls.__name__, string=string
)
)
raise TypeError(f"Cannot construct a '{cls.__name__}' from '{string}'")
return cls()

@classmethod
35 changes: 15 additions & 20 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
@@ -802,8 +802,7 @@ def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
return arr.astype(dtype)

raise TypeError(
"cannot astype a datetimelike from [{from_dtype}] "
"to [{to_dtype}]".format(from_dtype=arr.dtype, to_dtype=dtype)
f"cannot astype a datetimelike from [{arr.dtype}] " f"to [{dtype}]"
)

elif is_timedelta64_dtype(arr):
@@ -825,8 +824,7 @@ def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
return arr.astype(_TD_DTYPE, copy=copy)

raise TypeError(
"cannot astype a timedelta from [{from_dtype}] "
"to [{to_dtype}]".format(from_dtype=arr.dtype, to_dtype=dtype)
f"cannot astype a timedelta from [{arr.dtype}] " f"to [{dtype}]"
)

elif np.issubdtype(arr.dtype, np.floating) and np.issubdtype(dtype, np.integer):
@@ -853,8 +851,11 @@ def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
return astype_nansafe(to_timedelta(arr).values, dtype, copy=copy)

if dtype.name in ("datetime64", "timedelta64"):
msg = "The '{dtype}' dtype has no unit. Please pass in '{dtype}[ns]' instead."
raise ValueError(msg.format(dtype=dtype.name))
msg = (
f"The '{dtype.name}' dtype has no unit. Please pass in "
f"'{dtype.name}[ns]' instead."
)
raise ValueError(msg)

if copy or is_object_dtype(arr) or is_object_dtype(dtype):
# Explicit copy, or required since NumPy can't view from / to object.
@@ -1124,8 +1125,8 @@ def maybe_cast_to_datetime(value, dtype, errors: str = "raise"):

# Force the dtype if needed.
msg = (
"The '{dtype}' dtype has no unit. "
"Please pass in '{dtype}[ns]' instead."
f"The '{dtype.name}' dtype has no unit. "
f"Please pass in '{dtype.name}[ns]' instead."
)

if is_datetime64 and not is_dtype_equal(dtype, _NS_DTYPE):
@@ -1134,13 +1135,10 @@ def maybe_cast_to_datetime(value, dtype, errors: str = "raise"):
# e.g., [ps], [fs], [as]
if dtype <= np.dtype("M8[ns]"):
if dtype.name == "datetime64":
raise ValueError(msg.format(dtype=dtype.name))
raise ValueError(msg)
dtype = _NS_DTYPE
else:
raise TypeError(
"cannot convert datetimelike to "
"dtype [{dtype}]".format(dtype=dtype)
)
raise TypeError(f"cannot convert datetimelike to dtype [{dtype}]")
elif is_datetime64tz:

# our NaT doesn't support tz's
@@ -1155,13 +1153,10 @@ def maybe_cast_to_datetime(value, dtype, errors: str = "raise"):
# e.g., [ps], [fs], [as]
if dtype <= np.dtype("m8[ns]"):
if dtype.name == "timedelta64":
raise ValueError(msg.format(dtype=dtype.name))
raise ValueError(msg)
dtype = _TD_DTYPE
else:
raise TypeError(
"cannot convert timedeltalike to "
"dtype [{dtype}]".format(dtype=dtype)
)
raise TypeError(f"cannot convert timedeltalike to dtype [{dtype}]")

if is_scalar(value):
if value == iNaT or isna(value):
@@ -1213,7 +1208,7 @@ def maybe_cast_to_datetime(value, dtype, errors: str = "raise"):
return tslib.ints_to_pydatetime(ints)

# we have a non-castable dtype that was passed
raise TypeError("Cannot cast datetime64 to {dtype}".format(dtype=dtype))
raise TypeError(f"Cannot cast datetime64 to {dtype}")

else:

@@ -1477,7 +1472,7 @@ def maybe_cast_to_integer_array(arr, dtype, copy: bool = False):
except OverflowError:
raise OverflowError(
"The elements provided in the data cannot all be "
"casted to the dtype {dtype}".format(dtype=dtype)
f"casted to the dtype {dtype}"
)

if np.array_equal(arr, casted):

0 comments on commit ce14abf

Please sign in to comment.