From 45c8090cee5b7839153a7101d4c463d8b993147b Mon Sep 17 00:00:00 2001 From: Vyom Pathak Date: Fri, 5 Mar 2021 05:26:25 +0530 Subject: [PATCH] STYLE: Inconsistent namespace - arithmetic (#39992) (#40224) --- pandas/tests/arithmetic/test_datetime64.py | 130 ++++++++--------- pandas/tests/arithmetic/test_period.py | 98 ++++++------- pandas/tests/arithmetic/test_timedelta64.py | 148 ++++++++++---------- 3 files changed, 187 insertions(+), 189 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index f28407df24508..f75b3800f623f 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -151,7 +151,7 @@ def test_dt64arr_nat_comparison(self, tz_naive_fixture, box_with_array): xbox = box if box not in [pd.Index, pd.array] else np.ndarray ts = Timestamp.now(tz) - ser = Series([ts, pd.NaT]) + ser = Series([ts, NaT]) obj = tm.box_expected(ser, box) @@ -222,7 +222,7 @@ def test_comparison_invalid(self, tz_naive_fixture, box_with_array): # invalid date/int comparisons tz = tz_naive_fixture ser = Series(range(5)) - ser2 = Series(pd.date_range("20010101", periods=5, tz=tz)) + ser2 = Series(date_range("20010101", periods=5, tz=tz)) ser = tm.box_expected(ser, box_with_array) ser2 = tm.box_expected(ser2, box_with_array) @@ -293,7 +293,7 @@ def test_series_comparison_scalars(self, val): def test_timestamp_compare_series(self, left, right): # see gh-4982 # Make sure we can compare Timestamps on the right AND left hand side. - ser = Series(pd.date_range("20010101", periods=10), name="dates") + ser = Series(date_range("20010101", periods=10), name="dates") s_nat = ser.copy(deep=True) ser[0] = Timestamp("nat") @@ -390,7 +390,7 @@ def test_comparators(self, op): ) def test_dti_cmp_datetimelike(self, other, tz_naive_fixture): tz = tz_naive_fixture - dti = pd.date_range("2016-01-01", periods=2, tz=tz) + dti = date_range("2016-01-01", periods=2, tz=tz) if tz is not None: if isinstance(other, np.datetime64): # no tzaware version available @@ -428,8 +428,8 @@ def test_dti_cmp_nat(self, dtype, box_with_array): box_with_array if box_with_array not in [pd.Index, pd.array] else np.ndarray ) - left = DatetimeIndex([Timestamp("2011-01-01"), pd.NaT, Timestamp("2011-01-03")]) - right = DatetimeIndex([pd.NaT, pd.NaT, Timestamp("2011-01-03")]) + left = DatetimeIndex([Timestamp("2011-01-01"), NaT, Timestamp("2011-01-03")]) + right = DatetimeIndex([NaT, NaT, Timestamp("2011-01-03")]) left = tm.box_expected(left, box_with_array) right = tm.box_expected(right, box_with_array) @@ -450,28 +450,28 @@ def test_dti_cmp_nat(self, dtype, box_with_array): expected = np.array([False, False, False]) expected = tm.box_expected(expected, xbox) - tm.assert_equal(lhs == pd.NaT, expected) - tm.assert_equal(pd.NaT == rhs, expected) + tm.assert_equal(lhs == NaT, expected) + tm.assert_equal(NaT == rhs, expected) expected = np.array([True, True, True]) expected = tm.box_expected(expected, xbox) - tm.assert_equal(lhs != pd.NaT, expected) - tm.assert_equal(pd.NaT != lhs, expected) + tm.assert_equal(lhs != NaT, expected) + tm.assert_equal(NaT != lhs, expected) expected = np.array([False, False, False]) expected = tm.box_expected(expected, xbox) - tm.assert_equal(lhs < pd.NaT, expected) - tm.assert_equal(pd.NaT > lhs, expected) + tm.assert_equal(lhs < NaT, expected) + tm.assert_equal(NaT > lhs, expected) def test_dti_cmp_nat_behaves_like_float_cmp_nan(self): fidx1 = pd.Index([1.0, np.nan, 3.0, np.nan, 5.0, 7.0]) fidx2 = pd.Index([2.0, 3.0, np.nan, np.nan, 6.0, 7.0]) didx1 = DatetimeIndex( - ["2014-01-01", pd.NaT, "2014-03-01", pd.NaT, "2014-05-01", "2014-07-01"] + ["2014-01-01", NaT, "2014-03-01", NaT, "2014-05-01", "2014-07-01"] ) didx2 = DatetimeIndex( - ["2014-02-01", "2014-03-01", pd.NaT, pd.NaT, "2014-06-01", "2014-07-01"] + ["2014-02-01", "2014-03-01", NaT, NaT, "2014-06-01", "2014-07-01"] ) darr = np.array( [ @@ -515,7 +515,7 @@ def test_dti_cmp_nat_behaves_like_float_cmp_nan(self): tm.assert_numpy_array_equal(result, expected) with tm.assert_produces_warning(None): - for idx1, val in [(fidx1, np.nan), (didx1, pd.NaT)]: + for idx1, val in [(fidx1, np.nan), (didx1, NaT)]: result = idx1 < val expected = np.array([False, False, False, False, False, False]) tm.assert_numpy_array_equal(result, expected) @@ -567,7 +567,7 @@ def test_comparison_tzawareness_compat(self, op, box_with_array): # GH#18162 box = box_with_array - dr = pd.date_range("2016-01-01", periods=6) + dr = date_range("2016-01-01", periods=6) dz = dr.tz_localize("US/Pacific") dr = tm.box_expected(dr, box) @@ -617,7 +617,7 @@ def test_comparison_tzawareness_compat(self, op, box_with_array): ) def test_comparison_tzawareness_compat_scalars(self, op, box_with_array): # GH#18162 - dr = pd.date_range("2016-01-01", periods=6) + dr = date_range("2016-01-01", periods=6) dz = dr.tz_localize("US/Pacific") dr = tm.box_expected(dr, box_with_array) @@ -661,7 +661,7 @@ def test_scalar_comparison_tzawareness( ): box = box_with_array tz = tz_aware_fixture - dti = pd.date_range("2016-01-01", periods=2, tz=tz) + dti = date_range("2016-01-01", periods=2, tz=tz) xbox = box if box not in [pd.Index, pd.array] else np.ndarray dtarr = tm.box_expected(dti, box_with_array) @@ -693,13 +693,13 @@ def test_nat_comparison_tzawareness(self, op): # GH#19276 # tzaware DatetimeIndex should not raise when compared to NaT dti = DatetimeIndex( - ["2014-01-01", pd.NaT, "2014-03-01", pd.NaT, "2014-05-01", "2014-07-01"] + ["2014-01-01", NaT, "2014-03-01", NaT, "2014-05-01", "2014-07-01"] ) expected = np.array([op == operator.ne] * len(dti)) - result = op(dti, pd.NaT) + result = op(dti, NaT) tm.assert_numpy_array_equal(result, expected) - result = op(dti.tz_localize("US/Pacific"), pd.NaT) + result = op(dti.tz_localize("US/Pacific"), NaT) tm.assert_numpy_array_equal(result, expected) def test_dti_cmp_str(self, tz_naive_fixture): @@ -813,8 +813,8 @@ def test_dt64arr_add_timedeltalike_scalar( # GH#22005, GH#22163 check DataFrame doesn't raise TypeError tz = tz_naive_fixture - rng = pd.date_range("2000-01-01", "2000-02-01", tz=tz) - expected = pd.date_range("2000-01-01 02:00", "2000-02-01 02:00", tz=tz) + rng = date_range("2000-01-01", "2000-02-01", tz=tz) + expected = date_range("2000-01-01 02:00", "2000-02-01 02:00", tz=tz) rng = tm.box_expected(rng, box_with_array) expected = tm.box_expected(expected, box_with_array) @@ -827,8 +827,8 @@ def test_dt64arr_iadd_timedeltalike_scalar( ): tz = tz_naive_fixture - rng = pd.date_range("2000-01-01", "2000-02-01", tz=tz) - expected = pd.date_range("2000-01-01 02:00", "2000-02-01 02:00", tz=tz) + rng = date_range("2000-01-01", "2000-02-01", tz=tz) + expected = date_range("2000-01-01 02:00", "2000-02-01 02:00", tz=tz) rng = tm.box_expected(rng, box_with_array) expected = tm.box_expected(expected, box_with_array) @@ -841,8 +841,8 @@ def test_dt64arr_sub_timedeltalike_scalar( ): tz = tz_naive_fixture - rng = pd.date_range("2000-01-01", "2000-02-01", tz=tz) - expected = pd.date_range("1999-12-31 22:00", "2000-01-31 22:00", tz=tz) + rng = date_range("2000-01-01", "2000-02-01", tz=tz) + expected = date_range("1999-12-31 22:00", "2000-01-31 22:00", tz=tz) rng = tm.box_expected(rng, box_with_array) expected = tm.box_expected(expected, box_with_array) @@ -855,8 +855,8 @@ def test_dt64arr_isub_timedeltalike_scalar( ): tz = tz_naive_fixture - rng = pd.date_range("2000-01-01", "2000-02-01", tz=tz) - expected = pd.date_range("1999-12-31 22:00", "2000-01-31 22:00", tz=tz) + rng = date_range("2000-01-01", "2000-02-01", tz=tz) + expected = date_range("1999-12-31 22:00", "2000-01-31 22:00", tz=tz) rng = tm.box_expected(rng, box_with_array) expected = tm.box_expected(expected, box_with_array) @@ -896,7 +896,7 @@ def test_dt64arr_add_sub_td64_nat(self, box_with_array, tz_naive_fixture): # GH#23320 special handling for timedelta64("NaT") tz = tz_naive_fixture - dti = pd.date_range("1994-04-01", periods=9, tz=tz, freq="QS") + dti = date_range("1994-04-01", periods=9, tz=tz, freq="QS") other = np.timedelta64("NaT") expected = DatetimeIndex(["NaT"] * 9, tz=tz) @@ -916,11 +916,11 @@ def test_dt64arr_add_sub_td64_nat(self, box_with_array, tz_naive_fixture): def test_dt64arr_add_sub_td64ndarray(self, tz_naive_fixture, box_with_array): tz = tz_naive_fixture - dti = pd.date_range("2016-01-01", periods=3, tz=tz) + dti = date_range("2016-01-01", periods=3, tz=tz) tdi = TimedeltaIndex(["-1 Day", "-1 Day", "-1 Day"]) tdarr = tdi.values - expected = pd.date_range("2015-12-31", "2016-01-02", periods=3, tz=tz) + expected = date_range("2015-12-31", "2016-01-02", periods=3, tz=tz) dtarr = tm.box_expected(dti, box_with_array) expected = tm.box_expected(expected, box_with_array) @@ -930,7 +930,7 @@ def test_dt64arr_add_sub_td64ndarray(self, tz_naive_fixture, box_with_array): result = tdarr + dtarr tm.assert_equal(result, expected) - expected = pd.date_range("2016-01-02", "2016-01-04", periods=3, tz=tz) + expected = date_range("2016-01-02", "2016-01-04", periods=3, tz=tz) expected = tm.box_expected(expected, box_with_array) result = dtarr - tdarr @@ -952,7 +952,7 @@ def test_dt64arr_add_sub_td64ndarray(self, tz_naive_fixture, box_with_array): ) def test_dt64arr_sub_dtscalar(self, box_with_array, ts): # GH#8554, GH#22163 DataFrame op should _not_ return dt64 dtype - idx = pd.date_range("2013-01-01", periods=3)._with_freq(None) + idx = date_range("2013-01-01", periods=3)._with_freq(None) idx = tm.box_expected(idx, box_with_array) expected = TimedeltaIndex(["0 Days", "1 Day", "2 Days"]) @@ -967,7 +967,7 @@ def test_dt64arr_sub_datetime64_not_ns(self, box_with_array): dt64 = np.datetime64("2013-01-01") assert dt64.dtype == "datetime64[D]" - dti = pd.date_range("20130101", periods=3)._with_freq(None) + dti = date_range("20130101", periods=3)._with_freq(None) dtarr = tm.box_expected(dti, box_with_array) expected = TimedeltaIndex(["0 Days", "1 Day", "2 Days"]) @@ -980,7 +980,7 @@ def test_dt64arr_sub_datetime64_not_ns(self, box_with_array): tm.assert_equal(result, -expected) def test_dt64arr_sub_timestamp(self, box_with_array): - ser = pd.date_range("2014-03-17", periods=2, freq="D", tz="US/Eastern") + ser = date_range("2014-03-17", periods=2, freq="D", tz="US/Eastern") ser = ser._with_freq(None) ts = ser[0] @@ -994,19 +994,19 @@ def test_dt64arr_sub_timestamp(self, box_with_array): def test_dt64arr_sub_NaT(self, box_with_array): # GH#18808 - dti = DatetimeIndex([pd.NaT, Timestamp("19900315")]) + dti = DatetimeIndex([NaT, Timestamp("19900315")]) ser = tm.box_expected(dti, box_with_array) - result = ser - pd.NaT - expected = Series([pd.NaT, pd.NaT], dtype="timedelta64[ns]") + result = ser - NaT + expected = Series([NaT, NaT], dtype="timedelta64[ns]") expected = tm.box_expected(expected, box_with_array) tm.assert_equal(result, expected) dti_tz = dti.tz_localize("Asia/Tokyo") ser_tz = tm.box_expected(dti_tz, box_with_array) - result = ser_tz - pd.NaT - expected = Series([pd.NaT, pd.NaT], dtype="timedelta64[ns]") + result = ser_tz - NaT + expected = Series([NaT, NaT], dtype="timedelta64[ns]") expected = tm.box_expected(expected, box_with_array) tm.assert_equal(result, expected) @@ -1014,7 +1014,7 @@ def test_dt64arr_sub_NaT(self, box_with_array): # Subtraction of datetime-like array-like def test_dt64arr_sub_dt64object_array(self, box_with_array, tz_naive_fixture): - dti = pd.date_range("2016-01-01", periods=3, tz=tz_naive_fixture) + dti = date_range("2016-01-01", periods=3, tz=tz_naive_fixture) expected = dti - dti obj = tm.box_expected(dti, box_with_array) @@ -1025,7 +1025,7 @@ def test_dt64arr_sub_dt64object_array(self, box_with_array, tz_naive_fixture): tm.assert_equal(result, expected) def test_dt64arr_naive_sub_dt64ndarray(self, box_with_array): - dti = pd.date_range("2016-01-01", periods=3, tz=None) + dti = date_range("2016-01-01", periods=3, tz=None) dt64vals = dti.values dtarr = tm.box_expected(dti, box_with_array) @@ -1041,7 +1041,7 @@ def test_dt64arr_aware_sub_dt64ndarray_raises( ): tz = tz_aware_fixture - dti = pd.date_range("2016-01-01", periods=3, tz=tz) + dti = date_range("2016-01-01", periods=3, tz=tz) dt64vals = dti.values dtarr = tm.box_expected(dti, box_with_array) @@ -1057,7 +1057,7 @@ def test_dt64arr_aware_sub_dt64ndarray_raises( def test_dt64arr_add_dt64ndarray_raises(self, tz_naive_fixture, box_with_array): tz = tz_naive_fixture - dti = pd.date_range("2016-01-01", periods=3, tz=tz) + dti = date_range("2016-01-01", periods=3, tz=tz) dt64vals = dti.values dtarr = tm.box_expected(dti, box_with_array) @@ -1132,7 +1132,7 @@ def test_dt64arr_addsub_time_objects_raises(self, box_with_array, tz_naive_fixtu tz = tz_naive_fixture - obj1 = pd.date_range("2012-01-01", periods=3, tz=tz) + obj1 = date_range("2012-01-01", periods=3, tz=tz) obj2 = [time(i, i, i) for i in range(3)] obj1 = tm.box_expected(obj1, box_with_array) @@ -1512,7 +1512,7 @@ def test_dt64arr_add_sub_offset_array( # GH#10699 array of offsets tz = tz_naive_fixture - dti = pd.date_range("2017-01-01", periods=2, tz=tz) + dti = date_range("2017-01-01", periods=2, tz=tz) dtarr = tm.box_expected(dti, box_with_array) other = np.array([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)]) @@ -1612,7 +1612,7 @@ def test_dt64_series_arith_overflow(self): # GH#12534, fixed by GH#19024 dt = Timestamp("1700-01-31") td = Timedelta("20000 Days") - dti = pd.date_range("1949-09-30", freq="100Y", periods=4) + dti = date_range("1949-09-30", freq="100Y", periods=4) ser = Series(dti) msg = "Overflow in int64 addition" with pytest.raises(OverflowError, match=msg): @@ -1624,7 +1624,7 @@ def test_dt64_series_arith_overflow(self): with pytest.raises(OverflowError, match=msg): td + ser - ser.iloc[-1] = pd.NaT + ser.iloc[-1] = NaT expected = Series( ["2004-10-03", "2104-10-04", "2204-10-04", "NaT"], dtype="datetime64[ns]" ) @@ -1633,7 +1633,7 @@ def test_dt64_series_arith_overflow(self): res = td + ser tm.assert_series_equal(res, expected) - ser.iloc[1:] = pd.NaT + ser.iloc[1:] = NaT expected = Series(["91279 Days", "NaT", "NaT", "NaT"], dtype="timedelta64[ns]") res = ser - dt tm.assert_series_equal(res, expected) @@ -1834,7 +1834,7 @@ def test_sub_single_tz(self): def test_dt64tz_series_sub_dtitz(self): # GH#19071 subtracting tzaware DatetimeIndex from tzaware Series # (with same tz) raises, fixed by #19024 - dti = pd.date_range("1999-09-30", periods=10, tz="US/Pacific") + dti = date_range("1999-09-30", periods=10, tz="US/Pacific") ser = Series(dti) expected = Series(TimedeltaIndex(["0days"] * 10)) @@ -1845,9 +1845,9 @@ def test_dt64tz_series_sub_dtitz(self): def test_sub_datetime_compat(self): # see GH#14088 - s = Series([datetime(2016, 8, 23, 12, tzinfo=pytz.utc), pd.NaT]) + s = Series([datetime(2016, 8, 23, 12, tzinfo=pytz.utc), NaT]) dt = datetime(2016, 8, 22, 12, tzinfo=pytz.utc) - exp = Series([Timedelta("1 days"), pd.NaT]) + exp = Series([Timedelta("1 days"), NaT]) tm.assert_series_equal(s - dt, exp) tm.assert_series_equal(s - Timestamp(dt), exp) @@ -1911,8 +1911,8 @@ def test_datetime64_ops_nat(self): "dt64_series", [ Series([Timestamp("19900315"), Timestamp("19900315")]), - Series([pd.NaT, Timestamp("19900315")]), - Series([pd.NaT, pd.NaT], dtype="datetime64[ns]"), + Series([NaT, Timestamp("19900315")]), + Series([NaT, NaT], dtype="datetime64[ns]"), ], ) @pytest.mark.parametrize("one", [1, 1.0, np.array(1)]) @@ -2029,7 +2029,7 @@ class TestDatetimeIndexArithmetic: def test_dti_addsub_int(self, tz_naive_fixture, one): # Variants of `one` for #19012 tz = tz_naive_fixture - rng = pd.date_range("2000-01-01 09:00", freq="H", periods=10, tz=tz) + rng = date_range("2000-01-01 09:00", freq="H", periods=10, tz=tz) msg = "Addition/subtraction of integers" with pytest.raises(TypeError, match=msg): @@ -2048,7 +2048,7 @@ def test_dti_addsub_int(self, tz_naive_fixture, one): @pytest.mark.parametrize("int_holder", [np.array, pd.Index]) def test_dti_add_intarray_tick(self, int_holder, freq): # GH#19959 - dti = pd.date_range("2016-01-01", periods=2, freq=freq) + dti = date_range("2016-01-01", periods=2, freq=freq) other = int_holder([4, -1]) msg = "Addition/subtraction of integers|cannot subtract DatetimeArray from" @@ -2058,7 +2058,7 @@ def test_dti_add_intarray_tick(self, int_holder, freq): @pytest.mark.parametrize("int_holder", [np.array, pd.Index]) def test_dti_add_intarray_non_tick(self, int_holder, freq): # GH#19959 - dti = pd.date_range("2016-01-01", periods=2, freq=freq) + dti = date_range("2016-01-01", periods=2, freq=freq) other = int_holder([4, -1]) msg = "Addition/subtraction of integers|cannot subtract DatetimeArray from" @@ -2082,7 +2082,7 @@ def test_dti_add_tdi(self, tz_naive_fixture): tz = tz_naive_fixture dti = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10) tdi = pd.timedelta_range("0 days", periods=10) - expected = pd.date_range("2017-01-01", periods=10, tz=tz) + expected = date_range("2017-01-01", periods=10, tz=tz) expected = expected._with_freq(None) # add with TimdeltaIndex @@ -2104,7 +2104,7 @@ def test_dti_iadd_tdi(self, tz_naive_fixture): tz = tz_naive_fixture dti = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10) tdi = pd.timedelta_range("0 days", periods=10) - expected = pd.date_range("2017-01-01", periods=10, tz=tz) + expected = date_range("2017-01-01", periods=10, tz=tz) expected = expected._with_freq(None) # iadd with TimdeltaIndex @@ -2130,7 +2130,7 @@ def test_dti_sub_tdi(self, tz_naive_fixture): tz = tz_naive_fixture dti = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10) tdi = pd.timedelta_range("0 days", periods=10) - expected = pd.date_range("2017-01-01", periods=10, tz=tz, freq="-1D") + expected = date_range("2017-01-01", periods=10, tz=tz, freq="-1D") expected = expected._with_freq(None) # sub with TimedeltaIndex @@ -2154,7 +2154,7 @@ def test_dti_isub_tdi(self, tz_naive_fixture): tz = tz_naive_fixture dti = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10) tdi = pd.timedelta_range("0 days", periods=10) - expected = pd.date_range("2017-01-01", periods=10, tz=tz, freq="-1D") + expected = date_range("2017-01-01", periods=10, tz=tz, freq="-1D") expected = expected._with_freq(None) # isub with TimedeltaIndex @@ -2435,7 +2435,7 @@ def test_dti_addsub_offset_arraylike( other_box = index_or_series tz = tz_naive_fixture - dti = pd.date_range("2017-01-01", periods=2, tz=tz, name=names[0]) + dti = date_range("2017-01-01", periods=2, tz=tz, name=names[0]) other = other_box([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)], name=names[1]) xbox = get_upcast_box(box, other) @@ -2455,7 +2455,7 @@ def test_dti_addsub_object_arraylike( ): tz = tz_naive_fixture - dti = pd.date_range("2017-01-01", periods=2, tz=tz) + dti = date_range("2017-01-01", periods=2, tz=tz) dtarr = tm.box_expected(dti, box_with_array) other = other_box([pd.offsets.MonthEnd(), Timedelta(days=4)]) xbox = get_upcast_box(box_with_array, other) @@ -2497,7 +2497,7 @@ def test_shift_months(years, months): def test_dt64arr_addsub_object_dtype_2d(): # block-wise DataFrame operations will require operating on 2D # DatetimeArray/TimedeltaArray, so check that specifically. - dti = pd.date_range("1994-02-13", freq="2W", periods=4) + dti = date_range("1994-02-13", freq="2W", periods=4) dta = dti._data.reshape((4, 1)) other = np.array([[pd.offsets.Day(n)] for n in range(4)]) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index f0c03fee50b39..bb78e29924ba2 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -42,7 +42,7 @@ def test_compare_zerodim(self, box_with_array): box_with_array if box_with_array not in [pd.Index, pd.array] else np.ndarray ) - pi = pd.period_range("2000", periods=4) + pi = period_range("2000", periods=4) other = np.array(pi.to_numpy()[0]) pi = tm.box_expected(pi, box_with_array) @@ -56,7 +56,7 @@ def test_compare_zerodim(self, box_with_array): ) def test_compare_invalid_scalar(self, box_with_array, scalar): # comparison with scalar that cannot be interpreted as a Period - pi = pd.period_range("2000", periods=4) + pi = period_range("2000", periods=4) parr = tm.box_expected(pi, box_with_array) assert_invalid_comparison(parr, scalar, box_with_array) @@ -71,13 +71,13 @@ def test_compare_invalid_scalar(self, box_with_array, scalar): ], ) def test_compare_invalid_listlike(self, box_with_array, other): - pi = pd.period_range("2000", periods=4) + pi = period_range("2000", periods=4) parr = tm.box_expected(pi, box_with_array) assert_invalid_comparison(parr, other, box_with_array) @pytest.mark.parametrize("other_box", [list, np.array, lambda x: x.astype(object)]) def test_compare_object_dtype(self, box_with_array, other_box): - pi = pd.period_range("2000", periods=5) + pi = period_range("2000", periods=5) parr = tm.box_expected(pi, box_with_array) xbox = np.ndarray if box_with_array in [pd.Index, pd.array] else box_with_array @@ -191,7 +191,7 @@ def test_parr_cmp_period_scalar2(self, box_with_array): box_with_array if box_with_array not in [pd.Index, pd.array] else np.ndarray ) - pi = pd.period_range("2000-01-01", periods=10, freq="D") + pi = period_range("2000-01-01", periods=10, freq="D") val = Period("2000-01-04", freq="D") expected = [x > val for x in pi] @@ -594,8 +594,8 @@ class TestPeriodIndexArithmetic: # and PeriodIndex (with matching freq) def test_parr_add_iadd_parr_raises(self, box_with_array): - rng = pd.period_range("1/1/2000", freq="D", periods=5) - other = pd.period_range("1/6/2000", freq="D", periods=5) + rng = period_range("1/1/2000", freq="D", periods=5) + other = period_range("1/6/2000", freq="D", periods=5) # TODO: parametrize over boxes for other? rng = tm.box_expected(rng, box_with_array) @@ -615,8 +615,8 @@ def test_pi_sub_isub_pi(self): # For historical reference see GH#14164, GH#13077. # PeriodIndex subtraction originally performed set difference, # then changed to raise TypeError before being implemented in GH#20049 - rng = pd.period_range("1/1/2000", freq="D", periods=5) - other = pd.period_range("1/6/2000", freq="D", periods=5) + rng = period_range("1/1/2000", freq="D", periods=5) + other = period_range("1/6/2000", freq="D", periods=5) off = rng.freq expected = pd.Index([-5 * off] * 5) @@ -627,7 +627,7 @@ def test_pi_sub_isub_pi(self): tm.assert_index_equal(rng, expected) def test_pi_sub_pi_with_nat(self): - rng = pd.period_range("1/1/2000", freq="D", periods=5) + rng = period_range("1/1/2000", freq="D", periods=5) other = rng[1:].insert(0, pd.NaT) assert other[1:].equals(rng[1:]) @@ -637,8 +637,8 @@ def test_pi_sub_pi_with_nat(self): tm.assert_index_equal(result, expected) def test_parr_sub_pi_mismatched_freq(self, box_with_array): - rng = pd.period_range("1/1/2000", freq="D", periods=5) - other = pd.period_range("1/6/2000", freq="H", periods=5) + rng = period_range("1/1/2000", freq="D", periods=5) + other = period_range("1/6/2000", freq="H", periods=5) # TODO: parametrize over boxes for other? rng = tm.box_expected(rng, box_with_array) @@ -720,7 +720,7 @@ def test_parr_add_sub_float_raises(self, op, other, box_with_array): ) def test_parr_add_sub_invalid(self, other, box_with_array): # GH#23215 - rng = pd.period_range("1/1/2000", freq="D", periods=3) + rng = period_range("1/1/2000", freq="D", periods=3) rng = tm.box_expected(rng, box_with_array) msg = ( @@ -741,7 +741,7 @@ def test_parr_add_sub_invalid(self, other, box_with_array): # __add__/__sub__ with ndarray[datetime64] and ndarray[timedelta64] def test_pi_add_sub_td64_array_non_tick_raises(self): - rng = pd.period_range("1/1/2000", freq="Q", periods=3) + rng = period_range("1/1/2000", freq="Q", periods=3) tdi = TimedeltaIndex(["-1 Day", "-1 Day", "-1 Day"]) tdarr = tdi.values @@ -760,11 +760,11 @@ def test_pi_add_sub_td64_array_non_tick_raises(self): def test_pi_add_sub_td64_array_tick(self): # PeriodIndex + Timedelta-like is allowed only with # tick-like frequencies - rng = pd.period_range("1/1/2000", freq="90D", periods=3) + rng = period_range("1/1/2000", freq="90D", periods=3) tdi = TimedeltaIndex(["-1 Day", "-1 Day", "-1 Day"]) tdarr = tdi.values - expected = pd.period_range("12/31/1999", freq="90D", periods=3) + expected = period_range("12/31/1999", freq="90D", periods=3) result = rng + tdi tm.assert_index_equal(result, expected) result = rng + tdarr @@ -774,7 +774,7 @@ def test_pi_add_sub_td64_array_tick(self): result = tdarr + rng tm.assert_index_equal(result, expected) - expected = pd.period_range("1/2/2000", freq="90D", periods=3) + expected = period_range("1/2/2000", freq="90D", periods=3) result = rng - tdi tm.assert_index_equal(result, expected) @@ -895,9 +895,9 @@ def test_pi_sub_offset_array(self, box): def test_pi_add_iadd_int(self, one): # Variants of `one` for #19012 - rng = pd.period_range("2000-01-01 09:00", freq="H", periods=10) + rng = period_range("2000-01-01 09:00", freq="H", periods=10) result = rng + one - expected = pd.period_range("2000-01-01 10:00", freq="H", periods=10) + expected = period_range("2000-01-01 10:00", freq="H", periods=10) tm.assert_index_equal(result, expected) rng += one tm.assert_index_equal(rng, expected) @@ -907,9 +907,9 @@ def test_pi_sub_isub_int(self, one): PeriodIndex.__sub__ and __isub__ with several representations of the integer 1, e.g. int, np.int64, np.uint8, ... """ - rng = pd.period_range("2000-01-01 09:00", freq="H", periods=10) + rng = period_range("2000-01-01 09:00", freq="H", periods=10) result = rng - one - expected = pd.period_range("2000-01-01 08:00", freq="H", periods=10) + expected = period_range("2000-01-01 08:00", freq="H", periods=10) tm.assert_index_equal(result, expected) rng -= one tm.assert_index_equal(rng, expected) @@ -925,16 +925,16 @@ def test_pi_sub_intlike(self, five): def test_pi_sub_isub_offset(self): # offset # DateOffset - rng = pd.period_range("2014", "2024", freq="A") + rng = period_range("2014", "2024", freq="A") result = rng - pd.offsets.YearEnd(5) - expected = pd.period_range("2009", "2019", freq="A") + expected = period_range("2009", "2019", freq="A") tm.assert_index_equal(result, expected) rng -= pd.offsets.YearEnd(5) tm.assert_index_equal(rng, expected) - rng = pd.period_range("2014-01", "2016-12", freq="M") + rng = period_range("2014-01", "2016-12", freq="M") result = rng - pd.offsets.MonthEnd(5) - expected = pd.period_range("2013-08", "2016-07", freq="M") + expected = period_range("2013-08", "2016-07", freq="M") tm.assert_index_equal(result, expected) rng -= pd.offsets.MonthEnd(5) @@ -1012,7 +1012,7 @@ def test_pi_add_timedeltalike_minute_gt1(self, three_days): # in test_pi_add_timedeltalike_tick_gt1, but here we write out the # expected result more explicitly. other = three_days - rng = pd.period_range("2014-05-01", periods=3, freq="2D") + rng = period_range("2014-05-01", periods=3, freq="2D") expected = PeriodIndex(["2014-05-04", "2014-05-06", "2014-05-08"], freq="2D") @@ -1039,9 +1039,9 @@ def test_pi_add_timedeltalike_tick_gt1(self, three_days, freqstr): # GH#23031 adding a time-delta-like offset to a PeriodArray that has # tick-like frequency with n != 1 other = three_days - rng = pd.period_range("2014-05-01", periods=6, freq=freqstr) + rng = period_range("2014-05-01", periods=6, freq=freqstr) - expected = pd.period_range(rng[0] + other, periods=6, freq=freqstr) + expected = period_range(rng[0] + other, periods=6, freq=freqstr) result = rng + other tm.assert_index_equal(result, expected) @@ -1050,7 +1050,7 @@ def test_pi_add_timedeltalike_tick_gt1(self, three_days, freqstr): tm.assert_index_equal(result, expected) # subtraction - expected = pd.period_range(rng[0] - other, periods=6, freq=freqstr) + expected = period_range(rng[0] - other, periods=6, freq=freqstr) result = rng - other tm.assert_index_equal(result, expected) msg = ( @@ -1063,8 +1063,8 @@ def test_pi_add_timedeltalike_tick_gt1(self, three_days, freqstr): def test_pi_add_iadd_timedeltalike_daily(self, three_days): # Tick other = three_days - rng = pd.period_range("2014-05-01", "2014-05-15", freq="D") - expected = pd.period_range("2014-05-04", "2014-05-18", freq="D") + rng = period_range("2014-05-01", "2014-05-15", freq="D") + expected = period_range("2014-05-04", "2014-05-18", freq="D") result = rng + other tm.assert_index_equal(result, expected) @@ -1075,8 +1075,8 @@ def test_pi_add_iadd_timedeltalike_daily(self, three_days): def test_pi_sub_isub_timedeltalike_daily(self, three_days): # Tick-like 3 Days other = three_days - rng = pd.period_range("2014-05-01", "2014-05-15", freq="D") - expected = pd.period_range("2014-04-28", "2014-05-12", freq="D") + rng = period_range("2014-05-01", "2014-05-15", freq="D") + expected = period_range("2014-04-28", "2014-05-12", freq="D") result = rng - other tm.assert_index_equal(result, expected) @@ -1086,7 +1086,7 @@ def test_pi_sub_isub_timedeltalike_daily(self, three_days): def test_pi_add_sub_timedeltalike_freq_mismatch_daily(self, not_daily): other = not_daily - rng = pd.period_range("2014-05-01", "2014-05-15", freq="D") + rng = period_range("2014-05-01", "2014-05-15", freq="D") msg = "Input has different freq(=.+)? from Period.*?\\(freq=D\\)" with pytest.raises(IncompatibleFrequency, match=msg): rng + other @@ -1099,8 +1099,8 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_daily(self, not_daily): def test_pi_add_iadd_timedeltalike_hourly(self, two_hours): other = two_hours - rng = pd.period_range("2014-01-01 10:00", "2014-01-05 10:00", freq="H") - expected = pd.period_range("2014-01-01 12:00", "2014-01-05 12:00", freq="H") + rng = period_range("2014-01-01 10:00", "2014-01-05 10:00", freq="H") + expected = period_range("2014-01-01 12:00", "2014-01-05 12:00", freq="H") result = rng + other tm.assert_index_equal(result, expected) @@ -1110,7 +1110,7 @@ def test_pi_add_iadd_timedeltalike_hourly(self, two_hours): def test_pi_add_timedeltalike_mismatched_freq_hourly(self, not_hourly): other = not_hourly - rng = pd.period_range("2014-01-01 10:00", "2014-01-05 10:00", freq="H") + rng = period_range("2014-01-01 10:00", "2014-01-05 10:00", freq="H") msg = "Input has different freq(=.+)? from Period.*?\\(freq=H\\)" with pytest.raises(IncompatibleFrequency, match=msg): @@ -1121,8 +1121,8 @@ def test_pi_add_timedeltalike_mismatched_freq_hourly(self, not_hourly): def test_pi_sub_isub_timedeltalike_hourly(self, two_hours): other = two_hours - rng = pd.period_range("2014-01-01 10:00", "2014-01-05 10:00", freq="H") - expected = pd.period_range("2014-01-01 08:00", "2014-01-05 08:00", freq="H") + rng = period_range("2014-01-01 10:00", "2014-01-05 10:00", freq="H") + expected = period_range("2014-01-01 08:00", "2014-01-05 08:00", freq="H") result = rng - other tm.assert_index_equal(result, expected) @@ -1133,16 +1133,16 @@ def test_pi_sub_isub_timedeltalike_hourly(self, two_hours): def test_add_iadd_timedeltalike_annual(self): # offset # DateOffset - rng = pd.period_range("2014", "2024", freq="A") + rng = period_range("2014", "2024", freq="A") result = rng + pd.offsets.YearEnd(5) - expected = pd.period_range("2019", "2029", freq="A") + expected = period_range("2019", "2029", freq="A") tm.assert_index_equal(result, expected) rng += pd.offsets.YearEnd(5) tm.assert_index_equal(rng, expected) def test_pi_add_sub_timedeltalike_freq_mismatch_annual(self, mismatched_freq): other = mismatched_freq - rng = pd.period_range("2014", "2024", freq="A") + rng = period_range("2014", "2024", freq="A") msg = "Input has different freq(=.+)? from Period.*?\\(freq=A-DEC\\)" with pytest.raises(IncompatibleFrequency, match=msg): rng + other @@ -1154,8 +1154,8 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_annual(self, mismatched_freq): rng -= other def test_pi_add_iadd_timedeltalike_M(self): - rng = pd.period_range("2014-01", "2016-12", freq="M") - expected = pd.period_range("2014-06", "2017-05", freq="M") + rng = period_range("2014-01", "2016-12", freq="M") + expected = period_range("2014-06", "2017-05", freq="M") result = rng + pd.offsets.MonthEnd(5) tm.assert_index_equal(result, expected) @@ -1165,7 +1165,7 @@ def test_pi_add_iadd_timedeltalike_M(self): def test_pi_add_sub_timedeltalike_freq_mismatch_monthly(self, mismatched_freq): other = mismatched_freq - rng = pd.period_range("2014-01", "2016-12", freq="M") + rng = period_range("2014-01", "2016-12", freq="M") msg = "Input has different freq(=.+)? from Period.*?\\(freq=M\\)" with pytest.raises(IncompatibleFrequency, match=msg): rng + other @@ -1179,7 +1179,7 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_monthly(self, mismatched_freq): @pytest.mark.parametrize("transpose", [True, False]) def test_parr_add_sub_td64_nat(self, box_with_array, transpose): # GH#23320 special handling for timedelta64("NaT") - pi = pd.period_range("1994-04-01", periods=9, freq="19D") + pi = period_range("1994-04-01", periods=9, freq="19D") other = np.timedelta64("NaT") expected = PeriodIndex(["NaT"] * 9, freq="19D") @@ -1204,7 +1204,7 @@ def test_parr_add_sub_td64_nat(self, box_with_array, transpose): ], ) def test_parr_add_sub_tdt64_nat_array(self, box_with_array, other): - pi = pd.period_range("1994-04-01", periods=9, freq="19D") + pi = period_range("1994-04-01", periods=9, freq="19D") expected = PeriodIndex(["NaT"] * 9, freq="19D") obj = tm.box_expected(pi, box_with_array) @@ -1225,7 +1225,7 @@ def test_parr_add_sub_tdt64_nat_array(self, box_with_array, other): def test_parr_add_sub_index(self): # Check that PeriodArray defers to Index on arithmetic ops - pi = pd.period_range("2000-12-31", periods=3) + pi = period_range("2000-12-31", periods=3) parr = pi.array result = parr - pi @@ -1233,7 +1233,7 @@ def test_parr_add_sub_index(self): tm.assert_index_equal(result, expected) def test_parr_add_sub_object_array(self): - pi = pd.period_range("2000-12-31", periods=3, freq="D") + pi = period_range("2000-12-31", periods=3, freq="D") parr = pi.array other = np.array([Timedelta(days=1), pd.offsets.Day(2), 3]) diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index b08442321f502..dcfe3a40b4487 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -70,7 +70,7 @@ def test_compare_timedelta64_zerodim(self, box_with_array): box_with_array if box_with_array not in [pd.Index, pd.array] else np.ndarray ) - tdi = pd.timedelta_range("2H", periods=4) + tdi = timedelta_range("2H", periods=4) other = np.array(tdi.to_numpy()[0]) tdi = tm.box_expected(tdi, box) @@ -175,8 +175,8 @@ class TestTimedelta64ArrayComparisons: @pytest.mark.parametrize("dtype", [None, object]) def test_comp_nat(self, dtype): - left = TimedeltaIndex([Timedelta("1 days"), pd.NaT, Timedelta("3 days")]) - right = TimedeltaIndex([pd.NaT, pd.NaT, Timedelta("3 days")]) + left = TimedeltaIndex([Timedelta("1 days"), NaT, Timedelta("3 days")]) + right = TimedeltaIndex([NaT, NaT, Timedelta("3 days")]) lhs, rhs = left, right if dtype is object: @@ -191,30 +191,30 @@ def test_comp_nat(self, dtype): tm.assert_numpy_array_equal(result, expected) expected = np.array([False, False, False]) - tm.assert_numpy_array_equal(lhs == pd.NaT, expected) - tm.assert_numpy_array_equal(pd.NaT == rhs, expected) + tm.assert_numpy_array_equal(lhs == NaT, expected) + tm.assert_numpy_array_equal(NaT == rhs, expected) expected = np.array([True, True, True]) - tm.assert_numpy_array_equal(lhs != pd.NaT, expected) - tm.assert_numpy_array_equal(pd.NaT != lhs, expected) + tm.assert_numpy_array_equal(lhs != NaT, expected) + tm.assert_numpy_array_equal(NaT != lhs, expected) expected = np.array([False, False, False]) - tm.assert_numpy_array_equal(lhs < pd.NaT, expected) - tm.assert_numpy_array_equal(pd.NaT > lhs, expected) + tm.assert_numpy_array_equal(lhs < NaT, expected) + tm.assert_numpy_array_equal(NaT > lhs, expected) def test_comparisons_nat(self): tdidx1 = TimedeltaIndex( [ "1 day", - pd.NaT, + NaT, "1 day 00:00:01", - pd.NaT, + NaT, "1 day 00:00:01", "5 day 00:00:03", ] ) tdidx2 = TimedeltaIndex( - ["2 day", "2 day", pd.NaT, pd.NaT, "1 day 00:00:02", "5 days 00:00:03"] + ["2 day", "2 day", NaT, NaT, "1 day 00:00:02", "5 days 00:00:03"] ) tdarr = np.array( [ @@ -310,7 +310,7 @@ def test_ufunc_coercions(self): def test_subtraction_ops(self): # with datetimes/timedelta and tdi/dti - tdi = TimedeltaIndex(["1 days", pd.NaT, "2 days"], name="foo") + tdi = TimedeltaIndex(["1 days", NaT, "2 days"], name="foo") dti = pd.date_range("20130101", periods=3, name="bar") td = Timedelta("1 days") dt = Timestamp("20130101") @@ -338,11 +338,11 @@ def test_subtraction_ops(self): tm.assert_index_equal(result, expected) result = tdi - td - expected = TimedeltaIndex(["0 days", pd.NaT, "1 days"], name="foo") + expected = TimedeltaIndex(["0 days", NaT, "1 days"], name="foo") tm.assert_index_equal(result, expected, check_names=False) result = td - tdi - expected = TimedeltaIndex(["0 days", pd.NaT, "-1 days"], name="foo") + expected = TimedeltaIndex(["0 days", NaT, "-1 days"], name="foo") tm.assert_index_equal(result, expected, check_names=False) result = dti - td @@ -352,7 +352,7 @@ def test_subtraction_ops(self): tm.assert_index_equal(result, expected, check_names=False) result = dt - tdi - expected = DatetimeIndex(["20121231", pd.NaT, "20121230"], name="foo") + expected = DatetimeIndex(["20121231", NaT, "20121230"], name="foo") tm.assert_index_equal(result, expected) def test_subtraction_ops_with_tz(self): @@ -441,42 +441,42 @@ def _check(result, expected): def test_dti_tdi_numeric_ops(self): # These are normally union/diff set-like ops - tdi = TimedeltaIndex(["1 days", pd.NaT, "2 days"], name="foo") + tdi = TimedeltaIndex(["1 days", NaT, "2 days"], name="foo") dti = pd.date_range("20130101", periods=3, name="bar") result = tdi - tdi - expected = TimedeltaIndex(["0 days", pd.NaT, "0 days"], name="foo") + expected = TimedeltaIndex(["0 days", NaT, "0 days"], name="foo") tm.assert_index_equal(result, expected) result = tdi + tdi - expected = TimedeltaIndex(["2 days", pd.NaT, "4 days"], name="foo") + expected = TimedeltaIndex(["2 days", NaT, "4 days"], name="foo") tm.assert_index_equal(result, expected) result = dti - tdi # name will be reset - expected = DatetimeIndex(["20121231", pd.NaT, "20130101"]) + expected = DatetimeIndex(["20121231", NaT, "20130101"]) tm.assert_index_equal(result, expected) def test_addition_ops(self): # with datetimes/timedelta and tdi/dti - tdi = TimedeltaIndex(["1 days", pd.NaT, "2 days"], name="foo") + tdi = TimedeltaIndex(["1 days", NaT, "2 days"], name="foo") dti = pd.date_range("20130101", periods=3, name="bar") td = Timedelta("1 days") dt = Timestamp("20130101") result = tdi + dt - expected = DatetimeIndex(["20130102", pd.NaT, "20130103"], name="foo") + expected = DatetimeIndex(["20130102", NaT, "20130103"], name="foo") tm.assert_index_equal(result, expected) result = dt + tdi - expected = DatetimeIndex(["20130102", pd.NaT, "20130103"], name="foo") + expected = DatetimeIndex(["20130102", NaT, "20130103"], name="foo") tm.assert_index_equal(result, expected) result = td + tdi - expected = TimedeltaIndex(["2 days", pd.NaT, "3 days"], name="foo") + expected = TimedeltaIndex(["2 days", NaT, "3 days"], name="foo") tm.assert_index_equal(result, expected) result = tdi + td - expected = TimedeltaIndex(["2 days", pd.NaT, "3 days"], name="foo") + expected = TimedeltaIndex(["2 days", NaT, "3 days"], name="foo") tm.assert_index_equal(result, expected) # unequal length @@ -495,11 +495,11 @@ def test_addition_ops(self): # pytest.raises(TypeError, lambda : pd.Int64Index([1,2,3]) + tdi) result = tdi + dti # name will be reset - expected = DatetimeIndex(["20130102", pd.NaT, "20130105"]) + expected = DatetimeIndex(["20130102", NaT, "20130105"]) tm.assert_index_equal(result, expected) result = dti + tdi # name will be reset - expected = DatetimeIndex(["20130102", pd.NaT, "20130105"]) + expected = DatetimeIndex(["20130102", NaT, "20130105"]) tm.assert_index_equal(result, expected) result = dt + td @@ -538,10 +538,10 @@ def test_timedelta(self, freq): # GH#4134, buggy with timedeltas rng = pd.date_range("2013", "2014") s = Series(rng) - result1 = rng - pd.offsets.Hour(1) + result1 = rng - offsets.Hour(1) result2 = DatetimeIndex(s - np.timedelta64(100000000)) result3 = rng - np.timedelta64(100000000) - result4 = DatetimeIndex(s - pd.offsets.Hour(1)) + result4 = DatetimeIndex(s - offsets.Hour(1)) assert result1.freq == rng.freq result1 = result1._with_freq(None) @@ -553,7 +553,7 @@ def test_timedelta(self, freq): def test_tda_add_sub_index(self): # Check that TimedeltaArray defers to Index on arithmetic ops - tdi = TimedeltaIndex(["1 days", pd.NaT, "2 days"]) + tdi = TimedeltaIndex(["1 days", NaT, "2 days"]) tda = tdi.array dti = pd.date_range("1999-12-31", periods=3, freq="D") @@ -648,7 +648,7 @@ def test_tdi_ops_attributes(self): tm.assert_index_equal(result, exp) assert result.freq == "-2D" - rng = pd.timedelta_range("-2 days", periods=5, freq="D", name="x") + rng = timedelta_range("-2 days", periods=5, freq="D", name="x") result = abs(rng) exp = TimedeltaIndex( @@ -683,7 +683,7 @@ def test_tdi_add_timestamp_nat_masking(self): for variant in ts_neg_variants + ts_pos_variants: res = tdinat + variant - assert res[1] is pd.NaT + assert res[1] is NaT def test_tdi_add_overflow(self): # See GH#14068 @@ -694,7 +694,7 @@ def test_tdi_add_overflow(self): with pytest.raises(OutOfBoundsDatetime, match="10155196800000000000"): Timestamp("2000") + pd.to_timedelta(106580, "D") - _NaT = pd.NaT.value + 1 + _NaT = NaT.value + 1 msg = "Overflow in int64 addition" with pytest.raises(OverflowError, match=msg): pd.to_timedelta([106580], "D") + Timestamp("2000") @@ -711,17 +711,17 @@ def test_tdi_add_overflow(self): ) # These should not overflow! - exp = TimedeltaIndex([pd.NaT]) - result = pd.to_timedelta([pd.NaT]) - Timedelta("1 days") + exp = TimedeltaIndex([NaT]) + result = pd.to_timedelta([NaT]) - Timedelta("1 days") tm.assert_index_equal(result, exp) - exp = TimedeltaIndex(["4 days", pd.NaT]) - result = pd.to_timedelta(["5 days", pd.NaT]) - Timedelta("1 days") + exp = TimedeltaIndex(["4 days", NaT]) + result = pd.to_timedelta(["5 days", NaT]) - Timedelta("1 days") tm.assert_index_equal(result, exp) - exp = TimedeltaIndex([pd.NaT, pd.NaT, "5 hours"]) - result = pd.to_timedelta([pd.NaT, "5 days", "1 hours"]) + pd.to_timedelta( - ["7 seconds", pd.NaT, "4 hours"] + exp = TimedeltaIndex([NaT, NaT, "5 hours"]) + result = pd.to_timedelta([NaT, "5 days", "1 hours"]) + pd.to_timedelta( + ["7 seconds", NaT, "4 hours"] ) tm.assert_index_equal(result, exp) @@ -740,18 +740,18 @@ def test_timedelta_ops_with_missing_values(self): with pytest.raises(TypeError, match=msg): # Passing datetime64-dtype data to TimedeltaIndex is no longer # supported GH#29794 - pd.to_timedelta(Series([pd.NaT])) + pd.to_timedelta(Series([NaT])) - sn = pd.to_timedelta(Series([pd.NaT], dtype="m8[ns]")) + sn = pd.to_timedelta(Series([NaT], dtype="m8[ns]")) df1 = DataFrame(["00:00:01"]).apply(pd.to_timedelta) df2 = DataFrame(["00:00:02"]).apply(pd.to_timedelta) with pytest.raises(TypeError, match=msg): # Passing datetime64-dtype data to TimedeltaIndex is no longer # supported GH#29794 - DataFrame([pd.NaT]).apply(pd.to_timedelta) + DataFrame([NaT]).apply(pd.to_timedelta) - dfn = DataFrame([pd.NaT.value]).apply(pd.to_timedelta) + dfn = DataFrame([NaT.value]).apply(pd.to_timedelta) scalar1 = pd.to_timedelta("00:00:01") scalar2 = pd.to_timedelta("00:00:02") @@ -795,9 +795,9 @@ def test_timedelta_ops_with_missing_values(self): with pytest.raises(TypeError, match=msg): -np.nan + s1 - actual = s1 + pd.NaT + actual = s1 + NaT tm.assert_series_equal(actual, sn) - actual = s2 - pd.NaT + actual = s2 - NaT tm.assert_series_equal(actual, sn) actual = s1 + df1 @@ -830,9 +830,9 @@ def test_timedelta_ops_with_missing_values(self): with pytest.raises(TypeError, match=msg): df1 - np.nan - actual = df1 + pd.NaT # NaT is datetime, not timedelta + actual = df1 + NaT # NaT is datetime, not timedelta tm.assert_frame_equal(actual, dfn) - actual = df1 - pd.NaT + actual = df1 - NaT tm.assert_frame_equal(actual, dfn) # TODO: moved from tests.series.test_operators, needs splitting, cleanup, @@ -1286,7 +1286,7 @@ def test_td64arr_sub_NaT(self, box_with_array): ser = tm.box_expected(ser, box) expected = tm.box_expected(expected, box) - res = ser - pd.NaT + res = ser - NaT tm.assert_equal(res, expected) def test_td64arr_add_timedeltalike(self, two_hours, box_with_array): @@ -1329,7 +1329,7 @@ def test_td64arr_add_offset_index(self, names, box_with_array): exname = get_expected_name(box, names) tdi = TimedeltaIndex(["1 days 00:00:00", "3 days 04:00:00"], name=names[0]) - other = pd.Index([pd.offsets.Hour(n=1), pd.offsets.Minute(n=-2)], name=names[1]) + other = pd.Index([offsets.Hour(n=1), offsets.Minute(n=-2)], name=names[1]) other = np.array(other) if box in [tm.to_array, pd.array] else other expected = TimedeltaIndex( @@ -1352,7 +1352,7 @@ def test_td64arr_add_offset_array(self, box_with_array): # GH#18849 box = box_with_array tdi = TimedeltaIndex(["1 days 00:00:00", "3 days 04:00:00"]) - other = np.array([pd.offsets.Hour(n=1), pd.offsets.Minute(n=-2)]) + other = np.array([offsets.Hour(n=1), offsets.Minute(n=-2)]) expected = TimedeltaIndex( [tdi[n] + other[n] for n in range(len(tdi))], freq="infer" @@ -1376,7 +1376,7 @@ def test_td64arr_sub_offset_index(self, names, box_with_array): exname = get_expected_name(box, names) tdi = TimedeltaIndex(["1 days 00:00:00", "3 days 04:00:00"], name=names[0]) - other = pd.Index([pd.offsets.Hour(n=1), pd.offsets.Minute(n=-2)], name=names[1]) + other = pd.Index([offsets.Hour(n=1), offsets.Minute(n=-2)], name=names[1]) expected = TimedeltaIndex( [tdi[n] - other[n] for n in range(len(tdi))], freq="infer", name=exname @@ -1392,7 +1392,7 @@ def test_td64arr_sub_offset_index(self, names, box_with_array): def test_td64arr_sub_offset_array(self, box_with_array): # GH#18824 tdi = TimedeltaIndex(["1 days 00:00:00", "3 days 04:00:00"]) - other = np.array([pd.offsets.Hour(n=1), pd.offsets.Minute(n=-2)]) + other = np.array([offsets.Hour(n=1), offsets.Minute(n=-2)]) expected = TimedeltaIndex( [tdi[n] - other[n] for n in range(len(tdi))], freq="infer" @@ -1412,7 +1412,7 @@ def test_td64arr_with_offset_series(self, names, box_with_array): exname = get_expected_name(box, names) tdi = TimedeltaIndex(["1 days 00:00:00", "3 days 04:00:00"], name=names[0]) - other = Series([pd.offsets.Hour(n=1), pd.offsets.Minute(n=-2)], name=names[1]) + other = Series([offsets.Hour(n=1), offsets.Minute(n=-2)], name=names[1]) expected_add = Series([tdi[n] + other[n] for n in range(len(tdi))], name=exname) obj = tm.box_expected(tdi, box) @@ -1433,13 +1433,13 @@ def test_td64arr_with_offset_series(self, names, box_with_array): res3 = obj - other tm.assert_equal(res3, expected_sub) - @pytest.mark.parametrize("obox", [np.array, pd.Index, pd.Series]) + @pytest.mark.parametrize("obox", [np.array, pd.Index, Series]) def test_td64arr_addsub_anchored_offset_arraylike(self, obox, box_with_array): # GH#18824 tdi = TimedeltaIndex(["1 days 00:00:00", "3 days 04:00:00"]) tdi = tm.box_expected(tdi, box_with_array) - anchored = obox([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)]) + anchored = obox([offsets.MonthEnd(), offsets.Day(n=2)]) # addition/subtraction ops with anchored offsets should issue # a PerformanceWarning and _then_ raise a TypeError. @@ -1464,12 +1464,10 @@ def test_td64arr_add_sub_object_array(self, box_with_array): box = box_with_array xbox = np.ndarray if box is pd.array else box - tdi = pd.timedelta_range("1 day", periods=3, freq="D") + tdi = timedelta_range("1 day", periods=3, freq="D") tdarr = tm.box_expected(tdi, box) - other = np.array( - [Timedelta(days=1), pd.offsets.Day(2), Timestamp("2000-01-04")] - ) + other = np.array([Timedelta(days=1), offsets.Day(2), Timestamp("2000-01-04")]) with tm.assert_produces_warning(PerformanceWarning): result = tdarr + other @@ -1542,7 +1540,7 @@ def test_tdi_mul_int_array(self, box_with_array): def test_tdi_mul_int_series(self, box_with_array): box = box_with_array - xbox = pd.Series if box in [pd.Index, tm.to_array, pd.array] else box + xbox = Series if box in [pd.Index, tm.to_array, pd.array] else box idx = TimedeltaIndex(np.arange(5, dtype="int64")) expected = TimedeltaIndex(np.arange(5, dtype="int64") ** 2) @@ -1555,7 +1553,7 @@ def test_tdi_mul_int_series(self, box_with_array): def test_tdi_mul_float_series(self, box_with_array): box = box_with_array - xbox = pd.Series if box in [pd.Index, tm.to_array, pd.array] else box + xbox = Series if box in [pd.Index, tm.to_array, pd.array] else box idx = TimedeltaIndex(np.arange(5, dtype="int64")) idx = tm.box_expected(idx, box) @@ -1604,9 +1602,9 @@ def test_td64arr_div_nat_invalid(self, box_with_array): rng = tm.box_expected(rng, box_with_array) with pytest.raises(TypeError, match="unsupported operand type"): - rng / pd.NaT + rng / NaT with pytest.raises(TypeError, match="Cannot divide NaTType by"): - pd.NaT / rng + NaT / rng def test_td64arr_div_td64nat(self, box_with_array): # GH#23829 @@ -1686,7 +1684,7 @@ def test_td64arr_div_tdlike_scalar_with_nat(self, two_hours, box_with_array): box = box_with_array xbox = np.ndarray if box is pd.array else box - rng = TimedeltaIndex(["1 days", pd.NaT, "2 days"], name="foo") + rng = TimedeltaIndex(["1 days", NaT, "2 days"], name="foo") expected = pd.Float64Index([12, np.nan, 24], name="foo") rng = tm.box_expected(rng, box) @@ -1704,7 +1702,7 @@ def test_td64arr_div_td64_ndarray(self, box_with_array): box = box_with_array xbox = np.ndarray if box is pd.array else box - rng = TimedeltaIndex(["1 days", pd.NaT, "2 days"]) + rng = TimedeltaIndex(["1 days", NaT, "2 days"]) expected = pd.Float64Index([12, np.nan, 24]) rng = tm.box_expected(rng, box) @@ -1738,7 +1736,7 @@ def test_td64arr_div_td64_ndarray(self, box_with_array): tm.assert_equal(result, expected) def test_tdarr_div_length_mismatch(self, box_with_array): - rng = TimedeltaIndex(["1 days", pd.NaT, "2 days"]) + rng = TimedeltaIndex(["1 days", NaT, "2 days"]) mismatched = [1, 2, 3, 4] rng = tm.box_expected(rng, box_with_array) @@ -1871,7 +1869,7 @@ def test_td64arr_rfloordiv_tdlike_scalar(self, scalar_td, box_with_array): box = box_with_array xbox = np.ndarray if box_with_array is pd.array else box_with_array - tdi = TimedeltaIndex(["00:05:03", "00:05:03", pd.NaT], freq=None) + tdi = TimedeltaIndex(["00:05:03", "00:05:03", NaT], freq=None) expected = pd.Index([2.0, 2.0, np.nan]) tdi = tm.box_expected(tdi, box, transpose=False) @@ -1902,7 +1900,7 @@ def test_td64arr_mod_tdscalar(self, box_with_array, three_days): tm.assert_equal(result, expected) warn = None - if box_with_array is pd.DataFrame and isinstance(three_days, pd.DateOffset): + if box_with_array is DataFrame and isinstance(three_days, pd.DateOffset): warn = PerformanceWarning with tm.assert_produces_warning(warn): @@ -2084,7 +2082,7 @@ def test_td64arr_div_numeric_array( if not isinstance(vector, pd.Index): # Index.__rdiv__ won't try to operate elementwise, just raises result = tdser / vector.astype(object) - if box_with_array is pd.DataFrame: + if box_with_array is DataFrame: expected = [tdser.iloc[0, n] / vector[n] for n in range(len(vector))] else: expected = [tdser[n] / vector[n] for n in range(len(tdser))] @@ -2092,13 +2090,13 @@ def test_td64arr_div_numeric_array( expected = tm.box_expected(expected, xbox) assert tm.get_dtype(expected) == "m8[ns]" - if using_array_manager and box_with_array is pd.DataFrame: + if using_array_manager and box_with_array is DataFrame: # TODO the behaviour is buggy here (third column with all-NaT # as result doesn't get preserved as timedelta64 dtype). # Reported at https://github.com/pandas-dev/pandas/issues/39750 # Changing the expected instead of xfailing to continue to test # the correct behaviour for the other columns - expected[2] = Series([pd.NaT, pd.NaT], dtype=object) + expected[2] = Series([NaT, NaT], dtype=object) tm.assert_equal(result, expected) @@ -2132,7 +2130,7 @@ def test_td64arr_mul_int_series(self, box_with_array, names, request): # The direct operation tdi * ser still needs to be fixed. result = ser.__rmul__(tdi) - if box is pd.DataFrame: + if box is DataFrame: assert result is NotImplemented else: tm.assert_equal(result, expected) @@ -2161,7 +2159,7 @@ def test_float_series_rdiv_td64arr(self, box_with_array, names): expected = tm.box_expected(expected, xbox) result = ser.__rtruediv__(tdi) - if box is pd.DataFrame: + if box is DataFrame: # TODO: Should we skip this case sooner or test something else? assert result is NotImplemented else: @@ -2193,7 +2191,7 @@ def test_td64arr_pow_invalid(self, scalar_td, box_with_array): def test_add_timestamp_to_timedelta(): # GH: 35897 timestamp = Timestamp.now() - result = timestamp + pd.timedelta_range("0s", "1s", periods=31) + result = timestamp + timedelta_range("0s", "1s", periods=31) expected = DatetimeIndex( [ timestamp