From ad40b622c09ecfaccc2b80cccce98ece94247320 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Thu, 28 Nov 2019 00:43:32 +0200 Subject: [PATCH] x.__class__ to type(x) --- pandas/tests/indexes/datetimelike.py | 2 +- pandas/tests/indexes/multi/test_missing.py | 6 +++--- .../tests/indexes/period/test_partial_slicing.py | 2 +- pandas/tests/indexes/test_base.py | 8 ++++---- pandas/tests/reshape/test_concat.py | 8 ++++---- pandas/tests/series/test_apply.py | 16 ++++++++-------- pandas/tests/test_base.py | 2 +- pandas/tests/tseries/holiday/test_holiday.py | 2 +- pandas/tests/tseries/offsets/test_offsets.py | 4 ++-- pandas/tseries/holiday.py | 2 +- 10 files changed, 26 insertions(+), 26 deletions(-) diff --git a/pandas/tests/indexes/datetimelike.py b/pandas/tests/indexes/datetimelike.py index e6e38ce9921f5..42244626749b9 100644 --- a/pandas/tests/indexes/datetimelike.py +++ b/pandas/tests/indexes/datetimelike.py @@ -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: diff --git a/pandas/tests/indexes/multi/test_missing.py b/pandas/tests/indexes/multi/test_missing.py index 15bbd2ce97c3c..31de40512c474 100644 --- a/pandas/tests/indexes/multi/test_missing.py +++ b/pandas/tests/indexes/multi/test_missing.py @@ -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 @@ -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 diff --git a/pandas/tests/indexes/period/test_partial_slicing.py b/pandas/tests/indexes/period/test_partial_slicing.py index 50a12baf352d9..501c2a4d8edcc 100644 --- a/pandas/tests/indexes/period/test_partial_slicing.py +++ b/pandas/tests/indexes/period/test_partial_slicing.py @@ -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) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 1f99ba7ad01db..77d81a4a9566e 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -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) @@ -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 @@ -2446,8 +2446,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 diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index bb8339439d339..63f1ef7595f31 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -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 @@ -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 @@ -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 diff --git a/pandas/tests/series/test_apply.py b/pandas/tests/series/test_apply.py index bdbfa333ef33a..eb4f3273f8713 100644 --- a/pandas/tests/series/test_apply.py +++ b/pandas/tests/series/test_apply.py @@ -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) @@ -102,7 +102,7 @@ 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) @@ -110,7 +110,7 @@ def test_apply_box(self): 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) @@ -118,7 +118,7 @@ def test_apply_box(self): 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) @@ -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) @@ -624,7 +624,7 @@ 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) @@ -632,7 +632,7 @@ def test_map_box(self): 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) @@ -640,7 +640,7 @@ def test_map_box(self): 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) diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py index f24bb9e72aef5..e65388be2ba7d 100644 --- a/pandas/tests/test_base.py +++ b/pandas/tests/test_base.py @@ -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): diff --git a/pandas/tests/tseries/holiday/test_holiday.py b/pandas/tests/tseries/holiday/test_holiday.py index 06869fcd7a4f8..7748b965f8962 100644 --- a/pandas/tests/tseries/holiday/test_holiday.py +++ b/pandas/tests/tseries/holiday/test_holiday.py @@ -238,7 +238,7 @@ class TestCalendar(AbstractHolidayCalendar): rules = [] calendar = get_calendar("TestCalendar") - assert TestCalendar == calendar.__class__ + assert TestCalendar == type(calendar) def test_factory(): diff --git a/pandas/tests/tseries/offsets/test_offsets.py b/pandas/tests/tseries/offsets/test_offsets.py index d70780741aa88..ae78d5a55bb5e 100644 --- a/pandas/tests/tseries/offsets/test_offsets.py +++ b/pandas/tests/tseries/offsets/test_offsets.py @@ -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 ): @@ -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 ): diff --git a/pandas/tseries/holiday.py b/pandas/tseries/holiday.py index 9417dc4b48499..2e5477ea00e39 100644 --- a/pandas/tseries/holiday.py +++ b/pandas/tseries/holiday.py @@ -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: