Skip to content

Commit f34dbbf

Browse files
jbrockmendeljreback
authored andcommitted
CLN: Assorted cleanups (#27632)
1 parent 61362be commit f34dbbf

34 files changed

+168
-220
lines changed

pandas/core/accessor.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class PandasDelegate:
5151
"""
5252

5353
def _delegate_property_get(self, name, *args, **kwargs):
54-
raise TypeError("You cannot access the " "property {name}".format(name=name))
54+
raise TypeError("You cannot access the property {name}".format(name=name))
5555

5656
def _delegate_property_set(self, name, value, *args, **kwargs):
5757
raise TypeError("The property {name} cannot be set".format(name=name))
@@ -271,8 +271,7 @@ def plot(self):
271271
@Appender(
272272
_doc
273273
% dict(
274-
klass="DataFrame",
275-
others=("register_series_accessor, " "register_index_accessor"),
274+
klass="DataFrame", others=("register_series_accessor, register_index_accessor")
276275
)
277276
)
278277
def register_dataframe_accessor(name):
@@ -284,8 +283,7 @@ def register_dataframe_accessor(name):
284283
@Appender(
285284
_doc
286285
% dict(
287-
klass="Series",
288-
others=("register_dataframe_accessor, " "register_index_accessor"),
286+
klass="Series", others=("register_dataframe_accessor, register_index_accessor")
289287
)
290288
)
291289
def register_series_accessor(name):
@@ -297,8 +295,7 @@ def register_series_accessor(name):
297295
@Appender(
298296
_doc
299297
% dict(
300-
klass="Index",
301-
others=("register_dataframe_accessor, " "register_series_accessor"),
298+
klass="Index", others=("register_dataframe_accessor, register_series_accessor")
302299
)
303300
)
304301
def register_index_accessor(name):

pandas/core/arrays/base.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414
from pandas.compat.numpy import function as nv
1515
from pandas.errors import AbstractMethodError
1616
from pandas.util._decorators import Appender, Substitution
17+
from pandas.util._validators import validate_fillna_kwargs
1718

18-
from pandas.core.dtypes.common import is_list_like
19+
from pandas.core.dtypes.common import is_array_like, is_list_like
1920
from pandas.core.dtypes.dtypes import ExtensionDtype
2021
from pandas.core.dtypes.generic import ABCExtensionArray, ABCIndexClass, ABCSeries
2122
from pandas.core.dtypes.missing import isna
2223

2324
from pandas._typing import ArrayLike
2425
from pandas.core import ops
26+
from pandas.core.algorithms import _factorize_array, unique
27+
from pandas.core.missing import backfill_1d, pad_1d
2528
from pandas.core.sorting import nargsort
2629

2730
_not_implemented_message = "{} does not implement {}."
@@ -484,10 +487,6 @@ def fillna(self, value=None, method=None, limit=None):
484487
-------
485488
filled : ExtensionArray with NA/NaN filled
486489
"""
487-
from pandas.api.types import is_array_like
488-
from pandas.util._validators import validate_fillna_kwargs
489-
from pandas.core.missing import pad_1d, backfill_1d
490-
491490
value, method = validate_fillna_kwargs(value, method)
492491

493492
mask = self.isna()
@@ -584,8 +583,6 @@ def unique(self):
584583
-------
585584
uniques : ExtensionArray
586585
"""
587-
from pandas import unique
588-
589586
uniques = unique(self.astype(object))
590587
return self._from_sequence(uniques, dtype=self.dtype)
591588

@@ -700,8 +697,6 @@ def factorize(self, na_sentinel: int = -1) -> Tuple[np.ndarray, ABCExtensionArra
700697
# original ExtensionArray.
701698
# 2. ExtensionArray.factorize.
702699
# Complete control over factorization.
703-
from pandas.core.algorithms import _factorize_array
704-
705700
arr, na_value = self._values_for_factorize()
706701

707702
labels, uniques = _factorize_array(
@@ -874,7 +869,7 @@ def copy(self) -> ABCExtensionArray:
874869
def __repr__(self):
875870
from pandas.io.formats.printing import format_object_summary
876871

877-
template = "{class_name}" "{data}\n" "Length: {length}, dtype: {dtype}"
872+
template = "{class_name}{data}\nLength: {length}, dtype: {dtype}"
878873
# the short repr has no trailing newline, while the truncated
879874
# repr does. So we include a newline in our template, and strip
880875
# any trailing newlines from format_object_summary

pandas/core/arrays/categorical.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ def f(self, other):
9393
if not self.ordered:
9494
if op in ["__lt__", "__gt__", "__le__", "__ge__"]:
9595
raise TypeError(
96-
"Unordered Categoricals can only compare " "equality or not"
96+
"Unordered Categoricals can only compare equality or not"
9797
)
9898
if isinstance(other, Categorical):
9999
# Two Categoricals can only be be compared if the categories are
100100
# the same (maybe up to ordering, depending on ordered)
101101

102-
msg = "Categoricals can only be compared if " "'categories' are the same."
102+
msg = "Categoricals can only be compared if 'categories' are the same."
103103
if len(self.categories) != len(other.categories):
104104
raise TypeError(msg + " Categories are different lengths")
105105
elif self.ordered and not (self.categories == other.categories).all():
@@ -109,7 +109,7 @@ def f(self, other):
109109

110110
if not (self.ordered == other.ordered):
111111
raise TypeError(
112-
"Categoricals can only be compared if " "'ordered' is the same"
112+
"Categoricals can only be compared if 'ordered' is the same"
113113
)
114114
if not self.ordered and not self.categories.equals(other.categories):
115115
# both unordered and different order
@@ -387,7 +387,7 @@ def __init__(
387387

388388
# FIXME
389389
raise NotImplementedError(
390-
"> 1 ndim Categorical are not " "supported at this time"
390+
"> 1 ndim Categorical are not supported at this time"
391391
)
392392

393393
# we're inferring from values
@@ -694,7 +694,7 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
694694
raise ValueError(msg)
695695

696696
if len(codes) and (codes.max() >= len(dtype.categories) or codes.min() < -1):
697-
raise ValueError("codes need to be between -1 and " "len(categories)-1")
697+
raise ValueError("codes need to be between -1 and len(categories)-1")
698698

699699
return cls(codes, dtype=dtype, fastpath=True)
700700

@@ -1019,7 +1019,7 @@ def reorder_categories(self, new_categories, ordered=None, inplace=False):
10191019
inplace = validate_bool_kwarg(inplace, "inplace")
10201020
if set(self.dtype.categories) != set(new_categories):
10211021
raise ValueError(
1022-
"items in new_categories are not the same as in " "old categories"
1022+
"items in new_categories are not the same as in old categories"
10231023
)
10241024
return self.set_categories(new_categories, ordered=ordered, inplace=inplace)
10251025

@@ -1481,7 +1481,7 @@ def put(self, *args, **kwargs):
14811481
"""
14821482
Replace specific elements in the Categorical with given values.
14831483
"""
1484-
raise NotImplementedError(("'put' is not yet implemented " "for Categorical"))
1484+
raise NotImplementedError(("'put' is not yet implemented for Categorical"))
14851485

14861486
def dropna(self):
14871487
"""
@@ -1827,7 +1827,7 @@ def fillna(self, value=None, method=None, limit=None):
18271827
value = np.nan
18281828
if limit is not None:
18291829
raise NotImplementedError(
1830-
"specifying a limit for fillna has not " "been implemented yet"
1830+
"specifying a limit for fillna has not been implemented yet"
18311831
)
18321832

18331833
codes = self._codes
@@ -1963,7 +1963,7 @@ def take_nd(self, indexer, allow_fill=None, fill_value=None):
19631963
if fill_value in self.categories:
19641964
fill_value = self.categories.get_loc(fill_value)
19651965
else:
1966-
msg = "'fill_value' ('{}') is not in this Categorical's " "categories."
1966+
msg = "'fill_value' ('{}') is not in this Categorical's categories."
19671967
raise TypeError(msg.format(fill_value))
19681968

19691969
codes = take(self._codes, indexer, allow_fill=allow_fill, fill_value=fill_value)
@@ -2168,12 +2168,12 @@ def __setitem__(self, key, value):
21682168
# in a 2-d case be passd (slice(None),....)
21692169
if len(key) == 2:
21702170
if not com.is_null_slice(key[0]):
2171-
raise AssertionError("invalid slicing for a 1-ndim " "categorical")
2171+
raise AssertionError("invalid slicing for a 1-ndim categorical")
21722172
key = key[1]
21732173
elif len(key) == 1:
21742174
key = key[0]
21752175
else:
2176-
raise AssertionError("invalid slicing for a 1-ndim " "categorical")
2176+
raise AssertionError("invalid slicing for a 1-ndim categorical")
21772177

21782178
# slicing in Series or Categorical
21792179
elif isinstance(key, slice):
@@ -2561,9 +2561,7 @@ def __init__(self, data):
25612561
@staticmethod
25622562
def _validate(data):
25632563
if not is_categorical_dtype(data.dtype):
2564-
raise AttributeError(
2565-
"Can only use .cat accessor with a " "'category' dtype"
2566-
)
2564+
raise AttributeError("Can only use .cat accessor with a 'category' dtype")
25672565

25682566
def _delegate_property_get(self, name):
25692567
return getattr(self._parent, name)
@@ -2607,7 +2605,7 @@ def name(self):
26072605
# need to be updated. `name` will need to be removed from
26082606
# `ok_for_cat`.
26092607
warn(
2610-
"`Series.cat.name` has been deprecated. Use `Series.name` " "instead.",
2608+
"`Series.cat.name` has been deprecated. Use `Series.name` instead.",
26112609
FutureWarning,
26122610
stacklevel=2,
26132611
)
@@ -2619,7 +2617,7 @@ def index(self):
26192617
# need to be updated. `index` will need to be removed from
26202618
# ok_for_cat`.
26212619
warn(
2622-
"`Series.cat.index` has been deprecated. Use `Series.index` " "instead.",
2620+
"`Series.cat.index` has been deprecated. Use `Series.index` instead.",
26232621
FutureWarning,
26242622
stacklevel=2,
26252623
)

pandas/core/arrays/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ def _sub_period_array(self, other):
10971097
)
10981098

10991099
if len(self) != len(other):
1100-
raise ValueError("cannot subtract arrays/indices of " "unequal length")
1100+
raise ValueError("cannot subtract arrays/indices of unequal length")
11011101
if self.freq != other.freq:
11021102
msg = DIFFERENT_FREQ.format(
11031103
cls=type(self).__name__, own_freq=self.freqstr, other_freq=other.freqstr

pandas/core/arrays/datetimes.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def _generate_range(
478478

479479
periods = dtl.validate_periods(periods)
480480
if freq is None and any(x is None for x in [periods, start, end]):
481-
raise ValueError("Must provide freq argument if no data is " "supplied")
481+
raise ValueError("Must provide freq argument if no data is supplied")
482482

483483
if com.count_not_none(start, end, periods, freq) != 3:
484484
raise ValueError(
@@ -496,7 +496,7 @@ def _generate_range(
496496
if start is None and end is None:
497497
if closed is not None:
498498
raise ValueError(
499-
"Closed has to be None if not both of start" "and end are defined"
499+
"Closed has to be None if not both of startand end are defined"
500500
)
501501
if start is NaT or end is NaT:
502502
raise ValueError("Neither `start` nor `end` can be NaT")
@@ -786,11 +786,11 @@ def _assert_tzawareness_compat(self, other):
786786
elif self.tz is None:
787787
if other_tz is not None:
788788
raise TypeError(
789-
"Cannot compare tz-naive and tz-aware " "datetime-like objects."
789+
"Cannot compare tz-naive and tz-aware datetime-like objects."
790790
)
791791
elif other_tz is None:
792792
raise TypeError(
793-
"Cannot compare tz-naive and tz-aware " "datetime-like objects"
793+
"Cannot compare tz-naive and tz-aware datetime-like objects"
794794
)
795795

796796
# -----------------------------------------------------------------
@@ -833,7 +833,7 @@ def _add_offset(self, offset):
833833

834834
except NotImplementedError:
835835
warnings.warn(
836-
"Non-vectorized DateOffset being applied to Series " "or DatetimeIndex",
836+
"Non-vectorized DateOffset being applied to Series or DatetimeIndex",
837837
PerformanceWarning,
838838
)
839839
result = self.astype("O") + offset
@@ -851,7 +851,7 @@ def _sub_datetimelike_scalar(self, other):
851851
if not self._has_same_tz(other):
852852
# require tz compat
853853
raise TypeError(
854-
"Timestamp subtraction must have the same " "timezones or no timezones"
854+
"Timestamp subtraction must have the same timezones or no timezones"
855855
)
856856

857857
i8 = self.asi8
@@ -957,7 +957,7 @@ def tz_convert(self, tz):
957957
if self.tz is None:
958958
# tz naive, use tz_localize
959959
raise TypeError(
960-
"Cannot convert tz-naive timestamps, use " "tz_localize to localize"
960+
"Cannot convert tz-naive timestamps, use tz_localize to localize"
961961
)
962962

963963
# No conversion since timestamps are all UTC to begin with
@@ -1125,7 +1125,7 @@ def tz_localize(self, tz, ambiguous="raise", nonexistent="raise", errors=None):
11251125
nonexistent = "raise"
11261126
else:
11271127
raise ValueError(
1128-
"The errors argument must be either 'coerce' " "or 'raise'."
1128+
"The errors argument must be either 'coerce' or 'raise'."
11291129
)
11301130

11311131
nonexistent_options = ("raise", "NaT", "shift_forward", "shift_backward")
@@ -1274,7 +1274,7 @@ def to_period(self, freq=None):
12741274

12751275
if freq is None:
12761276
raise ValueError(
1277-
"You must pass a freq argument as " "current index has none."
1277+
"You must pass a freq argument as current index has none."
12781278
)
12791279

12801280
freq = get_period_alias(freq)
@@ -2047,7 +2047,7 @@ def maybe_convert_dtype(data, copy):
20472047
# Note: without explicitly raising here, PeriodIndex
20482048
# test_setops.test_join_does_not_recur fails
20492049
raise TypeError(
2050-
"Passing PeriodDtype data is invalid. " "Use `data.to_timestamp()` instead"
2050+
"Passing PeriodDtype data is invalid. Use `data.to_timestamp()` instead"
20512051
)
20522052

20532053
elif is_categorical_dtype(data):
@@ -2177,7 +2177,7 @@ def validate_tz_from_dtype(dtype, tz):
21772177
dtz = getattr(dtype, "tz", None)
21782178
if dtz is not None:
21792179
if tz is not None and not timezones.tz_compare(tz, dtz):
2180-
raise ValueError("cannot supply both a tz and a dtype" " with a tz")
2180+
raise ValueError("cannot supply both a tz and a dtype with a tz")
21812181
tz = dtz
21822182

21832183
if tz is not None and is_datetime64_dtype(dtype):
@@ -2216,15 +2216,15 @@ def _infer_tz_from_endpoints(start, end, tz):
22162216
inferred_tz = timezones.infer_tzinfo(start, end)
22172217
except Exception:
22182218
raise TypeError(
2219-
"Start and end cannot both be tz-aware with " "different timezones"
2219+
"Start and end cannot both be tz-aware with different timezones"
22202220
)
22212221

22222222
inferred_tz = timezones.maybe_get_tz(inferred_tz)
22232223
tz = timezones.maybe_get_tz(tz)
22242224

22252225
if tz is not None and inferred_tz is not None:
22262226
if not timezones.tz_compare(inferred_tz, tz):
2227-
raise AssertionError("Inferred time zone not equal to passed " "time zone")
2227+
raise AssertionError("Inferred time zone not equal to passed time zone")
22282228

22292229
elif inferred_tz is not None:
22302230
tz = inferred_tz

0 commit comments

Comments
 (0)