Skip to content

Commit dbae240

Browse files
ShaharNavehWillAyd
authored andcommitted
x.__class__ to type(x) (#29904)
1 parent 6f03e76 commit dbae240

File tree

10 files changed

+26
-26
lines changed

10 files changed

+26
-26
lines changed

pandas/tests/indexes/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_str(self):
3838
idx.name = "foo"
3939
assert not "length={}".format(len(idx)) in str(idx)
4040
assert "'foo'" in str(idx)
41-
assert idx.__class__.__name__ in str(idx)
41+
assert type(idx).__name__ in str(idx)
4242

4343
if hasattr(idx, "tz"):
4444
if idx.tz is not None:

pandas/tests/indexes/multi/test_missing.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ def test_fillna(idx):
4242
values[1] = np.nan
4343

4444
if isinstance(index, PeriodIndex):
45-
idx = index.__class__(values, freq=index.freq)
45+
idx = type(index)(values, freq=index.freq)
4646
else:
47-
idx = index.__class__(values)
47+
idx = type(index)(values)
4848

4949
expected = np.array([False] * len(idx), dtype=bool)
5050
expected[1] = True
@@ -115,7 +115,7 @@ def test_hasnans_isnans(idx):
115115
values = index.values
116116
values[1] = np.nan
117117

118-
index = idx.__class__(values)
118+
index = type(idx)(values)
119119

120120
expected = np.array([False] * len(index), dtype=bool)
121121
expected[1] = True

pandas/tests/indexes/period/test_partial_slicing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def test_range_slice_outofbounds(self):
123123

124124
for idx in [didx, pidx]:
125125
df = DataFrame(dict(units=[100 + i for i in range(10)]), index=idx)
126-
empty = DataFrame(index=idx.__class__([], freq="D"), columns=["units"])
126+
empty = DataFrame(index=type(idx)([], freq="D"), columns=["units"])
127127
empty["units"] = empty["units"].astype("int64")
128128

129129
tm.assert_frame_equal(df["2013/09/01":"2013/09/30"], empty)

pandas/tests/indexes/test_base.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ def test_fancy(self):
752752
@pytest.mark.parametrize("dtype", [np.int_, np.bool_])
753753
def test_empty_fancy(self, index, dtype):
754754
empty_arr = np.array([], dtype=dtype)
755-
empty_index = index.__class__([])
755+
empty_index = type(index)([])
756756

757757
assert index[[]].identical(empty_index)
758758
assert index[empty_arr].identical(empty_index)
@@ -762,7 +762,7 @@ def test_empty_fancy_raises(self, index):
762762
# pd.DatetimeIndex is excluded, because it overrides getitem and should
763763
# be tested separately.
764764
empty_farr = np.array([], dtype=np.float_)
765-
empty_index = index.__class__([])
765+
empty_index = type(index)([])
766766

767767
assert index[[]].identical(empty_index)
768768
# np.ndarray only accepts ndarray of int & bool dtypes, so should Index
@@ -2446,8 +2446,8 @@ def test_copy_name(self):
24462446
# GH12309
24472447
index = self.create_index()
24482448

2449-
first = index.__class__(index, copy=True, name="mario")
2450-
second = first.__class__(first, copy=False)
2449+
first = type(index)(index, copy=True, name="mario")
2450+
second = type(first)(first, copy=False)
24512451

24522452
# Even though "copy=False", we want a new object.
24532453
assert first is not second

pandas/tests/reshape/test_concat.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ def test_append_preserve_index_name(self):
949949

950950
all_indexes = indexes_can_append + indexes_cannot_append_with_other
951951

952-
@pytest.mark.parametrize("index", all_indexes, ids=lambda x: x.__class__.__name__)
952+
@pytest.mark.parametrize("index", all_indexes, ids=lambda x: type(x).__name__)
953953
def test_append_same_columns_type(self, index):
954954
# GH18359
955955

@@ -979,7 +979,7 @@ def test_append_same_columns_type(self, index):
979979
@pytest.mark.parametrize(
980980
"df_columns, series_index",
981981
combinations(indexes_can_append, r=2),
982-
ids=lambda x: x.__class__.__name__,
982+
ids=lambda x: type(x).__name__,
983983
)
984984
def test_append_different_columns_types(self, df_columns, series_index):
985985
# GH18359
@@ -1004,12 +1004,12 @@ def test_append_different_columns_types(self, df_columns, series_index):
10041004
tm.assert_frame_equal(result, expected)
10051005

10061006
@pytest.mark.parametrize(
1007-
"index_can_append", indexes_can_append, ids=lambda x: x.__class__.__name__
1007+
"index_can_append", indexes_can_append, ids=lambda x: type(x).__name__
10081008
)
10091009
@pytest.mark.parametrize(
10101010
"index_cannot_append_with_other",
10111011
indexes_cannot_append_with_other,
1012-
ids=lambda x: x.__class__.__name__,
1012+
ids=lambda x: type(x).__name__,
10131013
)
10141014
def test_append_different_columns_types_raises(
10151015
self, index_can_append, index_cannot_append_with_other

pandas/tests/series/test_apply.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_apply_box(self):
9292
s = pd.Series(vals)
9393
assert s.dtype == "datetime64[ns]"
9494
# boxed value must be Timestamp instance
95-
res = s.apply(lambda x: "{0}_{1}_{2}".format(x.__class__.__name__, x.day, x.tz))
95+
res = s.apply(lambda x: f"{type(x).__name__}_{x.day}_{x.tz}")
9696
exp = pd.Series(["Timestamp_1_None", "Timestamp_2_None"])
9797
tm.assert_series_equal(res, exp)
9898

@@ -102,23 +102,23 @@ def test_apply_box(self):
102102
]
103103
s = pd.Series(vals)
104104
assert s.dtype == "datetime64[ns, US/Eastern]"
105-
res = s.apply(lambda x: "{0}_{1}_{2}".format(x.__class__.__name__, x.day, x.tz))
105+
res = s.apply(lambda x: f"{type(x).__name__}_{x.day}_{x.tz}")
106106
exp = pd.Series(["Timestamp_1_US/Eastern", "Timestamp_2_US/Eastern"])
107107
tm.assert_series_equal(res, exp)
108108

109109
# timedelta
110110
vals = [pd.Timedelta("1 days"), pd.Timedelta("2 days")]
111111
s = pd.Series(vals)
112112
assert s.dtype == "timedelta64[ns]"
113-
res = s.apply(lambda x: "{0}_{1}".format(x.__class__.__name__, x.days))
113+
res = s.apply(lambda x: f"{type(x).__name__}_{x.days}")
114114
exp = pd.Series(["Timedelta_1", "Timedelta_2"])
115115
tm.assert_series_equal(res, exp)
116116

117117
# period
118118
vals = [pd.Period("2011-01-01", freq="M"), pd.Period("2011-01-02", freq="M")]
119119
s = pd.Series(vals)
120120
assert s.dtype == "Period[M]"
121-
res = s.apply(lambda x: "{0}_{1}".format(x.__class__.__name__, x.freqstr))
121+
res = s.apply(lambda x: f"{type(x).__name__}_{x.freqstr}")
122122
exp = pd.Series(["Period_M", "Period_M"])
123123
tm.assert_series_equal(res, exp)
124124

@@ -614,7 +614,7 @@ def test_map_box(self):
614614
s = pd.Series(vals)
615615
assert s.dtype == "datetime64[ns]"
616616
# boxed value must be Timestamp instance
617-
res = s.map(lambda x: "{0}_{1}_{2}".format(x.__class__.__name__, x.day, x.tz))
617+
res = s.apply(lambda x: f"{type(x).__name__}_{x.day}_{x.tz}")
618618
exp = pd.Series(["Timestamp_1_None", "Timestamp_2_None"])
619619
tm.assert_series_equal(res, exp)
620620

@@ -624,23 +624,23 @@ def test_map_box(self):
624624
]
625625
s = pd.Series(vals)
626626
assert s.dtype == "datetime64[ns, US/Eastern]"
627-
res = s.map(lambda x: "{0}_{1}_{2}".format(x.__class__.__name__, x.day, x.tz))
627+
res = s.apply(lambda x: f"{type(x).__name__}_{x.day}_{x.tz}")
628628
exp = pd.Series(["Timestamp_1_US/Eastern", "Timestamp_2_US/Eastern"])
629629
tm.assert_series_equal(res, exp)
630630

631631
# timedelta
632632
vals = [pd.Timedelta("1 days"), pd.Timedelta("2 days")]
633633
s = pd.Series(vals)
634634
assert s.dtype == "timedelta64[ns]"
635-
res = s.map(lambda x: "{0}_{1}".format(x.__class__.__name__, x.days))
635+
res = s.apply(lambda x: f"{type(x).__name__}_{x.days}")
636636
exp = pd.Series(["Timedelta_1", "Timedelta_2"])
637637
tm.assert_series_equal(res, exp)
638638

639639
# period
640640
vals = [pd.Period("2011-01-01", freq="M"), pd.Period("2011-01-02", freq="M")]
641641
s = pd.Series(vals)
642642
assert s.dtype == "Period[M]"
643-
res = s.map(lambda x: "{0}_{1}".format(x.__class__.__name__, x.freqstr))
643+
res = s.apply(lambda x: f"{type(x).__name__}_{x.freqstr}")
644644
exp = pd.Series(["Period_M", "Period_M"])
645645
tm.assert_series_equal(res, exp)
646646

pandas/tests/test_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def test_value_counts_unique_nunique(self):
400400

401401
result = o.unique()
402402
if isinstance(o, Index):
403-
assert isinstance(result, o.__class__)
403+
assert isinstance(result, type(o))
404404
tm.assert_index_equal(result, orig)
405405
assert result.dtype == orig.dtype
406406
elif is_datetime64tz_dtype(o):

pandas/tests/tseries/holiday/test_holiday.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class TestCalendar(AbstractHolidayCalendar):
238238
rules = []
239239

240240
calendar = get_calendar("TestCalendar")
241-
assert TestCalendar == calendar.__class__
241+
assert TestCalendar == type(calendar)
242242

243243

244244
def test_factory():

pandas/tests/tseries/offsets/test_offsets.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def _check_offsetfunc_works(self, offset, funcname, dt, expected, normalize=Fals
358358
ts = Timestamp(dt) + Nano(5)
359359

360360
if (
361-
offset_s.__class__.__name__ == "DateOffset"
361+
type(offset_s).__name__ == "DateOffset"
362362
and (funcname == "apply" or normalize)
363363
and ts.nanosecond > 0
364364
):
@@ -395,7 +395,7 @@ def _check_offsetfunc_works(self, offset, funcname, dt, expected, normalize=Fals
395395
ts = Timestamp(dt, tz=tz) + Nano(5)
396396

397397
if (
398-
offset_s.__class__.__name__ == "DateOffset"
398+
type(offset_s).__name__ == "DateOffset"
399399
and (funcname == "apply" or normalize)
400400
and ts.nanosecond > 0
401401
):

pandas/tseries/holiday.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def __init__(self, name=None, rules=None):
363363
"""
364364
super().__init__()
365365
if name is None:
366-
name = self.__class__.__name__
366+
name = type(self).__name__
367367
self.name = name
368368

369369
if rules is not None:

0 commit comments

Comments
 (0)