Skip to content

Commit 14b2970

Browse files
katieyoungloveproost
authored andcommitted
CLN: changed .format to f string in construction.py, dtypes/base.py, and dtypes/cast.py (pandas-dev#30223)
1 parent 47f7049 commit 14b2970

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

pandas/core/construction.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ def array(
265265
)
266266

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

271271
if dtype is None and isinstance(
272272
data, (ABCSeries, ABCIndexClass, ABCExtensionArray)

pandas/core/dtypes/base.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,13 @@ def construct_from_string(cls, string: str):
231231
... if match:
232232
... return cls(**match.groupdict())
233233
... else:
234-
... raise TypeError("Cannot construct a '{}' from "
235-
... "'{}'".format(cls.__name__, string))
234+
... raise TypeError(f"Cannot construct a '{cls.__name__}' from
235+
... " "'{string}'")
236236
"""
237237
if not isinstance(string, str):
238-
raise TypeError("Expects a string, got {typ}".format(typ=type(string)))
238+
raise TypeError(f"Expects a string, got {type(string).__name__}")
239239
if string != cls.name:
240-
raise TypeError(
241-
"Cannot construct a '{cls}' from '{string}'".format(
242-
cls=cls.__name__, string=string
243-
)
244-
)
240+
raise TypeError(f"Cannot construct a '{cls.__name__}' from '{string}'")
245241
return cls()
246242

247243
@classmethod

pandas/core/dtypes/cast.py

+15-20
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,7 @@ def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
802802
return arr.astype(dtype)
803803

804804
raise TypeError(
805-
"cannot astype a datetimelike from [{from_dtype}] "
806-
"to [{to_dtype}]".format(from_dtype=arr.dtype, to_dtype=dtype)
805+
f"cannot astype a datetimelike from [{arr.dtype}] " f"to [{dtype}]"
807806
)
808807

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

827826
raise TypeError(
828-
"cannot astype a timedelta from [{from_dtype}] "
829-
"to [{to_dtype}]".format(from_dtype=arr.dtype, to_dtype=dtype)
827+
f"cannot astype a timedelta from [{arr.dtype}] " f"to [{dtype}]"
830828
)
831829

832830
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):
853851
return astype_nansafe(to_timedelta(arr).values, dtype, copy=copy)
854852

855853
if dtype.name in ("datetime64", "timedelta64"):
856-
msg = "The '{dtype}' dtype has no unit. Please pass in '{dtype}[ns]' instead."
857-
raise ValueError(msg.format(dtype=dtype.name))
854+
msg = (
855+
f"The '{dtype.name}' dtype has no unit. Please pass in "
856+
f"'{dtype.name}[ns]' instead."
857+
)
858+
raise ValueError(msg)
858859

859860
if copy or is_object_dtype(arr) or is_object_dtype(dtype):
860861
# 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"):
11241125

11251126
# Force the dtype if needed.
11261127
msg = (
1127-
"The '{dtype}' dtype has no unit. "
1128-
"Please pass in '{dtype}[ns]' instead."
1128+
f"The '{dtype.name}' dtype has no unit. "
1129+
f"Please pass in '{dtype.name}[ns]' instead."
11291130
)
11301131

11311132
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"):
11341135
# e.g., [ps], [fs], [as]
11351136
if dtype <= np.dtype("M8[ns]"):
11361137
if dtype.name == "datetime64":
1137-
raise ValueError(msg.format(dtype=dtype.name))
1138+
raise ValueError(msg)
11381139
dtype = _NS_DTYPE
11391140
else:
1140-
raise TypeError(
1141-
"cannot convert datetimelike to "
1142-
"dtype [{dtype}]".format(dtype=dtype)
1143-
)
1141+
raise TypeError(f"cannot convert datetimelike to dtype [{dtype}]")
11441142
elif is_datetime64tz:
11451143

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

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

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

12181213
else:
12191214

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

14831478
if np.array_equal(arr, casted):

0 commit comments

Comments
 (0)