Skip to content

Commit

Permalink
x.__class__ TO type(x) (#29889)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaharNaveh authored and jreback committed Nov 27, 2019
1 parent ab64dd7 commit 2ca9ed1
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pandas/_libs/internals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 9 additions & 12 deletions pandas/_libs/tslibs/c_timestamp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/computation/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/computation/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2ca9ed1

Please sign in to comment.