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

CLN: changed .format to f string in construction.py, dtypes/base.py, and dtypes/cast.py #30223

Merged
merged 3 commits into from
Dec 12, 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
4 changes: 2 additions & 2 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 4 additions & 8 deletions pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 15 additions & 20 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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.
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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):
Expand Down