diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index b18f0db622b3e..f8f5d337185c4 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -352,7 +352,7 @@ def to_string(self) -> str: if len(series) == 0: return "{name}([], {footer})".format( - name=self.series.__class__.__name__, footer=footer + name=type(self.series).__name__, footer=footer ) fmt_index, have_header = self._get_formatted_index() diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index a4f1488fb6b69..8218799129952 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -321,7 +321,7 @@ def format_object_summary( if display_width is None: display_width = get_option("display.width") or 80 if name is None: - name = obj.__class__.__name__ + name = type(obj).__name__ if indent_for_name: name_len = len(name) diff --git a/pandas/io/packers.py b/pandas/io/packers.py index 253441ab25813..bb7b00571b0df 100644 --- a/pandas/io/packers.py +++ b/pandas/io/packers.py @@ -404,7 +404,7 @@ def encode(obj): if isinstance(obj, RangeIndex): return { "typ": "range_index", - "klass": obj.__class__.__name__, + "klass": type(obj).__name__, "name": getattr(obj, "name", None), "start": obj._range.start, "stop": obj._range.stop, @@ -413,7 +413,7 @@ def encode(obj): elif isinstance(obj, PeriodIndex): return { "typ": "period_index", - "klass": obj.__class__.__name__, + "klass": type(obj).__name__, "name": getattr(obj, "name", None), "freq": getattr(obj, "freqstr", None), "dtype": obj.dtype.name, @@ -429,7 +429,7 @@ def encode(obj): obj = obj.tz_convert("UTC") return { "typ": "datetime_index", - "klass": obj.__class__.__name__, + "klass": type(obj).__name__, "name": getattr(obj, "name", None), "dtype": obj.dtype.name, "data": convert(obj.asi8), @@ -444,7 +444,7 @@ def encode(obj): typ = "interval_array" return { "typ": typ, - "klass": obj.__class__.__name__, + "klass": type(obj).__name__, "name": getattr(obj, "name", None), "left": getattr(obj, "left", None), "right": getattr(obj, "right", None), @@ -453,7 +453,7 @@ def encode(obj): elif isinstance(obj, MultiIndex): return { "typ": "multi_index", - "klass": obj.__class__.__name__, + "klass": type(obj).__name__, "names": getattr(obj, "names", None), "dtype": obj.dtype.name, "data": convert(obj.values), @@ -462,7 +462,7 @@ def encode(obj): else: return { "typ": "index", - "klass": obj.__class__.__name__, + "klass": type(obj).__name__, "name": getattr(obj, "name", None), "dtype": obj.dtype.name, "data": convert(obj.values), @@ -472,7 +472,7 @@ def encode(obj): elif isinstance(obj, Categorical): return { "typ": "category", - "klass": obj.__class__.__name__, + "klass": type(obj).__name__, "name": getattr(obj, "name", None), "codes": obj.codes, "categories": obj.categories, @@ -483,7 +483,7 @@ def encode(obj): elif isinstance(obj, Series): return { "typ": "series", - "klass": obj.__class__.__name__, + "klass": type(obj).__name__, "name": getattr(obj, "name", None), "index": obj.index, "dtype": obj.dtype.name, @@ -498,7 +498,7 @@ def encode(obj): # the block manager return { "typ": "block_manager", - "klass": obj.__class__.__name__, + "klass": type(obj).__name__, "axes": data.axes, "blocks": [ { @@ -506,7 +506,7 @@ def encode(obj): "values": convert(b.values), "shape": b.values.shape, "dtype": b.dtype.name, - "klass": b.__class__.__name__, + "klass": type(b).__name__, "compress": compressor, } for b in data.blocks @@ -553,7 +553,7 @@ def encode(obj): elif isinstance(obj, BlockIndex): return { "typ": "block_index", - "klass": obj.__class__.__name__, + "klass": type(obj).__name__, "blocs": obj.blocs, "blengths": obj.blengths, "length": obj.length, @@ -561,7 +561,7 @@ def encode(obj): elif isinstance(obj, IntIndex): return { "typ": "int_index", - "klass": obj.__class__.__name__, + "klass": type(obj).__name__, "indices": obj.indices, "length": obj.length, } diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index bf7aa5970519f..fb63bdcaaa876 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -3692,7 +3692,7 @@ def create_axes( # the non_index_axes info info = _get_info(self.info, i) info["names"] = list(a.names) - info["type"] = a.__class__.__name__ + info["type"] = type(a).__name__ self.non_index_axes.append((i, append_axis)) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index bd5e215730397..59bb4e3bf236a 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -856,12 +856,11 @@ def __str__(self) -> str: return self.string def __repr__(self) -> str: - # not perfect :-/ - return "{cls}({obj})".format(cls=self.__class__, obj=self) + return f"{type(self)}({self})" def __eq__(self, other) -> bool: return ( - isinstance(other, self.__class__) + isinstance(other, type(self)) and self.string == other.string and self.value == other.value ) diff --git a/pandas/tests/extension/base/dtype.py b/pandas/tests/extension/base/dtype.py index a5040c8cfc2fc..d1e1717225e15 100644 --- a/pandas/tests/extension/base/dtype.py +++ b/pandas/tests/extension/base/dtype.py @@ -96,7 +96,7 @@ def test_eq(self, dtype): assert dtype != "anonther_type" def test_construct_from_string(self, dtype): - dtype_instance = dtype.__class__.construct_from_string(dtype.name) - assert isinstance(dtype_instance, dtype.__class__) + dtype_instance = type(dtype).construct_from_string(dtype.name) + assert isinstance(dtype_instance, type(dtype)) with pytest.raises(TypeError): - dtype.__class__.construct_from_string("another_type") + type(dtype).construct_from_string("another_type") diff --git a/pandas/tests/extension/base/ops.py b/pandas/tests/extension/base/ops.py index 5e4fb6d69e52c..20d06ef2e5647 100644 --- a/pandas/tests/extension/base/ops.py +++ b/pandas/tests/extension/base/ops.py @@ -123,9 +123,7 @@ def test_direct_arith_with_series_returns_not_implemented(self, data): result = data.__add__(other) assert result is NotImplemented else: - raise pytest.skip( - "{} does not implement add".format(data.__class__.__name__) - ) + raise pytest.skip(f"{type(data).__name__} does not implement add") class BaseComparisonOpsTests(BaseOpsUtil): @@ -169,6 +167,4 @@ def test_direct_arith_with_series_returns_not_implemented(self, data): result = data.__eq__(other) assert result is NotImplemented else: - raise pytest.skip( - "{} does not implement __eq__".format(data.__class__.__name__) - ) + raise pytest.skip(f"{type(data).__name__} does not implement __eq__") diff --git a/pandas/tests/extension/base/printing.py b/pandas/tests/extension/base/printing.py index 0f10efbf32a49..5d17a4b0cbee2 100644 --- a/pandas/tests/extension/base/printing.py +++ b/pandas/tests/extension/base/printing.py @@ -18,7 +18,7 @@ def test_array_repr(self, data, size): data = type(data)._concat_same_type([data] * 5) result = repr(data) - assert data.__class__.__name__ in result + assert type(data).__name__ in result assert "Length: {}".format(len(data)) in result assert str(data.dtype) in result if size == "big": diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index 3c97a87c95bd2..26a3c738750ca 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -642,7 +642,7 @@ def test_applymap_box(self): } ) - result = df.applymap(lambda x: "{0}".format(x.__class__.__name__)) + result = df.applymap(lambda x: type(x).__name__) expected = pd.DataFrame( { "a": ["Timestamp", "Timestamp"], diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index c35c4c3568f74..102949fe3f05e 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -244,7 +244,7 @@ def test_str(self): idx = self.create_index() idx.name = "foo" assert "'foo'" in str(idx) - assert idx.__class__.__name__ in str(idx) + assert type(idx).__name__ in str(idx) def test_repr_max_seq_item_setting(self): # GH10182 @@ -260,8 +260,8 @@ def test_copy_name(self, indices): if isinstance(indices, MultiIndex): return - first = indices.__class__(indices, copy=True, name="mario") - second = first.__class__(first, copy=False) + first = type(indices)(indices, copy=True, name="mario") + second = type(first)(first, copy=False) # Even though "copy=False", we want a new object. assert first is not second @@ -292,7 +292,7 @@ def test_ensure_copied_data(self, indices): # MultiIndex and CategoricalIndex are tested separately return - index_type = indices.__class__ + index_type = type(indices) result = index_type(indices.values, copy=True, **init_kwargs) tm.assert_index_equal(indices, result) tm.assert_numpy_array_equal( @@ -502,7 +502,7 @@ def test_difference_base(self, sort, indices): cases = [klass(second.values) for klass in [np.array, Series, list]] for case in cases: if isinstance(indices, (DatetimeIndex, TimedeltaIndex)): - assert result.__class__ == answer.__class__ + assert type(result) == type(answer) tm.assert_numpy_array_equal( result.sort_values().asi8, answer.sort_values().asi8 ) @@ -677,9 +677,9 @@ def test_hasnans_isnans(self, indices): values[1] = np.nan if isinstance(indices, PeriodIndex): - idx = indices.__class__(values, freq=indices.freq) + idx = type(indices)(values, freq=indices.freq) else: - idx = indices.__class__(values) + idx = type(indices)(values) expected = np.array([False] * len(idx), dtype=bool) expected[1] = True @@ -716,9 +716,9 @@ def test_fillna(self, indices): values[1] = np.nan if isinstance(indices, PeriodIndex): - idx = indices.__class__(values, freq=indices.freq) + idx = type(indices)(values, freq=indices.freq) else: - idx = indices.__class__(values) + idx = type(indices)(values) expected = np.array([False] * len(idx), dtype=bool) expected[1] = True