From 0700bfe17205d11a90a2afecf998175353304c32 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 31 Oct 2019 07:24:02 -0700 Subject: [PATCH] CLN: update imports in tests (#29275) --- pandas/tests/scalar/period/test_period.py | 65 +++++++++---------- .../tests/scalar/timedelta/test_arithmetic.py | 28 ++++---- .../tests/scalar/timedelta/test_timedelta.py | 46 ++++++------- .../offsets/test_offsets_properties.py | 9 +-- pandas/tests/tslibs/test_timedeltas.py | 5 +- 5 files changed, 75 insertions(+), 78 deletions(-) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index a1de205afc0e21..bbd97291fab3f6 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -15,7 +15,6 @@ import pandas as pd from pandas import NaT, Period, Timedelta, Timestamp, offsets -import pandas.core.indexes.period as period import pandas.util.testing as tm @@ -942,7 +941,7 @@ def test_equal(self): assert self.january1 == self.january2 def test_equal_Raises_Value(self): - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): self.january1 == self.day def test_notEqual(self): @@ -953,7 +952,7 @@ def test_greater(self): assert self.february > self.january1 def test_greater_Raises_Value(self): - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): self.january1 > self.day def test_greater_Raises_Type(self): @@ -964,7 +963,7 @@ def test_greaterEqual(self): assert self.january1 >= self.january2 def test_greaterEqual_Raises_Value(self): - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): self.january1 >= self.day with pytest.raises(TypeError): @@ -974,7 +973,7 @@ def test_smallerEqual(self): assert self.january1 <= self.january2 def test_smallerEqual_Raises_Value(self): - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): self.january1 <= self.day def test_smallerEqual_Raises_Type(self): @@ -985,7 +984,7 @@ def test_smaller(self): assert self.january1 < self.february def test_smaller_Raises_Value(self): - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): self.january1 < self.day def test_smaller_Raises_Type(self): @@ -1026,7 +1025,7 @@ def test_sub_delta(self): result = left - right assert result == 4 * right.freq - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): left - Period("2007-01", freq="M") def test_add_integer(self): @@ -1097,16 +1096,16 @@ def test_sub(self): assert per2 - per1 == 14 * off msg = r"Input has different freq=M from Period\(freq=D\)" - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): per1 - Period("2011-02", freq="M") @pytest.mark.parametrize("n", [1, 2, 3, 4]) def test_sub_n_gt_1_ticks(self, tick_classes, n): # GH 23878 - p1 = pd.Period("19910905", freq=tick_classes(n)) - p2 = pd.Period("19920406", freq=tick_classes(n)) + p1 = Period("19910905", freq=tick_classes(n)) + p2 = Period("19920406", freq=tick_classes(n)) - expected = pd.Period(str(p2), freq=p2.freq.base) - pd.Period( + expected = Period(str(p2), freq=p2.freq.base) - Period( str(p1), freq=p1.freq.base ) @@ -1117,10 +1116,10 @@ def test_sub_n_gt_1_ticks(self, tick_classes, n): @pytest.mark.parametrize( "offset, kwd_name", [ - (pd.offsets.YearEnd, "month"), - (pd.offsets.QuarterEnd, "startingMonth"), - (pd.offsets.MonthEnd, None), - (pd.offsets.Week, "weekday"), + (offsets.YearEnd, "month"), + (offsets.QuarterEnd, "startingMonth"), + (offsets.MonthEnd, None), + (offsets.Week, "weekday"), ], ) def test_sub_n_gt_1_offsets(self, offset, kwd_name, n, normalize): @@ -1128,12 +1127,10 @@ def test_sub_n_gt_1_offsets(self, offset, kwd_name, n, normalize): kwds = {kwd_name: 3} if kwd_name is not None else {} p1_d = "19910905" p2_d = "19920406" - p1 = pd.Period(p1_d, freq=offset(n, normalize, **kwds)) - p2 = pd.Period(p2_d, freq=offset(n, normalize, **kwds)) + p1 = Period(p1_d, freq=offset(n, normalize, **kwds)) + p2 = Period(p2_d, freq=offset(n, normalize, **kwds)) - expected = pd.Period(p2_d, freq=p2.freq.base) - pd.Period( - p1_d, freq=p1.freq.base - ) + expected = Period(p2_d, freq=p2.freq.base) - Period(p1_d, freq=p1.freq.base) assert (p2 - p1) == expected @@ -1152,14 +1149,14 @@ def test_add_offset(self): np.timedelta64(365, "D"), timedelta(365), ]: - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): p + o if isinstance(o, np.timedelta64): with pytest.raises(TypeError): o + p else: - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): o + p for freq in ["M", "2M", "3M"]: @@ -1179,14 +1176,14 @@ def test_add_offset(self): np.timedelta64(365, "D"), timedelta(365), ]: - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): p + o if isinstance(o, np.timedelta64): with pytest.raises(TypeError): o + p else: - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): o + p # freq is Tick @@ -1226,14 +1223,14 @@ def test_add_offset(self): np.timedelta64(4, "h"), timedelta(hours=23), ]: - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): p + o if isinstance(o, np.timedelta64): with pytest.raises(TypeError): o + p else: - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): o + p for freq in ["H", "2H", "3H"]: @@ -1272,14 +1269,14 @@ def test_add_offset(self): np.timedelta64(3200, "s"), timedelta(hours=23, minutes=30), ]: - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): p + o if isinstance(o, np.timedelta64): with pytest.raises(TypeError): o + p else: - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): o + p def test_add_offset_nat(self): @@ -1376,7 +1373,7 @@ def test_sub_offset(self): np.timedelta64(365, "D"), timedelta(365), ]: - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): p - o for freq in ["M", "2M", "3M"]: @@ -1391,7 +1388,7 @@ def test_sub_offset(self): np.timedelta64(365, "D"), timedelta(365), ]: - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): p - o # freq is Tick @@ -1411,7 +1408,7 @@ def test_sub_offset(self): np.timedelta64(4, "h"), timedelta(hours=23), ]: - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): p - o for freq in ["H", "2H", "3H"]: @@ -1434,7 +1431,7 @@ def test_sub_offset(self): np.timedelta64(3200, "s"), timedelta(hours=23, minutes=30), ]: - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): p - o def test_sub_offset_nat(self): @@ -1530,10 +1527,10 @@ def test_period_ops_offset(self): assert result == exp msg = r"Input cannot be converted to Period\(freq=D\)" - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): p + offsets.Hour(2) - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): p - offsets.Hour(2) diff --git a/pandas/tests/scalar/timedelta/test_arithmetic.py b/pandas/tests/scalar/timedelta/test_arithmetic.py index 52f32d41a02ff9..2ba55b22a7c545 100644 --- a/pandas/tests/scalar/timedelta/test_arithmetic.py +++ b/pandas/tests/scalar/timedelta/test_arithmetic.py @@ -8,7 +8,7 @@ import pytest import pandas as pd -from pandas import NaT, Timedelta, Timestamp +from pandas import NaT, Timedelta, Timestamp, offsets from pandas.core import ops import pandas.util.testing as tm @@ -28,7 +28,7 @@ class TestTimedeltaAdditionSubtraction: timedelta(seconds=10), np.timedelta64(10, "s"), np.timedelta64(10000000000, "ns"), - pd.offsets.Second(10), + offsets.Second(10), ], ) def test_td_add_sub_ten_seconds(self, ten_seconds): @@ -50,7 +50,7 @@ def test_td_add_sub_ten_seconds(self, ten_seconds): Timedelta("1 days, 00:00:10"), timedelta(days=1, seconds=10), np.timedelta64(1, "D") + np.timedelta64(10, "s"), - pd.offsets.Day() + pd.offsets.Second(10), + offsets.Day() + offsets.Second(10), ], ) def test_td_add_sub_one_day_ten_seconds(self, one_day_ten_secs): @@ -114,7 +114,7 @@ def test_td_add_timedelta64(self, op): def test_td_add_offset(self, op): td = Timedelta(10, unit="d") - result = op(td, pd.offsets.Hour(6)) + result = op(td, offsets.Hour(6)) assert isinstance(result, Timedelta) assert result == Timedelta(days=10, hours=6) @@ -167,7 +167,7 @@ def test_td_sub_td64_nat(self): def test_td_sub_offset(self): td = Timedelta(10, unit="d") - result = td - pd.offsets.Hour(1) + result = td - offsets.Hour(1) assert isinstance(result, Timedelta) assert result == Timedelta(239, unit="h") @@ -192,7 +192,7 @@ def test_td_rsub_nat(self): assert result is NaT def test_td_rsub_offset(self): - result = pd.offsets.Hour(1) - Timedelta(10, unit="d") + result = offsets.Hour(1) - Timedelta(10, unit="d") assert isinstance(result, Timedelta) assert result == Timedelta(-239, unit="h") @@ -306,7 +306,7 @@ def test_td_div_timedeltalike_scalar(self): # GH#19738 td = Timedelta(10, unit="d") - result = td / pd.offsets.Hour(1) + result = td / offsets.Hour(1) assert result == 240 assert td / td == 1 @@ -342,7 +342,7 @@ def test_td_div_nan(self, nan): def test_td_rdiv_timedeltalike_scalar(self): # GH#19738 td = Timedelta(10, unit="d") - result = pd.offsets.Hour(1) / td + result = offsets.Hour(1) / td assert result == 1 / 240.0 assert np.timedelta64(60, "h") / td == 0.25 @@ -370,8 +370,8 @@ def test_td_floordiv_null_scalar(self): def test_td_floordiv_offsets(self): # GH#19738 td = Timedelta(hours=3, minutes=4) - assert td // pd.offsets.Hour(1) == 3 - assert td // pd.offsets.Minute(2) == 92 + assert td // offsets.Hour(1) == 3 + assert td // offsets.Minute(2) == 92 def test_td_floordiv_invalid_scalar(self): # GH#18846 @@ -441,7 +441,7 @@ def test_td_rfloordiv_null_scalar(self): def test_td_rfloordiv_offsets(self): # GH#19738 - assert pd.offsets.Hour(1) // Timedelta(minutes=25) == 2 + assert offsets.Hour(1) // Timedelta(minutes=25) == 2 def test_td_rfloordiv_invalid_scalar(self): # GH#18846 @@ -532,7 +532,7 @@ def test_mod_offset(self): # GH#19365 td = Timedelta(hours=37) - result = td % pd.offsets.Hour(5) + result = td % offsets.Hour(5) assert isinstance(result, Timedelta) assert result == Timedelta(hours=2) @@ -633,7 +633,7 @@ def test_divmod_offset(self): # GH#19365 td = Timedelta(days=2, hours=6) - result = divmod(td, pd.offsets.Hour(-4)) + result = divmod(td, offsets.Hour(-4)) assert result[0] == -14 assert isinstance(result[1], Timedelta) assert result[1] == Timedelta(hours=-2) @@ -653,7 +653,7 @@ def test_rdivmod_pytimedelta(self): assert result[1] == Timedelta(hours=6) def test_rdivmod_offset(self): - result = divmod(pd.offsets.Hour(54), Timedelta(hours=-4)) + result = divmod(offsets.Hour(54), Timedelta(hours=-4)) assert result[0] == -14 assert isinstance(result[1], Timedelta) assert result[1] == Timedelta(hours=-2) diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index e4980be49d35fd..9bb6c991a930a4 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -5,7 +5,7 @@ import numpy as np import pytest -from pandas._libs.tslibs import NaT, iNaT +from pandas._libs.tslibs import NaT, Timestamp, iNaT import pandas as pd from pandas import Series, Timedelta, TimedeltaIndex, timedelta_range, to_timedelta @@ -15,17 +15,17 @@ class TestTimedeltaArithmetic: def test_arithmetic_overflow(self): with pytest.raises(OverflowError): - pd.Timestamp("1700-01-01") + pd.Timedelta(13 * 19999, unit="D") + Timestamp("1700-01-01") + Timedelta(13 * 19999, unit="D") with pytest.raises(OverflowError): - pd.Timestamp("1700-01-01") + timedelta(days=13 * 19999) + Timestamp("1700-01-01") + timedelta(days=13 * 19999) def test_array_timedelta_floordiv(self): # https://github.com/pandas-dev/pandas/issues/19761 ints = pd.date_range("2012-10-08", periods=4, freq="D").view("i8") msg = r"Use 'array // timedelta.value'" with tm.assert_produces_warning(FutureWarning) as m: - result = ints // pd.Timedelta(1, unit="s") + result = ints // Timedelta(1, unit="s") assert msg in str(m[0].message) expected = np.array( @@ -283,7 +283,7 @@ def check(value): rng.milliseconds # components - tup = pd.to_timedelta(-1, "us").components + tup = to_timedelta(-1, "us").components assert tup.days == -1 assert tup.hours == 23 assert tup.minutes == 59 @@ -512,7 +512,7 @@ def test_round(self): t1 = timedelta_range("1 days", periods=3, freq="1 min 2 s 3 us") t2 = -1 * t1 t1a = timedelta_range("1 days", periods=3, freq="1 min 2 s") - t1c = pd.TimedeltaIndex([1, 1, 1], unit="D") + t1c = TimedeltaIndex([1, 1, 1], unit="D") # note that negative times round DOWN! so don't give whole numbers for (freq, s1, s2) in [ @@ -554,7 +554,7 @@ def test_round(self): freq=None, ), ), - ("d", t1c, pd.TimedeltaIndex([-1, -1, -1], unit="D")), + ("d", t1c, TimedeltaIndex([-1, -1, -1], unit="D")), ]: r1 = t1.round(freq) @@ -575,11 +575,11 @@ def test_contains(self): # Checking for any NaT-like objects # GH 13603 td = to_timedelta(range(5), unit="d") + pd.offsets.Hour(1) - for v in [pd.NaT, None, float("nan"), np.nan]: + for v in [NaT, None, float("nan"), np.nan]: assert not (v in td) - td = to_timedelta([pd.NaT]) - for v in [pd.NaT, None, float("nan"), np.nan]: + td = to_timedelta([NaT]) + for v in [NaT, None, float("nan"), np.nan]: assert v in td def test_identity(self): @@ -668,11 +668,11 @@ def conv(v): def test_overflow(self): # GH 9442 s = Series(pd.date_range("20130101", periods=100000, freq="H")) - s[0] += pd.Timedelta("1s 1ms") + s[0] += Timedelta("1s 1ms") # mean result = (s - s.min()).mean() - expected = pd.Timedelta((pd.TimedeltaIndex((s - s.min())).asi8 / len(s)).sum()) + expected = Timedelta((TimedeltaIndex((s - s.min())).asi8 / len(s)).sum()) # the computation is converted to float so # might be some loss of precision @@ -748,41 +748,41 @@ def test_total_seconds_precision(self): assert (30.0 - Timedelta("30S").total_seconds()) < 1e-20 def test_timedelta_arithmetic(self): - data = pd.Series(["nat", "32 days"], dtype="timedelta64[ns]") + data = Series(["nat", "32 days"], dtype="timedelta64[ns]") deltas = [timedelta(days=1), Timedelta(1, unit="D")] for delta in deltas: result_method = data.add(delta) result_operator = data + delta - expected = pd.Series(["nat", "33 days"], dtype="timedelta64[ns]") + expected = Series(["nat", "33 days"], dtype="timedelta64[ns]") tm.assert_series_equal(result_operator, expected) tm.assert_series_equal(result_method, expected) result_method = data.sub(delta) result_operator = data - delta - expected = pd.Series(["nat", "31 days"], dtype="timedelta64[ns]") + expected = Series(["nat", "31 days"], dtype="timedelta64[ns]") tm.assert_series_equal(result_operator, expected) tm.assert_series_equal(result_method, expected) # GH 9396 result_method = data.div(delta) result_operator = data / delta - expected = pd.Series([np.nan, 32.0], dtype="float64") + expected = Series([np.nan, 32.0], dtype="float64") tm.assert_series_equal(result_operator, expected) tm.assert_series_equal(result_method, expected) def test_apply_to_timedelta(self): - timedelta_NaT = pd.to_timedelta("NaT") + timedelta_NaT = to_timedelta("NaT") list_of_valid_strings = ["00:00:01", "00:00:02"] - a = pd.to_timedelta(list_of_valid_strings) - b = Series(list_of_valid_strings).apply(pd.to_timedelta) + a = to_timedelta(list_of_valid_strings) + b = Series(list_of_valid_strings).apply(to_timedelta) # Can't compare until apply on a Series gives the correct dtype # assert_series_equal(a, b) - list_of_strings = ["00:00:01", np.nan, pd.NaT, timedelta_NaT] + list_of_strings = ["00:00:01", np.nan, NaT, timedelta_NaT] # TODO: unused? - a = pd.to_timedelta(list_of_strings) # noqa - b = Series(list_of_strings).apply(pd.to_timedelta) # noqa + a = to_timedelta(list_of_strings) # noqa + b = Series(list_of_strings).apply(to_timedelta) # noqa # Can't compare until apply on a Series gives the correct dtype # assert_series_equal(a, b) @@ -824,7 +824,7 @@ def test_resolution_deprecated(self): (Timedelta(0, unit="ns"), False), (Timedelta(-10, unit="ns"), True), (Timedelta(None), True), - (pd.NaT, True), + (NaT, True), ], ) def test_truthiness(value, expected): diff --git a/pandas/tests/tseries/offsets/test_offsets_properties.py b/pandas/tests/tseries/offsets/test_offsets_properties.py index a05de78e299f70..351f0f9ad3b5b1 100644 --- a/pandas/tests/tseries/offsets/test_offsets_properties.py +++ b/pandas/tests/tseries/offsets/test_offsets_properties.py @@ -15,6 +15,7 @@ import pytest import pandas as pd +from pandas import Timestamp from pandas.tseries.offsets import ( BMonthBegin, @@ -36,15 +37,15 @@ with warnings.catch_warnings(): warnings.simplefilter("ignore") - min_dt = pd.Timestamp(1900, 1, 1).to_pydatetime() - max_dt = pd.Timestamp(1900, 1, 1).to_pydatetime() + min_dt = Timestamp(1900, 1, 1).to_pydatetime() + max_dt = Timestamp(1900, 1, 1).to_pydatetime() gen_date_range = st.builds( pd.date_range, start=st.datetimes( # TODO: Choose the min/max values more systematically - min_value=pd.Timestamp(1900, 1, 1).to_pydatetime(), - max_value=pd.Timestamp(2100, 1, 1).to_pydatetime(), + min_value=Timestamp(1900, 1, 1).to_pydatetime(), + max_value=Timestamp(2100, 1, 1).to_pydatetime(), ), periods=st.integers(min_value=2, max_value=100), freq=st.sampled_from("Y Q M D H T s ms us ns".split()), diff --git a/pandas/tests/tslibs/test_timedeltas.py b/pandas/tests/tslibs/test_timedeltas.py index d4bd7c2d5486ce..86d5cc749b5e10 100644 --- a/pandas/tests/tslibs/test_timedeltas.py +++ b/pandas/tests/tslibs/test_timedeltas.py @@ -3,8 +3,7 @@ from pandas._libs.tslibs.timedeltas import delta_to_nanoseconds -import pandas as pd -from pandas import Timedelta +from pandas import Timedelta, offsets @pytest.mark.parametrize( @@ -13,7 +12,7 @@ (np.timedelta64(14, "D"), 14 * 24 * 3600 * 1e9), (Timedelta(minutes=-7), -7 * 60 * 1e9), (Timedelta(minutes=-7).to_pytimedelta(), -7 * 60 * 1e9), - (pd.offsets.Nano(125), 125), + (offsets.Nano(125), 125), (1, 1), (np.int64(2), 2), (np.int32(3), 3),