Skip to content

Commit

Permalink
x.__class__ to type(x) (pandas-dev#29904)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaharNaveh authored and proost committed Dec 19, 2019
1 parent ec6b652 commit afdccb7
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_str(self):
idx.name = "foo"
assert not "length={}".format(len(idx)) in str(idx)
assert "'foo'" in str(idx)
assert idx.__class__.__name__ in str(idx)
assert type(idx).__name__ in str(idx)

if hasattr(idx, "tz"):
if idx.tz is not None:
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexes/multi/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def test_fillna(idx):
values[1] = np.nan

if isinstance(index, PeriodIndex):
idx = index.__class__(values, freq=index.freq)
idx = type(index)(values, freq=index.freq)
else:
idx = index.__class__(values)
idx = type(index)(values)

expected = np.array([False] * len(idx), dtype=bool)
expected[1] = True
Expand Down Expand Up @@ -115,7 +115,7 @@ def test_hasnans_isnans(idx):
values = index.values
values[1] = np.nan

index = idx.__class__(values)
index = type(idx)(values)

expected = np.array([False] * len(index), dtype=bool)
expected[1] = True
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/period/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_range_slice_outofbounds(self):

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

tm.assert_frame_equal(df["2013/09/01":"2013/09/30"], empty)
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ def test_fancy(self):
@pytest.mark.parametrize("dtype", [np.int_, np.bool_])
def test_empty_fancy(self, index, dtype):
empty_arr = np.array([], dtype=dtype)
empty_index = index.__class__([])
empty_index = type(index)([])

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

assert index[[]].identical(empty_index)
# np.ndarray only accepts ndarray of int & bool dtypes, so should Index
Expand Down Expand Up @@ -2470,8 +2470,8 @@ def test_copy_name(self):
# GH12309
index = self.create_index()

first = index.__class__(index, copy=True, name="mario")
second = first.__class__(first, copy=False)
first = type(index)(index, copy=True, name="mario")
second = type(first)(first, copy=False)

# Even though "copy=False", we want a new object.
assert first is not second
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/reshape/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ def test_append_preserve_index_name(self):

all_indexes = indexes_can_append + indexes_cannot_append_with_other

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

Expand Down Expand Up @@ -979,7 +979,7 @@ def test_append_same_columns_type(self, index):
@pytest.mark.parametrize(
"df_columns, series_index",
combinations(indexes_can_append, r=2),
ids=lambda x: x.__class__.__name__,
ids=lambda x: type(x).__name__,
)
def test_append_different_columns_types(self, df_columns, series_index):
# GH18359
Expand All @@ -1004,12 +1004,12 @@ def test_append_different_columns_types(self, df_columns, series_index):
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"index_can_append", indexes_can_append, ids=lambda x: x.__class__.__name__
"index_can_append", indexes_can_append, ids=lambda x: type(x).__name__
)
@pytest.mark.parametrize(
"index_cannot_append_with_other",
indexes_cannot_append_with_other,
ids=lambda x: x.__class__.__name__,
ids=lambda x: type(x).__name__,
)
def test_append_different_columns_types_raises(
self, index_can_append, index_cannot_append_with_other
Expand Down
16 changes: 8 additions & 8 deletions pandas/tests/series/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_apply_box(self):
s = pd.Series(vals)
assert s.dtype == "datetime64[ns]"
# boxed value must be Timestamp instance
res = s.apply(lambda x: "{0}_{1}_{2}".format(x.__class__.__name__, x.day, x.tz))
res = s.apply(lambda x: f"{type(x).__name__}_{x.day}_{x.tz}")
exp = pd.Series(["Timestamp_1_None", "Timestamp_2_None"])
tm.assert_series_equal(res, exp)

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

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

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

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def test_value_counts_unique_nunique(self):

result = o.unique()
if isinstance(o, Index):
assert isinstance(result, o.__class__)
assert isinstance(result, type(o))
tm.assert_index_equal(result, orig)
assert result.dtype == orig.dtype
elif is_datetime64tz_dtype(o):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/tseries/holiday/test_holiday.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class TestCalendar(AbstractHolidayCalendar):
rules = []

calendar = get_calendar("TestCalendar")
assert TestCalendar == calendar.__class__
assert TestCalendar == type(calendar)


def test_factory():
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def _check_offsetfunc_works(self, offset, funcname, dt, expected, normalize=Fals
ts = Timestamp(dt) + Nano(5)

if (
offset_s.__class__.__name__ == "DateOffset"
type(offset_s).__name__ == "DateOffset"
and (funcname == "apply" or normalize)
and ts.nanosecond > 0
):
Expand Down Expand Up @@ -395,7 +395,7 @@ def _check_offsetfunc_works(self, offset, funcname, dt, expected, normalize=Fals
ts = Timestamp(dt, tz=tz) + Nano(5)

if (
offset_s.__class__.__name__ == "DateOffset"
type(offset_s).__name__ == "DateOffset"
and (funcname == "apply" or normalize)
and ts.nanosecond > 0
):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/holiday.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def __init__(self, name=None, rules=None):
"""
super().__init__()
if name is None:
name = self.__class__.__name__
name = type(self).__name__
self.name = name

if rules is not None:
Expand Down

0 comments on commit afdccb7

Please sign in to comment.