From b934d2a8eef71269dca77d6517bf7a3c0cbfea4e Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 17:18:51 -0800 Subject: [PATCH 01/20] change to f-strings reshape/concat, reshape/melt, reshape/merge, reshape/pivot, reshape/reshape --- pandas/core/reshape/concat.py | 16 ++++----- pandas/core/reshape/melt.py | 6 ++-- pandas/core/reshape/merge.py | 66 +++++++++++++++------------------- pandas/core/reshape/pivot.py | 6 ++-- pandas/core/reshape/reshape.py | 14 +++----- 5 files changed, 45 insertions(+), 63 deletions(-) diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index 8829c242b1129..fbe7894a65cc1 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -352,8 +352,8 @@ def __init__( for obj in objs: if not isinstance(obj, (Series, DataFrame)): msg = ( - "cannot concatenate object of type '{typ}'; " - "only Series and DataFrame objs are valid".format(typ=type(obj)) + f"cannot concatenate object of type '{type(obj)}'; " + f"only Series and DataFrame objs are valid" ) raise TypeError(msg) @@ -403,8 +403,8 @@ def __init__( self._is_series = isinstance(sample, ABCSeries) if not 0 <= axis <= sample.ndim: raise AssertionError( - "axis must be between 0 and {ndim}, input was " - "{axis}".format(ndim=sample.ndim, axis=axis) + f"axis must be between 0 and {sample.ndim}, input was " + f"{axis}" ) # if we have mixed ndims, then convert to highest ndim @@ -623,9 +623,7 @@ def _make_concat_multiindex(indexes, keys, levels=None, names=None) -> MultiInde i = level.get_loc(key) except KeyError: raise ValueError( - "Key {key!s} not in level {level!s}".format( - key=key, level=level - ) + f"Key {key!s} not in level {level!s}" ) to_concat.append(np.repeat(i, len(index))) @@ -678,9 +676,7 @@ def _make_concat_multiindex(indexes, keys, levels=None, names=None) -> MultiInde mask = mapped == -1 if mask.any(): raise ValueError( - "Values not found in passed level: {hlevel!s}".format( - hlevel=hlevel[mask] - ) + f"Values not found in passed level: {hlevel[mask]!s}" ) new_codes.append(np.repeat(mapped, n)) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index d04287e1e9088..9bbe85c4fa7da 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -89,7 +89,7 @@ def melt( var_name = frame.columns.names else: var_name = [ - "variable_{i}".format(i=i) for i in range(len(frame.columns.names)) + f"variable_{i}" for i in range(len(frame.columns.names)) ] else: var_name = [ @@ -417,9 +417,7 @@ def wide_to_long( """ def get_var_names(df, stub: str, sep: str, suffix: str) -> List[str]: - regex = r"^{stub}{sep}{suffix}$".format( - stub=re.escape(stub), sep=re.escape(sep), suffix=suffix - ) + regex = r"^{re.escape(stub)}{re.escape(sep)}{suffix}$" pattern = re.compile(regex) return [col for col in df.columns if pattern.match(col)] diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index ceee2f66dba42..23e2a3c61c84e 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -610,9 +610,9 @@ def __init__( # warn user when merging between different levels if _left.columns.nlevels != _right.columns.nlevels: msg = ( - "merging between different levels can give an unintended " - "result ({left} levels on the left, {right} on the right)" - ).format(left=_left.columns.nlevels, right=_right.columns.nlevels) + f"merging between different levels can give an unintended " + f"result ({left.columns.nlevels} levels on the left, {right.columns.nlevels} on the right)" + ) warnings.warn(msg, UserWarning) self._validate_specification() @@ -678,8 +678,8 @@ def _indicator_pre_merge( for i in ["_left_indicator", "_right_indicator"]: if i in columns: raise ValueError( - "Cannot use `indicator=True` option when " - "data contains a column named {name}".format(name=i) + f"Cannot use `indicator=True` option when " + f"data contains a column named {i}" ) if self.indicator_name in columns: raise ValueError( @@ -831,7 +831,7 @@ def _maybe_add_join_keys(self, result, left_indexer, right_indexer): else: result.index = Index(key_col, name=name) else: - result.insert(i, name or "key_{i}".format(i=i), key_col) + result.insert(i, name or f"key_{i}", key_col) def _get_join_indexers(self): """ return the join indexers """ @@ -1184,14 +1184,9 @@ def _validate_specification(self): common_cols = self.left.columns.intersection(self.right.columns) if len(common_cols) == 0: raise MergeError( - "No common columns to perform merge on. " - "Merge options: left_on={lon}, right_on={ron}, " - "left_index={lidx}, right_index={ridx}".format( - lon=self.left_on, - ron=self.right_on, - lidx=self.left_index, - ridx=self.right_index, - ) + f"No common columns to perform merge on. " + f"Merge options: left_on={self.left_on}, right_on={self.right_on}, " + f"left_index={self.left_index}, right_index={self.right_index}" ) if not common_cols.is_unique: raise MergeError(f"Data columns not unique: {repr(common_cols)}") @@ -1486,12 +1481,12 @@ def get_result(self): def _asof_function(direction: str): - name = "asof_join_{dir}".format(dir=direction) + name = f"asof_join_{direction}" return getattr(libjoin, name, None) def _asof_by_function(direction: str): - name = "asof_join_{dir}_on_X_by_Y".format(dir=direction) + name = f"asof_join_{direction}_on_X_by_Y" return getattr(libjoin, name, None) @@ -1602,7 +1597,7 @@ def _validate_specification(self): # check 'direction' is valid if self.direction not in ["backward", "forward", "nearest"]: raise MergeError( - "direction invalid: {direction}".format(direction=self.direction) + f"direction invalid: {self.direction}" ) @property @@ -1628,17 +1623,13 @@ def _get_merge_keys(self): # later with a ValueError, so we don't *need* to check # for them here. msg = ( - "incompatible merge keys [{i}] {lkdtype} and " - "{rkdtype}, both sides category, but not equal ones".format( - i=i, lkdtype=repr(lk.dtype), rkdtype=repr(rk.dtype) - ) + f"incompatible merge keys [{i}] {lk.dtype!r} and " + f"{rk.dtype!r}, both sides category, but not equal ones" ) else: msg = ( - "incompatible merge keys [{i}] {lkdtype} and " - "{rkdtype}, must be the same type".format( - i=i, lkdtype=repr(lk.dtype), rkdtype=repr(rk.dtype) - ) + f"incompatible merge keys [{i}] {lk.dtype!r} and " + f"{rk.dtype!r}, must be the same type" ) raise MergeError(msg) @@ -1651,10 +1642,8 @@ def _get_merge_keys(self): lt = left_join_keys[-1] msg = ( - "incompatible tolerance {tolerance}, must be compat " - "with type {lkdtype}".format( - tolerance=type(self.tolerance), lkdtype=repr(lt.dtype) - ) + f"incompatible tolerance {self.tolerance}, must be compat " + f"with type {lk.dtype!r}" ) if needs_i8_conversion(lt): @@ -1680,8 +1669,8 @@ def _get_merge_keys(self): # validate allow_exact_matches if not is_bool(self.allow_exact_matches): - msg = "allow_exact_matches must be boolean, passed {passed}" - raise MergeError(msg.format(passed=self.allow_exact_matches)) + msg = f"allow_exact_matches must be boolean, passed {self.allow_exact_matches}" + raise MergeError(msg) return left_join_keys, right_join_keys, join_names @@ -1708,20 +1697,23 @@ def flip(xs): tolerance = self.tolerance # we require sortedness and non-null values in the join keys - msg_sorted = "{side} keys must be sorted" - msg_missings = "Merge keys contain null values on {side} side" + def _msg_sorted(side): + return f"{side} keys must be sorted" + + def _msg_missings(side): + return f"Merge keys contain null values on {side} side" if not Index(left_values).is_monotonic: if isna(left_values).any(): - raise ValueError(msg_missings.format(side="left")) + raise ValueError(_msg_missings(side="left")) else: - raise ValueError(msg_sorted.format(side="left")) + raise ValueError(_msg_sorted(side="left")) if not Index(right_values).is_monotonic: if isna(right_values).any(): - raise ValueError(msg_missings.format(side="right")) + raise ValueError(_msg_missings(side="right")) else: - raise ValueError(msg_sorted.format(side="right")) + raise ValueError(_msg_sorted(side="right")) # initial type conversion as needed if needs_i8_conversion(left_values): diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index a5a9ec9fb79ba..625fc04393064 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -200,7 +200,7 @@ def _add_margins( if not isinstance(margins_name, str): raise ValueError("margins_name argument must be a string") - msg = 'Conflicting name "{name}" in margins'.format(name=margins_name) + msg = f'Conflicting name "{margins_name}" in margins' for level in table.index.names: if margins_name in table.index.get_level_values(level): raise ValueError(msg) @@ -651,7 +651,7 @@ def _normalize(table, normalize, margins: bool, margins_name="All"): margins_name != table.iloc[:, -1].name ): raise ValueError( - "{mname} not in pivoted DataFrame".format(mname=margins_name) + f"{margins_name} not in pivoted DataFrame" ) column_margin = table.iloc[:-1, -1] index_margin = table.iloc[-1, :-1] @@ -702,7 +702,7 @@ def _get_names(arrs, names, prefix: str = "row"): if isinstance(arr, ABCSeries) and arr.name is not None: names.append(arr.name) else: - names.append("{prefix}_{i}".format(prefix=prefix, i=i)) + names.append(f"{prefix}_{i}") else: if len(names) != len(arrs): raise AssertionError("arrays and names must have the same length") diff --git a/pandas/core/reshape/reshape.py b/pandas/core/reshape/reshape.py index f00ff0d4ba5ed..097b533aecb35 100644 --- a/pandas/core/reshape/reshape.py +++ b/pandas/core/reshape/reshape.py @@ -873,16 +873,13 @@ def get_dummies( # validate prefixes and separator to avoid silently dropping cols def check_len(item, name): - len_msg = ( - "Length of '{name}' ({len_item}) did not match the " - "length of the columns being encoded ({len_enc})." - ) if is_list_like(item): if not len(item) == data_to_encode.shape[1]: - len_msg = len_msg.format( - name=name, len_item=len(item), len_enc=data_to_encode.shape[1] - ) + len_msg = ( + f"Length of '{name}' ({len(item)}) did not match the " + f"length of the columns being encoded ({data_to_encode.shape[1]})." + ) raise ValueError(len_msg) check_len(prefix, "prefix") @@ -990,8 +987,7 @@ def get_empty_frame(data) -> DataFrame: # PY2 embedded unicode, gh-22084 def _make_col_name(prefix, prefix_sep, level) -> str: - fstr = "{prefix}{prefix_sep}{level}" - return fstr.format(prefix=prefix, prefix_sep=prefix_sep, level=level) + return f"{prefix}{prefix_sep}{level}" dummy_cols = [_make_col_name(prefix, prefix_sep, level) for level in levels] From 17a6828f8ea52fb10eed5438077648b0a4e48c29 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 17:27:12 -0800 Subject: [PATCH 02/20] pep8 --- pandas/core/reshape/merge.py | 12 ++++++++---- pandas/core/reshape/reshape.py | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 23e2a3c61c84e..f86d973aa732e 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -611,7 +611,8 @@ def __init__( if _left.columns.nlevels != _right.columns.nlevels: msg = ( f"merging between different levels can give an unintended " - f"result ({left.columns.nlevels} levels on the left, {right.columns.nlevels} on the right)" + f"result ({left.columns.nlevels} levels on the left," + f"{right.columns.nlevels} on the right)" ) warnings.warn(msg, UserWarning) @@ -1185,8 +1186,10 @@ def _validate_specification(self): if len(common_cols) == 0: raise MergeError( f"No common columns to perform merge on. " - f"Merge options: left_on={self.left_on}, right_on={self.right_on}, " - f"left_index={self.left_index}, right_index={self.right_index}" + f"Merge options: left_on={self.left_on}," + f"right_on={self.right_on}, " + f"left_index={self.left_index}, " + f"right_index={self.right_index}" ) if not common_cols.is_unique: raise MergeError(f"Data columns not unique: {repr(common_cols)}") @@ -1669,7 +1672,8 @@ def _get_merge_keys(self): # validate allow_exact_matches if not is_bool(self.allow_exact_matches): - msg = f"allow_exact_matches must be boolean, passed {self.allow_exact_matches}" + msg = f"allow_exact_matches must be boolean, " \ + f"passed {self.allow_exact_matches}" raise MergeError(msg) return left_join_keys, right_join_keys, join_names diff --git a/pandas/core/reshape/reshape.py b/pandas/core/reshape/reshape.py index 097b533aecb35..9b429619e70c9 100644 --- a/pandas/core/reshape/reshape.py +++ b/pandas/core/reshape/reshape.py @@ -877,9 +877,9 @@ def check_len(item, name): if is_list_like(item): if not len(item) == data_to_encode.shape[1]: len_msg = ( - f"Length of '{name}' ({len(item)}) did not match the " - f"length of the columns being encoded ({data_to_encode.shape[1]})." - ) + f"Length of '{name}' ({len(item)}) did not match the " + f"length of the columns being encoded ({data_to_encode.shape[1]})." + ) raise ValueError(len_msg) check_len(prefix, "prefix") From c8d0d9f4a8fa8afa49d6a30275766335a74cdaf5 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 17:29:03 -0800 Subject: [PATCH 03/20] pep8 --- pandas/core/reshape/merge.py | 4 ++-- pandas/core/reshape/reshape.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index f86d973aa732e..60ca7a0aab5c3 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -611,7 +611,7 @@ def __init__( if _left.columns.nlevels != _right.columns.nlevels: msg = ( f"merging between different levels can give an unintended " - f"result ({left.columns.nlevels} levels on the left," + f"result ({left.columns.nlevels} levels on the left," f"{right.columns.nlevels} on the right)" ) warnings.warn(msg, UserWarning) @@ -1186,7 +1186,7 @@ def _validate_specification(self): if len(common_cols) == 0: raise MergeError( f"No common columns to perform merge on. " - f"Merge options: left_on={self.left_on}," + f"Merge options: left_on={self.left_on}," f"right_on={self.right_on}, " f"left_index={self.left_index}, " f"right_index={self.right_index}" diff --git a/pandas/core/reshape/reshape.py b/pandas/core/reshape/reshape.py index 9b429619e70c9..a2f7073943b78 100644 --- a/pandas/core/reshape/reshape.py +++ b/pandas/core/reshape/reshape.py @@ -878,7 +878,8 @@ def check_len(item, name): if not len(item) == data_to_encode.shape[1]: len_msg = ( f"Length of '{name}' ({len(item)}) did not match the " - f"length of the columns being encoded ({data_to_encode.shape[1]})." + f"length of the columns being encoded " + f"({data_to_encode.shape[1]})." ) raise ValueError(len_msg) From ee11cb3440fce5827cea40dc7a94a587072830e3 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 17:55:42 -0800 Subject: [PATCH 04/20] Update pandas/core/reshape/concat.py Co-Authored-By: William Ayd --- pandas/core/reshape/concat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index fbe7894a65cc1..63dca50094aea 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -353,7 +353,7 @@ def __init__( if not isinstance(obj, (Series, DataFrame)): msg = ( f"cannot concatenate object of type '{type(obj)}'; " - f"only Series and DataFrame objs are valid" + "only Series and DataFrame objs are valid" ) raise TypeError(msg) From dc0587aea58755e66642fe3327355567eb903f10 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 17:55:57 -0800 Subject: [PATCH 05/20] Update pandas/core/reshape/melt.py Co-Authored-By: William Ayd --- pandas/core/reshape/melt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 9bbe85c4fa7da..025b94b2b87ff 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -417,7 +417,7 @@ def wide_to_long( """ def get_var_names(df, stub: str, sep: str, suffix: str) -> List[str]: - regex = r"^{re.escape(stub)}{re.escape(sep)}{suffix}$" + regex = fr"^{re.escape(stub)}{re.escape(sep)}{suffix}$" pattern = re.compile(regex) return [col for col in df.columns if pattern.match(col)] From e8d7f3ab89b0de0d3dd2a1f00febbb05034dca06 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 17:56:07 -0800 Subject: [PATCH 06/20] Update pandas/core/reshape/merge.py Co-Authored-By: William Ayd --- pandas/core/reshape/merge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 60ca7a0aab5c3..7c74667372baa 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -610,7 +610,7 @@ def __init__( # warn user when merging between different levels if _left.columns.nlevels != _right.columns.nlevels: msg = ( - f"merging between different levels can give an unintended " + "merging between different levels can give an unintended " f"result ({left.columns.nlevels} levels on the left," f"{right.columns.nlevels} on the right)" ) From 111e487808a965e60dc83f147967ca14bb3b618d Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 17:56:18 -0800 Subject: [PATCH 07/20] Update pandas/core/reshape/merge.py Co-Authored-By: William Ayd --- pandas/core/reshape/merge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 7c74667372baa..e68ebb4c1975f 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -679,7 +679,7 @@ def _indicator_pre_merge( for i in ["_left_indicator", "_right_indicator"]: if i in columns: raise ValueError( - f"Cannot use `indicator=True` option when " + "Cannot use `indicator=True` option when " f"data contains a column named {i}" ) if self.indicator_name in columns: From f27779c20b184b780463c6d441a44f75f29aa398 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 17:56:32 -0800 Subject: [PATCH 08/20] Update pandas/core/reshape/merge.py Co-Authored-By: William Ayd --- pandas/core/reshape/merge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index e68ebb4c1975f..e9b31c7936245 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1185,7 +1185,7 @@ def _validate_specification(self): common_cols = self.left.columns.intersection(self.right.columns) if len(common_cols) == 0: raise MergeError( - f"No common columns to perform merge on. " + "No common columns to perform merge on. " f"Merge options: left_on={self.left_on}," f"right_on={self.right_on}, " f"left_index={self.left_index}, " From ac8661a855bc0aacf6aa1fc12ea5aade632c3fee Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 17:56:58 -0800 Subject: [PATCH 09/20] Update pandas/core/reshape/reshape.py Co-Authored-By: William Ayd --- pandas/core/reshape/reshape.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/reshape.py b/pandas/core/reshape/reshape.py index a2f7073943b78..359e5b956f8a5 100644 --- a/pandas/core/reshape/reshape.py +++ b/pandas/core/reshape/reshape.py @@ -878,7 +878,7 @@ def check_len(item, name): if not len(item) == data_to_encode.shape[1]: len_msg = ( f"Length of '{name}' ({len(item)}) did not match the " - f"length of the columns being encoded " + "length of the columns being encoded " f"({data_to_encode.shape[1]})." ) raise ValueError(len_msg) From c85d33272b2fd47671cba403ef66b8035467de46 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 18:03:14 -0800 Subject: [PATCH 10/20] remove \ continuation --- pandas/core/reshape/merge.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index e9b31c7936245..24f7d81a83a02 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1672,8 +1672,10 @@ def _get_merge_keys(self): # validate allow_exact_matches if not is_bool(self.allow_exact_matches): - msg = f"allow_exact_matches must be boolean, " \ + msg = ( + f"allow_exact_matches must be boolean, " f"passed {self.allow_exact_matches}" + ) raise MergeError(msg) return left_join_keys, right_join_keys, join_names From ca63c197de9ae65beeaa247776b6dde976ccdeb9 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 18:06:10 -0800 Subject: [PATCH 11/20] pep8 --- pandas/core/reshape/merge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 24f7d81a83a02..f91ac88acb79d 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1673,7 +1673,7 @@ def _get_merge_keys(self): # validate allow_exact_matches if not is_bool(self.allow_exact_matches): msg = ( - f"allow_exact_matches must be boolean, " + f"allow_exact_matches must be boolean, " f"passed {self.allow_exact_matches}" ) raise MergeError(msg) From ce1e8e5b2a19927fcd410e701565889149a7e439 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 19:53:40 -0800 Subject: [PATCH 12/20] fix foo!r to repr(foo) --- pandas/core/reshape/merge.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index f91ac88acb79d..9f22c96254114 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1626,13 +1626,13 @@ def _get_merge_keys(self): # later with a ValueError, so we don't *need* to check # for them here. msg = ( - f"incompatible merge keys [{i}] {lk.dtype!r} and " - f"{rk.dtype!r}, both sides category, but not equal ones" + f"incompatible merge keys [{i}] {repr(lk.dtype)} and " + f"{repr(rk.dtype)}, both sides category, but not equal ones" ) else: msg = ( - f"incompatible merge keys [{i}] {lk.dtype!r} and " - f"{rk.dtype!r}, must be the same type" + f"incompatible merge keys [{i}] {repr(lk.dtype)} and " + f"{repr(rk.dtype)}, must be the same type" ) raise MergeError(msg) @@ -1646,7 +1646,7 @@ def _get_merge_keys(self): msg = ( f"incompatible tolerance {self.tolerance}, must be compat " - f"with type {lk.dtype!r}" + f"with type {repr(lk.dtype)}" ) if needs_i8_conversion(lt): From 9c697366058933a05e960756944fbaf9db85ff88 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 20:48:43 -0800 Subject: [PATCH 13/20] make test match error message f-string formatting --- pandas/tests/reshape/merge/test_merge.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/reshape/merge/test_merge.py b/pandas/tests/reshape/merge/test_merge.py index f9acf5b60a3cd..a778b364708ef 100644 --- a/pandas/tests/reshape/merge/test_merge.py +++ b/pandas/tests/reshape/merge/test_merge.py @@ -370,12 +370,12 @@ def test_no_overlap_more_informative_error(self): df2 = DataFrame({"y": ["b", "c"]}, index=[dt, dt]) msg = ( - "No common columns to perform merge on. " - "Merge options: left_on={lon}, right_on={ron}, " - "left_index={lidx}, right_index={ridx}".format( - lon=None, ron=None, lidx=False, ridx=False + "No common columns to perform merge on. " + f"Merge options: left_on={self.left_on}," + f"right_on={self.right_on}, " + f"left_index={self.left_index}, " + f"right_index={self.right_index}" ) - ) with pytest.raises(MergeError, match=msg): merge(df1, df2) From ea7e93106f06563f77521108abbbf32aa51c500a Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 20:49:41 -0800 Subject: [PATCH 14/20] pep8 --- pandas/tests/reshape/merge/test_merge.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/tests/reshape/merge/test_merge.py b/pandas/tests/reshape/merge/test_merge.py index a778b364708ef..c812248426be4 100644 --- a/pandas/tests/reshape/merge/test_merge.py +++ b/pandas/tests/reshape/merge/test_merge.py @@ -370,12 +370,12 @@ def test_no_overlap_more_informative_error(self): df2 = DataFrame({"y": ["b", "c"]}, index=[dt, dt]) msg = ( - "No common columns to perform merge on. " - f"Merge options: left_on={self.left_on}," - f"right_on={self.right_on}, " - f"left_index={self.left_index}, " - f"right_index={self.right_index}" - ) + "No common columns to perform merge on. " + f"Merge options: left_on={self.left_on}," + f"right_on={self.right_on}, " + f"left_index={self.left_index}, " + f"right_index={self.right_index}" + ) with pytest.raises(MergeError, match=msg): merge(df1, df2) From 58309957747fb408d07e58019cfa803756a337d0 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sat, 1 Feb 2020 21:08:21 -0800 Subject: [PATCH 15/20] better test f-string formatting --- pandas/tests/reshape/merge/test_merge.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/tests/reshape/merge/test_merge.py b/pandas/tests/reshape/merge/test_merge.py index c812248426be4..6c5a3138c7d66 100644 --- a/pandas/tests/reshape/merge/test_merge.py +++ b/pandas/tests/reshape/merge/test_merge.py @@ -370,11 +370,9 @@ def test_no_overlap_more_informative_error(self): df2 = DataFrame({"y": ["b", "c"]}, index=[dt, dt]) msg = ( - "No common columns to perform merge on. " - f"Merge options: left_on={self.left_on}," - f"right_on={self.right_on}, " - f"left_index={self.left_index}, " - f"right_index={self.right_index}" + f"No common columns to perform merge on. " + f"Merge options: left_on={None}, right_on={None}, " + f"left_index={False}, right_index={False}" ) with pytest.raises(MergeError, match=msg): From 16ec7f799bec3e4cd7ec5b0ca731bd2f41e731b3 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sun, 2 Feb 2020 10:14:18 -0800 Subject: [PATCH 16/20] black pandas --- pandas/core/reshape/concat.py | 11 +++-------- pandas/core/reshape/melt.py | 4 +--- pandas/core/reshape/merge.py | 4 +--- pandas/core/reshape/pivot.py | 4 +--- 4 files changed, 6 insertions(+), 17 deletions(-) diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index 63dca50094aea..e585a388b9bbf 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -403,8 +403,7 @@ def __init__( self._is_series = isinstance(sample, ABCSeries) if not 0 <= axis <= sample.ndim: raise AssertionError( - f"axis must be between 0 and {sample.ndim}, input was " - f"{axis}" + f"axis must be between 0 and {sample.ndim}, input was " f"{axis}" ) # if we have mixed ndims, then convert to highest ndim @@ -622,9 +621,7 @@ def _make_concat_multiindex(indexes, keys, levels=None, names=None) -> MultiInde try: i = level.get_loc(key) except KeyError: - raise ValueError( - f"Key {key!s} not in level {level!s}" - ) + raise ValueError(f"Key {key!s} not in level {level!s}") to_concat.append(np.repeat(i, len(index))) codes_list.append(np.concatenate(to_concat)) @@ -675,9 +672,7 @@ def _make_concat_multiindex(indexes, keys, levels=None, names=None) -> MultiInde mask = mapped == -1 if mask.any(): - raise ValueError( - f"Values not found in passed level: {hlevel[mask]!s}" - ) + raise ValueError(f"Values not found in passed level: {hlevel[mask]!s}") new_codes.append(np.repeat(mapped, n)) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 025b94b2b87ff..782b8043430e1 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -88,9 +88,7 @@ def melt( if len(frame.columns.names) == len(set(frame.columns.names)): var_name = frame.columns.names else: - var_name = [ - f"variable_{i}" for i in range(len(frame.columns.names)) - ] + var_name = [f"variable_{i}" for i in range(len(frame.columns.names))] else: var_name = [ frame.columns.name if frame.columns.name is not None else "variable" diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 9f22c96254114..f954adda8eb04 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1599,9 +1599,7 @@ def _validate_specification(self): # check 'direction' is valid if self.direction not in ["backward", "forward", "nearest"]: - raise MergeError( - f"direction invalid: {self.direction}" - ) + raise MergeError(f"direction invalid: {self.direction}") @property def _asof_key(self): diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 625fc04393064..053fb86836ff8 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -650,9 +650,7 @@ def _normalize(table, normalize, margins: bool, margins_name="All"): if (margins_name not in table.iloc[-1, :].name) | ( margins_name != table.iloc[:, -1].name ): - raise ValueError( - f"{margins_name} not in pivoted DataFrame" - ) + raise ValueError(f"{margins_name} not in pivoted DataFrame") column_margin = table.iloc[:-1, -1] index_margin = table.iloc[-1, :-1] From 4b1d7e5615dc710e61e22f2a1704013346d948fb Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sun, 2 Feb 2020 11:17:08 -0800 Subject: [PATCH 17/20] fix a string that was split by black --- pandas/core/reshape/concat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index e585a388b9bbf..3a033868d6a95 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -403,7 +403,7 @@ def __init__( self._is_series = isinstance(sample, ABCSeries) if not 0 <= axis <= sample.ndim: raise AssertionError( - f"axis must be between 0 and {sample.ndim}, input was " f"{axis}" + f"axis must be between 0 and {sample.ndim}, input was {axis}" ) # if we have mixed ndims, then convert to highest ndim From e974649c0816624fad5db4b8628455c6029a52a9 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sun, 2 Feb 2020 11:37:34 -0800 Subject: [PATCH 18/20] fix missing space in error message --- pandas/core/reshape/merge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index f954adda8eb04..160f442a2bade 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1186,7 +1186,7 @@ def _validate_specification(self): if len(common_cols) == 0: raise MergeError( "No common columns to perform merge on. " - f"Merge options: left_on={self.left_on}," + f"Merge options: left_on={self.left_on}, " f"right_on={self.right_on}, " f"left_index={self.left_index}, " f"right_index={self.right_index}" From cc4c6aad484b33f255a853db21494f2b8775cf78 Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sun, 2 Feb 2020 11:37:34 -0800 Subject: [PATCH 19/20] fix missing space in error message --- pandas/core/reshape/merge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index f954adda8eb04..160f442a2bade 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1186,7 +1186,7 @@ def _validate_specification(self): if len(common_cols) == 0: raise MergeError( "No common columns to perform merge on. " - f"Merge options: left_on={self.left_on}," + f"Merge options: left_on={self.left_on}, " f"right_on={self.right_on}, " f"left_index={self.left_index}, " f"right_index={self.right_index}" From 1ecd18bb9b657609535809c3e6ea88d2945c416a Mon Sep 17 00:00:00 2001 From: Abbie Popa Date: Sun, 2 Feb 2020 20:07:38 -0800 Subject: [PATCH 20/20] respond to second reviews --- pandas/core/reshape/concat.py | 2 +- pandas/core/reshape/merge.py | 17 +++++++---------- pandas/tests/reshape/merge/test_merge.py | 2 +- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index 3a033868d6a95..d9f21f0b274ac 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -621,7 +621,7 @@ def _make_concat_multiindex(indexes, keys, levels=None, names=None) -> MultiInde try: i = level.get_loc(key) except KeyError: - raise ValueError(f"Key {key!s} not in level {level!s}") + raise ValueError(f"Key {key} not in level {level}") to_concat.append(np.repeat(i, len(index))) codes_list.append(np.concatenate(to_concat)) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 160f442a2bade..480c5279ad3f6 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1671,7 +1671,7 @@ def _get_merge_keys(self): # validate allow_exact_matches if not is_bool(self.allow_exact_matches): msg = ( - f"allow_exact_matches must be boolean, " + "allow_exact_matches must be boolean, " f"passed {self.allow_exact_matches}" ) raise MergeError(msg) @@ -1701,23 +1701,20 @@ def flip(xs): tolerance = self.tolerance # we require sortedness and non-null values in the join keys - def _msg_sorted(side): - return f"{side} keys must be sorted" - - def _msg_missings(side): - return f"Merge keys contain null values on {side} side" + msg_sorted = "{side} keys must be sorted" + msg_missings = "Merge keys contain null values on {side} side" if not Index(left_values).is_monotonic: if isna(left_values).any(): - raise ValueError(_msg_missings(side="left")) + raise ValueError(msg_missings.format(side="left")) else: - raise ValueError(_msg_sorted(side="left")) + raise ValueError(msg_sorted.format(side="left")) if not Index(right_values).is_monotonic: if isna(right_values).any(): - raise ValueError(_msg_missings(side="right")) + raise ValueError(msg_missings.format(side="right")) else: - raise ValueError(_msg_sorted(side="right")) + raise ValueError(msg_sorted.format(side="right")) # initial type conversion as needed if needs_i8_conversion(left_values): diff --git a/pandas/tests/reshape/merge/test_merge.py b/pandas/tests/reshape/merge/test_merge.py index 6c5a3138c7d66..fd189c7435b29 100644 --- a/pandas/tests/reshape/merge/test_merge.py +++ b/pandas/tests/reshape/merge/test_merge.py @@ -370,7 +370,7 @@ def test_no_overlap_more_informative_error(self): df2 = DataFrame({"y": ["b", "c"]}, index=[dt, dt]) msg = ( - f"No common columns to perform merge on. " + "No common columns to perform merge on. " f"Merge options: left_on={None}, right_on={None}, " f"left_index={False}, right_index={False}" )