diff --git a/pandas/core/construction.py b/pandas/core/construction.py index c7dec9e1234d2..a6c489b95952f 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -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) diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index 063014cbe970d..ae544376c452e 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -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 diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index acf8b6ca4e312..0de7a2e745531 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -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):