From 1b016c44fadc0381f33dd5d70cf7e7ea86168c8f Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 27 Nov 2019 18:20:11 +0200 Subject: [PATCH] x.__class__ TO type(x) --- pandas/_libs/internals.pyx | 2 +- pandas/_libs/tslibs/c_timestamp.pyx | 21 +++++++++------------ pandas/_libs/tslibs/offsets.pyx | 2 +- pandas/core/arrays/base.py | 2 +- pandas/core/arrays/interval.py | 4 ++-- pandas/core/base.py | 4 ++-- pandas/core/common.py | 2 +- pandas/core/computation/expr.py | 2 +- pandas/core/computation/ops.py | 2 +- pandas/core/computation/pytables.py | 2 +- 10 files changed, 20 insertions(+), 23 deletions(-) diff --git a/pandas/_libs/internals.pyx b/pandas/_libs/internals.pyx index ba108c4524b9c..603caed805e58 100644 --- a/pandas/_libs/internals.pyx +++ b/pandas/_libs/internals.pyx @@ -54,7 +54,7 @@ cdef class BlockPlacement: else: v = self._as_array - return f'{self.__class__.__name__}({v})' + return f'{type(self).__name__}({v})' def __repr__(self) -> str: return str(self) diff --git a/pandas/_libs/tslibs/c_timestamp.pyx b/pandas/_libs/tslibs/c_timestamp.pyx index c6c98e996b745..02e252219453b 100644 --- a/pandas/_libs/tslibs/c_timestamp.pyx +++ b/pandas/_libs/tslibs/c_timestamp.pyx @@ -87,7 +87,7 @@ cdef class _Timestamp(datetime): return PyObject_RichCompareBool(val, other, op) try: - ots = self.__class__(other) + ots = type(self)(other) except ValueError: return self._compare_outside_nanorange(other, op) else: @@ -96,7 +96,7 @@ cdef class _Timestamp(datetime): if ndim != -1: if ndim == 0: if is_datetime64_object(other): - other = self.__class__(other) + other = type(self)(other) elif is_array(other): # zero-dim array, occurs if try comparison with # datetime64 scalar on the left hand side @@ -105,7 +105,7 @@ cdef class _Timestamp(datetime): # the numpy C api to extract it. other = cnp.PyArray_ToScalar(cnp.PyArray_DATA(other), other) - other = self.__class__(other) + other = type(self)(other) else: return NotImplemented elif is_array(other): @@ -226,8 +226,7 @@ cdef class _Timestamp(datetime): if is_timedelta64_object(other): other_int = other.astype('timedelta64[ns]').view('i8') - return self.__class__(self.value + other_int, - tz=self.tzinfo, freq=self.freq) + return type(self)(self.value + other_int, tz=self.tzinfo, freq=self.freq) elif is_integer_object(other): maybe_integer_op_deprecated(self) @@ -238,8 +237,7 @@ cdef class _Timestamp(datetime): elif self.freq is None: raise NullFrequencyError( "Cannot add integral value to Timestamp without freq.") - return self.__class__((self.freq * other).apply(self), - freq=self.freq) + return type(self)((self.freq * other).apply(self), freq=self.freq) elif PyDelta_Check(other) or hasattr(other, 'delta'): # delta --> offsets.Tick @@ -253,8 +251,7 @@ cdef class _Timestamp(datetime): other.seconds * 1000000 + other.microseconds) * 1000 - result = self.__class__(self.value + nanos, - tz=self.tzinfo, freq=self.freq) + result = type(self)(self.value + nanos, tz=self.tzinfo, freq=self.freq) return result elif is_array(other): @@ -272,7 +269,7 @@ cdef class _Timestamp(datetime): result = datetime.__add__(self, other) if PyDateTime_Check(result): - result = self.__class__(result) + result = type(self)(result) result.nanosecond = self.nanosecond return result @@ -304,9 +301,9 @@ cdef class _Timestamp(datetime): if (PyDateTime_Check(self) and (PyDateTime_Check(other) or is_datetime64_object(other))): if isinstance(self, _Timestamp): - other = self.__class__(other) + other = type(self)(other) else: - self = other.__class__(self) + self = type(other)(self) # validate tz's if not tz_compare(self.tzinfo, other.tzinfo): diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 327d1067dd17d..c8985c365741d 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -363,7 +363,7 @@ class _BaseOffset: attrs = [(k, v) for k, v in all_paras.items() if (k not in exclude) and (k[0] != '_')] attrs = sorted(set(attrs)) - params = tuple([str(self.__class__)] + attrs) + params = tuple([str(type(self))] + attrs) return params @property diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index a444a4e46d0d7..bf50d6e9b50e7 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -923,7 +923,7 @@ def __repr__(self) -> str: data = format_object_summary( self, self._formatter(), indent_for_name=False ).rstrip(", \n") - class_name = "<{}>\n".format(self.__class__.__name__) + class_name = "<{}>\n".format(type(self).__name__) return template.format( class_name=class_name, data=data, length=len(self), dtype=self.dtype ) diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index cb482665b3534..ab558b8fa75d6 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -870,7 +870,7 @@ def __repr__(self) -> str: # repr does. So we include a newline in our template, and strip # any trailing newlines from format_object_summary data = self._format_data() - class_name = "<{}>\n".format(self.__class__.__name__) + class_name = "<{}>\n".format(type(self).__name__) return template.format( class_name=class_name, data=data, @@ -880,7 +880,7 @@ def __repr__(self) -> str: ) def _format_space(self): - space = " " * (len(self.__class__.__name__) + 1) + space = " " * (len(type(self).__name__) + 1) return "\n{space}".format(space=space) @property diff --git a/pandas/core/base.py b/pandas/core/base.py index 176a92132e20a..83d6ac76cdd98 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -51,7 +51,7 @@ class PandasObject(DirNamesMixin): @property def _constructor(self): """class constructor (for this class it's just `__class__`""" - return self.__class__ + return type(self) def __repr__(self) -> str: """ @@ -1185,7 +1185,7 @@ def _reduce( if func is None: raise TypeError( "{klass} cannot perform the operation {op}".format( - klass=self.__class__.__name__, op=name + klass=type(self).__name__, op=name ) ) return func(skipna=skipna, **kwds) diff --git a/pandas/core/common.py b/pandas/core/common.py index 41b6ebbd2f196..d62f1557952a8 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -317,7 +317,7 @@ def get_callable_name(obj): return get_callable_name(obj.func) # fall back to class name if hasattr(obj, "__call__"): - return obj.__class__.__name__ + return type(obj).__name__ # everything failed (probably because the argument # wasn't actually callable); we return None # instead of the empty string in this case to allow diff --git a/pandas/core/computation/expr.py b/pandas/core/computation/expr.py index 95785af8dc5ea..e608f82b03ade 100644 --- a/pandas/core/computation/expr.py +++ b/pandas/core/computation/expr.py @@ -435,7 +435,7 @@ def visit(self, node, **kwargs): e.msg = "Python keyword not valid identifier in numexpr query" raise e - method = "visit_" + node.__class__.__name__ + method = "visit_" + type(node).__name__ visitor = getattr(self, method) return visitor(node, **kwargs) diff --git a/pandas/core/computation/ops.py b/pandas/core/computation/ops.py index 983382dce717a..4852e498537f2 100644 --- a/pandas/core/computation/ops.py +++ b/pandas/core/computation/ops.py @@ -145,7 +145,7 @@ def type(self): def raw(self) -> str: return pprint_thing( "{0}(name={1!r}, type={2})" - "".format(self.__class__.__name__, self.name, self.type) + "".format(type(self).__name__, self.name, self.type) ) @property diff --git a/pandas/core/computation/pytables.py b/pandas/core/computation/pytables.py index 58bbfd0a1bdee..65e38ff290ce4 100644 --- a/pandas/core/computation/pytables.py +++ b/pandas/core/computation/pytables.py @@ -440,7 +440,7 @@ def visit_Attribute(self, node, **kwargs): attr = node.attr value = node.value - ctx = node.ctx.__class__ + ctx = type(node.ctx) if ctx == ast.Load: # resolve the value resolved = self.visit(value)