From bcd2152d63201915f1d80683b84676ef070467ef Mon Sep 17 00:00:00 2001 From: ganevgv Date: Mon, 2 Dec 2019 00:53:46 +0000 Subject: [PATCH 01/10] clean pandas/core/window/common.py --- pandas/core/window/common.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/core/window/common.py b/pandas/core/window/common.py index 453fd12495543..9f377c180a263 100644 --- a/pandas/core/window/common.py +++ b/pandas/core/window/common.py @@ -303,10 +303,7 @@ def calculate_min_periods( else: min_periods = max(required_min_periods, min_periods) if min_periods > window: - raise ValueError( - "min_periods {min_periods} must be <= " - "window {window}".format(min_periods=min_periods, window=window) - ) + raise ValueError(f"min_periods {min_periods} must be <= window {window}") elif min_periods > num_values: min_periods = num_values + 1 elif min_periods < 0: From b12223eed933d6454a40295df359d2298d158db5 Mon Sep 17 00:00:00 2001 From: ganevgv Date: Mon, 2 Dec 2019 00:54:32 +0000 Subject: [PATCH 02/10] clean pandas/core/window/ewm.py --- pandas/core/window/ewm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py index c9837afd96356..8337318ac6bcc 100644 --- a/pandas/core/window/ewm.py +++ b/pandas/core/window/ewm.py @@ -232,7 +232,7 @@ def _apply(self, func, **kwargs): if cfunc is None: raise ValueError( "we do not support this function " - "in window_aggregations.{func}".format(func=func) + f"in window_aggregations.{func}" ) def func(arg): From dd26b717fba97c512fdb1d4c543d7363c00ee23e Mon Sep 17 00:00:00 2001 From: ganevgv Date: Mon, 2 Dec 2019 00:54:59 +0000 Subject: [PATCH 03/10] clean pandas/core/window/expanding.py --- pandas/core/window/expanding.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/window/expanding.py b/pandas/core/window/expanding.py index 2e527b90249c9..32cf81f320893 100644 --- a/pandas/core/window/expanding.py +++ b/pandas/core/window/expanding.py @@ -209,10 +209,10 @@ def skew(self, **kwargs): >>> arr = [1, 2, 3, 4, 999] >>> import scipy.stats - >>> fmt = "{0:.6f}" # limit the printed precision to 6 digits - >>> print(fmt.format(scipy.stats.kurtosis(arr[:-1], bias=False))) + >>> precision = 6 # limit the printed precision to 6 digits + >>> print(f"{scipy.stats.kurtosis(arr[:-1], bias=False):.{precision}f}") -1.200000 - >>> print(fmt.format(scipy.stats.kurtosis(arr, bias=False))) + >>> print(f"{scipy.stats.kurtosis(arr, bias=False):.{precision}f}") 4.999874 >>> s = pd.Series(arr) >>> s.expanding(4).kurt() From 55ad26af1717266e18449ae9cf804ca32f1d1f2f Mon Sep 17 00:00:00 2001 From: ganevgv Date: Mon, 2 Dec 2019 01:21:32 +0000 Subject: [PATCH 04/10] clean pandas/core/window/rolling.py --- pandas/core/window/rolling.py | 65 +++++++++++++++-------------------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index d8aa362080093..de9e5912f2c51 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -117,7 +117,7 @@ def validate(self): ]: raise ValueError("closed must be 'right', 'left', 'both' or 'neither'") if not isinstance(self.obj, (ABCSeries, ABCDataFrame)): - raise TypeError("invalid type: {}".format(type(self))) + raise TypeError(f"invalid type: {type(self)}") def _create_blocks(self): """ @@ -163,9 +163,7 @@ def __getattr__(self, attr): if attr in self.obj: return self[attr] - raise AttributeError( - "%r object has no attribute %r" % (type(self).__name__, attr) - ) + raise AttributeError(f"{type(self).__name__} object has no attribute {attr}") def _dir_additions(self): return self.obj._dir_additions() @@ -212,17 +210,16 @@ def __repr__(self) -> str: """ attrs = ( - "{k}={v}".format(k=k, v=getattr(self, k)) + "{k}={getattr(self, k)}" for k in self._attributes if getattr(self, k, None) is not None ) - return "{klass} [{attrs}]".format( - klass=self._window_type, attrs=",".join(attrs) - ) + attrs = ",".join(attrs) + return f"{self._window_type} [{attrs}]" def __iter__(self): url = "https://github.com/pandas-dev/pandas/issues/11704" - raise NotImplementedError("See issue #11704 {url}".format(url=url)) + raise NotImplementedError(f"See issue #11704 {url}") def _get_index(self) -> Optional[np.ndarray]: """ @@ -250,15 +247,15 @@ def _prep_values(self, values: Optional[np.ndarray] = None) -> np.ndarray: values = ensure_float64(values) elif needs_i8_conversion(values.dtype): raise NotImplementedError( - "ops for {action} for this " - "dtype {dtype} are not " - "implemented".format(action=self._window_type, dtype=values.dtype) + f"ops for {self._window_type} for this " + f"dtype {values.dtype} are not " + "implemented" ) else: try: values = ensure_float64(values) except (ValueError, TypeError): - raise TypeError("cannot handle this type -> {0}".format(values.dtype)) + raise TypeError(f"cannot handle this type -> {values.dtype}") # Convert inf to nan for C funcs inf = np.isinf(values) @@ -383,8 +380,7 @@ def _get_roll_func(self, func_name: str) -> Callable: window_func = getattr(window_aggregations, func_name, None) if window_func is None: raise ValueError( - "we do not support this function " - "in window_aggregations.{func_name}".format(func_name=func_name) + "we do not support this function " f"in window_aggregations.{func_name}" ) return window_func @@ -395,10 +391,8 @@ def _get_cython_func_type(self, func): Variable algorithms do not use window while fixed do. """ if self.is_freq_type: - return self._get_roll_func("{}_variable".format(func)) - return partial( - self._get_roll_func("{}_fixed".format(func)), win=self._get_window() - ) + return self._get_roll_func(f"{func}_variable") + return partial(self._get_roll_func(f"{func}_fixed"), win=self._get_window()) def _get_window_indexer(self): """ @@ -917,11 +911,11 @@ def validate(self): import scipy.signal as sig if not isinstance(self.win_type, str): - raise ValueError("Invalid win_type {0}".format(self.win_type)) + raise ValueError(f"Invalid win_type {self.win_type}") if getattr(sig, self.win_type, None) is None: - raise ValueError("Invalid win_type {0}".format(self.win_type)) + raise ValueError(f"Invalid win_type {self.win_type}") else: - raise ValueError("Invalid window {0}".format(window)) + raise ValueError(f"Invalid window {window}") def _get_win_type(self, kwargs: Dict) -> Union[str, Tuple]: """ @@ -958,11 +952,10 @@ def _validate_win_type(win_type, kwargs): return win_type def _pop_args(win_type, arg_names, kwargs): - msg = "%s window requires %%s" % win_type all_args = [] for n in arg_names: if n not in kwargs: - raise ValueError(msg % n) + raise ValueError(f"{win_type} window requires {n}") all_args.append(kwargs.pop(n)) return all_args @@ -1634,12 +1627,12 @@ def _get_cov(X, Y): >>> v1 = [3, 3, 3, 5, 8] >>> v2 = [3, 4, 4, 4, 8] - >>> fmt = "{0:.6f}" # limit the printed precision to 6 digits + >>> precision = 6 # limit the printed precision to 6 digits >>> # numpy returns a 2X2 array, the correlation coefficient >>> # is the number at entry [0][1] - >>> print(fmt.format(np.corrcoef(v1[:-1], v2[:-1])[0][1])) + >>> print(f"{np.corrcoef(v1[:-1], v2[:-1])[0][1]:.{precision}f}") 0.333333 - >>> print(fmt.format(np.corrcoef(v1[1:], v2[1:])[0][1])) + >>> print(f"{np.corrcoef(v1[1:], v2[1:])[0][1]:.{precision}f}") 0.916949 >>> s1 = pd.Series(v1) >>> s2 = pd.Series(v2) @@ -1729,9 +1722,9 @@ def _on(self) -> Index: return Index(self.obj[self.on]) else: raise ValueError( - "invalid on specified as {on}, " + f"invalid on specified as {self.on}, " "must be a column (of DataFrame), an Index " - "or None".format(on=self.on) + "or None" ) def validate(self): @@ -1780,9 +1773,7 @@ def _validate_monotonic(self): formatted = self.on if self.on is None: formatted = "index" - raise ValueError( - "{formatted} must be monotonic".format(formatted=formatted) - ) + raise ValueError(f"{formatted} must be monotonic") def _validate_freq(self): """ @@ -1794,9 +1785,9 @@ def _validate_freq(self): return to_offset(self.window) except (TypeError, ValueError): raise ValueError( - "passed window {window} is not " + f"passed window {self.window} is not " "compatible with a datetimelike " - "index".format(window=self.window) + "index" ) _agg_see_also_doc = dedent( @@ -1941,11 +1932,11 @@ def skew(self, **kwargs): four matching the equivalent function call using `scipy.stats`. >>> arr = [1, 2, 3, 4, 999] - >>> fmt = "{0:.6f}" # limit the printed precision to 6 digits + >>> precision = 6 # limit the printed precision to 6 digits >>> import scipy.stats - >>> print(fmt.format(scipy.stats.kurtosis(arr[:-1], bias=False))) + >>> print(f"{scipy.stats.kurtosis(arr[:-1], bias=False):.{precision}f}") -1.200000 - >>> print(fmt.format(scipy.stats.kurtosis(arr[1:], bias=False))) + >>> print(f"{scipy.stats.kurtosis(arr[1:], bias=False):.{precision}f}") 3.999946 >>> s = pd.Series(arr) >>> s.rolling(4).kurt() From 477e4c20c18fba649f26b33976e895115041c298 Mon Sep 17 00:00:00 2001 From: ganevgv Date: Mon, 2 Dec 2019 02:14:57 +0000 Subject: [PATCH 05/10] add strings around values --- pandas/core/window/rolling.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index de9e5912f2c51..e918473c29e19 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -163,7 +163,9 @@ def __getattr__(self, attr): if attr in self.obj: return self[attr] - raise AttributeError(f"{type(self).__name__} object has no attribute {attr}") + raise AttributeError( + f"'{type(self).__name__}' object has no attribute '{attr}'" + ) def _dir_additions(self): return self.obj._dir_additions() From 5bc5475eec8f6370b5d6e3226c6b64f05aa578f1 Mon Sep 17 00:00:00 2001 From: ganevgv Date: Mon, 2 Dec 2019 02:52:32 +0000 Subject: [PATCH 06/10] add missing f --- pandas/core/window/rolling.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index e918473c29e19..677bd05167cdc 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -212,7 +212,7 @@ def __repr__(self) -> str: """ attrs = ( - "{k}={getattr(self, k)}" + f"{k}={getattr(self, k)}" for k in self._attributes if getattr(self, k, None) is not None ) @@ -250,8 +250,7 @@ def _prep_values(self, values: Optional[np.ndarray] = None) -> np.ndarray: elif needs_i8_conversion(values.dtype): raise NotImplementedError( f"ops for {self._window_type} for this " - f"dtype {values.dtype} are not " - "implemented" + f"dtype {values.dtype} are not implemented" ) else: try: @@ -382,7 +381,7 @@ def _get_roll_func(self, func_name: str) -> Callable: window_func = getattr(window_aggregations, func_name, None) if window_func is None: raise ValueError( - "we do not support this function " f"in window_aggregations.{func_name}" + f"we do not support this function in window_aggregations.{func_name}" ) return window_func From d385f6ea2997929b639921e3514e7ab78f8c6c55 Mon Sep 17 00:00:00 2001 From: ganevgv Date: Mon, 2 Dec 2019 10:41:00 +0000 Subject: [PATCH 07/10] revert __repr__ in pandas/core/window/rolling.py --- pandas/core/window/rolling.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 677bd05167cdc..477ef74fa111d 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -212,12 +212,13 @@ def __repr__(self) -> str: """ attrs = ( - f"{k}={getattr(self, k)}" + "{k}={v}".format(k=k, v=getattr(self, k)) for k in self._attributes if getattr(self, k, None) is not None ) - attrs = ",".join(attrs) - return f"{self._window_type} [{attrs}]" + return "{klass} [{attrs}]".format( + klass=self._window_type, attrs=",".join(attrs) + ) def __iter__(self): url = "https://github.com/pandas-dev/pandas/issues/11704" From 37756998f5686850b9ebf44a767d4e315f0f8802 Mon Sep 17 00:00:00 2001 From: ganevgv Date: Mon, 2 Dec 2019 12:10:09 +0000 Subject: [PATCH 08/10] use f-string for attrs_list in __repr__ in pandas/core/window/rolling.py --- pandas/core/window/rolling.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 477ef74fa111d..be33f2816e4f8 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -211,14 +211,13 @@ def __repr__(self) -> str: Provide a nice str repr of our rolling object. """ - attrs = ( - "{k}={v}".format(k=k, v=getattr(self, k)) - for k in self._attributes - if getattr(self, k, None) is not None - ) - return "{klass} [{attrs}]".format( - klass=self._window_type, attrs=",".join(attrs) + attrs_list = ( + f"{attr_name}={getattr(self, attr_name)}" + for attr_name in self._attributes + if getattr(self, attr_name, None) is not None ) + attrs = ",".join(attrs_list) + return "{klass} [{attrs}]".format(klass=self._window_type, attrs=attrs) def __iter__(self): url = "https://github.com/pandas-dev/pandas/issues/11704" From 80f9c1b2ec13b36531b5d2ff761152fef5f79686 Mon Sep 17 00:00:00 2001 From: ganevgv Date: Mon, 2 Dec 2019 13:19:53 +0000 Subject: [PATCH 09/10] use f-string in __repr__ return in pandas/core/window/rolling.py --- pandas/core/window/rolling.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index be33f2816e4f8..4d69cd81713da 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -217,7 +217,7 @@ def __repr__(self) -> str: if getattr(self, attr_name, None) is not None ) attrs = ",".join(attrs_list) - return "{klass} [{attrs}]".format(klass=self._window_type, attrs=attrs) + return f"{self._window_type} [{attrs}]" def __iter__(self): url = "https://github.com/pandas-dev/pandas/issues/11704" From a891e51eaa94fead178e6da2077447198556e283 Mon Sep 17 00:00:00 2001 From: ganevgv Date: Mon, 2 Dec 2019 17:15:48 +0000 Subject: [PATCH 10/10] hardcode precision in examples --- pandas/core/window/expanding.py | 5 ++--- pandas/core/window/rolling.py | 10 ++++------ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/pandas/core/window/expanding.py b/pandas/core/window/expanding.py index 32cf81f320893..68c3514308cbc 100644 --- a/pandas/core/window/expanding.py +++ b/pandas/core/window/expanding.py @@ -209,10 +209,9 @@ def skew(self, **kwargs): >>> arr = [1, 2, 3, 4, 999] >>> import scipy.stats - >>> precision = 6 # limit the printed precision to 6 digits - >>> print(f"{scipy.stats.kurtosis(arr[:-1], bias=False):.{precision}f}") + >>> print(f"{scipy.stats.kurtosis(arr[:-1], bias=False):.6f}") -1.200000 - >>> print(f"{scipy.stats.kurtosis(arr, bias=False):.{precision}f}") + >>> print(f"{scipy.stats.kurtosis(arr, bias=False):.6f}") 4.999874 >>> s = pd.Series(arr) >>> s.expanding(4).kurt() diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 4d69cd81713da..19ec4e335ee21 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1628,12 +1628,11 @@ def _get_cov(X, Y): >>> v1 = [3, 3, 3, 5, 8] >>> v2 = [3, 4, 4, 4, 8] - >>> precision = 6 # limit the printed precision to 6 digits >>> # numpy returns a 2X2 array, the correlation coefficient >>> # is the number at entry [0][1] - >>> print(f"{np.corrcoef(v1[:-1], v2[:-1])[0][1]:.{precision}f}") + >>> print(f"{np.corrcoef(v1[:-1], v2[:-1])[0][1]:.6f}") 0.333333 - >>> print(f"{np.corrcoef(v1[1:], v2[1:])[0][1]:.{precision}f}") + >>> print(f"{np.corrcoef(v1[1:], v2[1:])[0][1]:.6f}") 0.916949 >>> s1 = pd.Series(v1) >>> s2 = pd.Series(v2) @@ -1933,11 +1932,10 @@ def skew(self, **kwargs): four matching the equivalent function call using `scipy.stats`. >>> arr = [1, 2, 3, 4, 999] - >>> precision = 6 # limit the printed precision to 6 digits >>> import scipy.stats - >>> print(f"{scipy.stats.kurtosis(arr[:-1], bias=False):.{precision}f}") + >>> print(f"{scipy.stats.kurtosis(arr[:-1], bias=False):.6f}") -1.200000 - >>> print(f"{scipy.stats.kurtosis(arr[1:], bias=False):.{precision}f}") + >>> print(f"{scipy.stats.kurtosis(arr[1:], bias=False):.6f}") 3.999946 >>> s = pd.Series(arr) >>> s.rolling(4).kurt()