diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index 541c2988a0636..561c53c68347b 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -235,9 +235,9 @@ def test_datetime(): with warnings.catch_warnings(): warnings.simplefilter("ignore", FutureWarning) - assert datetime(2015, 1, 2, 0, 0) == pd.datetime(2015, 1, 2, 0, 0) + assert datetime(2015, 1, 2, 0, 0) == datetime(2015, 1, 2, 0, 0) - assert isinstance(pd.datetime(2015, 1, 2, 0, 0), pd.datetime) + assert isinstance(datetime(2015, 1, 2, 0, 0), datetime) def test_sparsearray(): diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index a3d30cf0bc3c6..7df6caf6da636 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -212,7 +212,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) @@ -283,7 +283,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") @@ -380,7 +380,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 @@ -557,7 +557,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) @@ -607,7 +607,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) @@ -651,7 +651,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) @@ -803,8 +803,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) @@ -817,8 +817,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) @@ -831,8 +831,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) @@ -845,8 +845,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) @@ -886,7 +886,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) @@ -906,11 +906,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) @@ -920,7 +920,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 @@ -942,7 +942,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"]) @@ -957,7 +957,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"]) @@ -970,7 +970,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] @@ -1004,7 +1004,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) @@ -1018,7 +1018,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) @@ -1034,7 +1034,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) @@ -1050,7 +1050,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) @@ -1125,7 +1125,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) @@ -1505,7 +1505,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)]) @@ -1608,7 +1608,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): @@ -1830,7 +1830,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)) @@ -2025,7 +2025,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): @@ -2044,7 +2044,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" @@ -2054,7 +2054,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" @@ -2078,7 +2078,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 @@ -2100,7 +2100,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 @@ -2126,7 +2126,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 @@ -2150,7 +2150,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 @@ -2431,7 +2431,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) @@ -2451,7 +2451,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_interval.py b/pandas/tests/arithmetic/test_interval.py index 6dc3b3b13dd0c..9cc5648d11ef4 100644 --- a/pandas/tests/arithmetic/test_interval.py +++ b/pandas/tests/arithmetic/test_interval.py @@ -50,7 +50,7 @@ def left_right_dtypes(request): @pytest.fixture -def array(left_right_dtypes): +def arr(left_right_dtypes): """ Fixture to generate an IntervalArray of various dtypes containing NA if possible """ @@ -98,27 +98,27 @@ def interval_constructor(self, request): """ return request.param - def elementwise_comparison(self, op, array, other): + def elementwise_comparison(self, op, arr, other): """ - Helper that performs elementwise comparisons between `array` and `other` + Helper that performs elementwise comparisons between `arr` and `other` """ - other = other if is_list_like(other) else [other] * len(array) - expected = np.array([op(x, y) for x, y in zip(array, other)]) + other = other if is_list_like(other) else [other] * len(arr) + expected = np.array([op(x, y) for x, y in zip(arr, other)]) if isinstance(other, Series): return Series(expected, index=other.index) return expected - def test_compare_scalar_interval(self, op, array): + def test_compare_scalar_interval(self, op, arr): # matches first interval - other = array[0] - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + other = arr[0] + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_numpy_array_equal(result, expected) # matches on a single endpoint but not both - other = Interval(array.left[0], array.right[1]) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + other = Interval(arr.left[0], arr.right[1]) + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_numpy_array_equal(result, expected) def test_compare_scalar_interval_mixed_closed(self, op, closed, other_closed): @@ -129,11 +129,11 @@ def test_compare_scalar_interval_mixed_closed(self, op, closed, other_closed): expected = self.elementwise_comparison(op, array, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_scalar_na(self, op, array, nulls_fixture, request): - result = op(array, nulls_fixture) - expected = self.elementwise_comparison(op, array, nulls_fixture) + def test_compare_scalar_na(self, op, arr, nulls_fixture, request): + result = op(arr, nulls_fixture) + expected = self.elementwise_comparison(op, arr, nulls_fixture) - if nulls_fixture is pd.NA and array.dtype != pd.IntervalDtype("int64"): + if nulls_fixture is pd.NA and arr.dtype != pd.IntervalDtype("int64"): mark = pytest.mark.xfail( reason="broken for non-integer IntervalArray; see GH 31882" ) @@ -154,28 +154,28 @@ def test_compare_scalar_na(self, op, array, nulls_fixture, request): Period("2017-01-01", "D"), ], ) - def test_compare_scalar_other(self, op, array, other): - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + def test_compare_scalar_other(self, op, arr, other): + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_list_like_interval(self, op, array, interval_constructor): + def test_compare_list_like_interval(self, op, arr, interval_constructor): # same endpoints - other = interval_constructor(array.left, array.right) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + other = interval_constructor(arr.left, arr.right) + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_equal(result, expected) # different endpoints - other = interval_constructor(array.left[::-1], array.right[::-1]) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + other = interval_constructor(arr.left[::-1], arr.right[::-1]) + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_equal(result, expected) # all nan endpoints other = interval_constructor([np.nan] * 4, [np.nan] * 4) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_equal(result, expected) def test_compare_list_like_interval_mixed_closed( @@ -206,17 +206,17 @@ def test_compare_list_like_interval_mixed_closed( ), ], ) - def test_compare_list_like_object(self, op, array, other): - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + def test_compare_list_like_object(self, op, arr, other): + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_list_like_nan(self, op, array, nulls_fixture, request): + def test_compare_list_like_nan(self, op, arr, nulls_fixture, request): other = [nulls_fixture] * 4 - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) - if nulls_fixture is pd.NA and array.dtype.subtype != "i8": + if nulls_fixture is pd.NA and arr.dtype.subtype != "i8": reason = "broken for non-integer IntervalArray; see GH 31882" mark = pytest.mark.xfail(reason=reason) request.node.add_marker(mark) @@ -239,9 +239,9 @@ def test_compare_list_like_nan(self, op, array, nulls_fixture, request): ], ids=lambda x: str(x.dtype), ) - def test_compare_list_like_other(self, op, array, other): - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + def test_compare_list_like_other(self, op, arr, other): + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_numpy_array_equal(result, expected) @pytest.mark.parametrize("length", [1, 3, 5]) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index c7a99fbb5c1a8..934a767255d4b 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -32,7 +32,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) @@ -46,7 +46,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) @@ -61,13 +61,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 @@ -181,7 +181,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] @@ -583,8 +583,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) @@ -604,8 +604,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) @@ -616,7 +616,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:]) @@ -626,8 +626,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) @@ -709,7 +709,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 = ( @@ -730,7 +730,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 @@ -749,11 +749,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 @@ -763,7 +763,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) @@ -884,9 +884,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) @@ -896,9 +896,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) @@ -914,16 +914,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) @@ -1001,7 +1001,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") @@ -1028,9 +1028,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) @@ -1039,7 +1039,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 = ( @@ -1052,8 +1052,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) @@ -1064,8 +1064,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) @@ -1075,7 +1075,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 @@ -1088,8 +1088,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) @@ -1099,7 +1099,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): @@ -1110,8 +1110,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) @@ -1122,16 +1122,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 @@ -1143,8 +1143,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) @@ -1154,7 +1154,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 @@ -1168,7 +1168,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") @@ -1193,7 +1193,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) @@ -1214,7 +1214,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 @@ -1222,7 +1222,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 5f3d2040b0201..6d9420c0d3645 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -55,7 +55,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) @@ -633,7 +633,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( @@ -1475,7 +1475,7 @@ 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( @@ -2194,7 +2194,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 diff --git a/pandas/tests/arrays/categorical/test_missing.py b/pandas/tests/arrays/categorical/test_missing.py index 36ed790eff63c..9120d02a7cfb1 100644 --- a/pandas/tests/arrays/categorical/test_missing.py +++ b/pandas/tests/arrays/categorical/test_missing.py @@ -148,14 +148,14 @@ def test_use_inf_as_na_outside_context(self, values, expected): cat = Categorical(values) with pd.option_context("mode.use_inf_as_na", True): - result = pd.isna(cat) + result = isna(cat) tm.assert_numpy_array_equal(result, expected) - result = pd.isna(Series(cat)) + result = isna(Series(cat)) expected = Series(expected) tm.assert_series_equal(result, expected) - result = pd.isna(DataFrame(cat)) + result = isna(DataFrame(cat)) expected = DataFrame(expected) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/arrays/floating/test_to_numpy.py b/pandas/tests/arrays/floating/test_to_numpy.py index 26e5687b1b4a0..e551b360e4d52 100644 --- a/pandas/tests/arrays/floating/test_to_numpy.py +++ b/pandas/tests/arrays/floating/test_to_numpy.py @@ -82,7 +82,7 @@ def test_to_numpy_na_value(box): def test_to_numpy_na_value_with_nan(): - # array with both NaN and NA -> only fill NA with `na_value` + # aarray with both NaN and NA -> only fill NA with `na_value` arr = FloatingArray(np.array([0.0, np.nan, 0.0]), np.array([False, False, True])) result = arr.to_numpy(dtype="float64", na_value=-1) expected = np.array([0.0, np.nan, -1.0], dtype="float64") diff --git a/pandas/tests/arrays/interval/test_interval.py b/pandas/tests/arrays/interval/test_interval.py index e5ccb51ce36f5..08241dccc9bd1 100644 --- a/pandas/tests/arrays/interval/test_interval.py +++ b/pandas/tests/arrays/interval/test_interval.py @@ -90,7 +90,7 @@ def test_shift(self): tm.assert_interval_array_equal(result, expected) def test_shift_datetime(self): - a = IntervalArray.from_breaks(pd.date_range("2000", periods=4)) + a = IntervalArray.from_breaks(date_range("2000", periods=4)) result = a.shift(2) expected = a.take([-1, -1, 0], allow_fill=True) tm.assert_interval_array_equal(result, expected) @@ -223,7 +223,7 @@ def test_arrow_array_missing(): @pyarrow_skip @pytest.mark.parametrize( "breaks", - [[0.0, 1.0, 2.0, 3.0], pd.date_range("2017", periods=4, freq="D")], + [[0.0, 1.0, 2.0, 3.0], date_range("2017", periods=4, freq="D")], ids=["float", "datetime64[ns]"], ) def test_arrow_table_roundtrip(breaks): @@ -250,7 +250,7 @@ def test_arrow_table_roundtrip(breaks): @pyarrow_skip @pytest.mark.parametrize( "breaks", - [[0.0, 1.0, 2.0, 3.0], pd.date_range("2017", periods=4, freq="D")], + [[0.0, 1.0, 2.0, 3.0], date_range("2017", periods=4, freq="D")], ids=["float", "datetime64[ns]"], ) def test_arrow_table_roundtrip_without_metadata(breaks): diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index 7f1b0f49ebd1e..e9f5f521c539d 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -182,8 +182,8 @@ def test_constructor_spindex_dtype_scalar_broadcasts(self): def test_constructor_inferred_fill_value(self, data, fill_value): result = SparseArray(data).fill_value - if pd.isna(fill_value): - assert pd.isna(result) + if isna(fill_value): + assert isna(result) else: assert result == fill_value diff --git a/pandas/tests/base/test_conversion.py b/pandas/tests/base/test_conversion.py index 94330f861eacd..6fd09219dc7f5 100644 --- a/pandas/tests/base/test_conversion.py +++ b/pandas/tests/base/test_conversion.py @@ -318,7 +318,7 @@ def test_array_multiindex_raises(): ), # GH#26406 tz is preserved in Categorical[dt64tz] ( - pd.Categorical(pd.date_range("2016-01-01", periods=2, tz="US/Pacific")), + pd.Categorical(date_range("2016-01-01", periods=2, tz="US/Pacific")), np.array( [ Timestamp("2016-01-01", tz="US/Pacific"), diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 8ba8562affb67..b3a34b1a3f78e 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -760,7 +760,7 @@ class TestCategoricalDtypeParametrized: np.arange(1000), ["a", "b", 10, 2, 1.3, True], [True, False], - pd.date_range("2017", periods=4), + date_range("2017", periods=4), ], ) def test_basic(self, categories, ordered): diff --git a/pandas/tests/dtypes/test_missing.py b/pandas/tests/dtypes/test_missing.py index c02185dd82043..0221614a0dfb8 100644 --- a/pandas/tests/dtypes/test_missing.py +++ b/pandas/tests/dtypes/test_missing.py @@ -188,7 +188,7 @@ def test_isna_datetime(self): def test_isna_old_datetimelike(self): # isna_old should work for dt64tz, td64, and period, not just tznaive - dti = pd.date_range("2016-01-01", periods=3) + dti = date_range("2016-01-01", periods=3) dta = dti._data dta[-1] = pd.NaT expected = np.array([False, False, True], dtype=bool) @@ -197,7 +197,7 @@ def test_isna_old_datetimelike(self): for obj in objs: with cf.option_context("mode.use_inf_as_na", True): - result = pd.isna(obj) + result = isna(obj) tm.assert_numpy_array_equal(result, expected) diff --git a/pandas/tests/extension/arrow/arrays.py b/pandas/tests/extension/arrow/arrays.py index 65c5102e22997..1bc59d7e02144 100644 --- a/pandas/tests/extension/arrow/arrays.py +++ b/pandas/tests/extension/arrow/arrays.py @@ -14,6 +14,8 @@ import numpy as np import pyarrow as pa +from pandas.core.dtypes.missing import isna + import pandas as pd from pandas.api.extensions import ( ExtensionArray, @@ -116,7 +118,7 @@ def _logical_method(self, other, op): result = op(np.array(self._data), np.array(other._data)) return ArrowBoolArray( - pa.chunked_array([pa.array(result, mask=pd.isna(self._data.to_pandas()))]) + pa.chunked_array([pa.array(result, mask=isna(self._data.to_pandas()))]) ) def __eq__(self, other): @@ -135,7 +137,7 @@ def nbytes(self) -> int: ) def isna(self): - nas = pd.isna(self._data.to_pandas()) + nas = isna(self._data.to_pandas()) return type(self).from_scalars(nas) def take(self, indices, allow_fill=False, fill_value=None): diff --git a/pandas/tests/extension/decimal/test_decimal.py b/pandas/tests/extension/decimal/test_decimal.py index 233b658d29782..2202c18d995a0 100644 --- a/pandas/tests/extension/decimal/test_decimal.py +++ b/pandas/tests/extension/decimal/test_decimal.py @@ -7,6 +7,7 @@ import pandas as pd import pandas._testing as tm +from pandas.core.construction import array from pandas.tests.extension import base from .array import DecimalArray, DecimalDtype, make_data, to_decimal @@ -514,7 +515,7 @@ def test_to_numpy_keyword(): expected = np.array( [decimal.Decimal("1.11"), decimal.Decimal("2.22")], dtype="object" ) - a = pd.array(values, dtype="decimal") + a = array(values, dtype="decimal") result = a.to_numpy(decimals=2) tm.assert_numpy_array_equal(result, expected) diff --git a/pandas/tests/extension/list/array.py b/pandas/tests/extension/list/array.py index d86f90e58d897..aa06be6359d47 100644 --- a/pandas/tests/extension/list/array.py +++ b/pandas/tests/extension/list/array.py @@ -11,6 +11,7 @@ import numpy as np from pandas.core.dtypes.base import ExtensionDtype +from pandas.core.dtypes.missing import isna import pandas as pd from pandas.core.arrays import ExtensionArray @@ -41,7 +42,7 @@ def __init__(self, values, dtype=None, copy=False): if not isinstance(values, np.ndarray): raise TypeError("Need to pass a numpy array as values") for val in values: - if not isinstance(val, self.dtype.type) and not pd.isna(val): + if not isinstance(val, self.dtype.type) and not isna(val): raise TypeError("All values must be of type " + str(self.dtype.type)) self.data = values diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index ffd56b9c23bc8..5e40d6283ab64 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -118,7 +118,7 @@ class TestConstructors(BaseSparseTests, base.BaseConstructorsTests): class TestReshaping(BaseSparseTests, base.BaseReshapingTests): def test_concat_mixed_dtypes(self, data): # https://github.com/pandas-dev/pandas/issues/20762 - # This should be the same, aside from concat([sparse, float]) + # This should be the same, aside from pd.concat([sparse, float]) df1 = pd.DataFrame({"A": data[:3]}) df2 = pd.DataFrame({"A": [1, 2, 3]}) df3 = pd.DataFrame({"A": ["a", "b", "c"]}).astype("category") diff --git a/pandas/tests/frame/apply/test_frame_apply.py b/pandas/tests/frame/apply/test_frame_apply.py index 9ec56c3429b22..ffceff456b284 100644 --- a/pandas/tests/frame/apply/test_frame_apply.py +++ b/pandas/tests/frame/apply/test_frame_apply.py @@ -697,14 +697,14 @@ def test_frame_apply_dont_convert_datetime64(self): def test_apply_non_numpy_dtype(self): # GH 12244 df = DataFrame( - {"dt": pd.date_range("2015-01-01", periods=3, tz="Europe/Brussels")} + {"dt": date_range("2015-01-01", periods=3, tz="Europe/Brussels")} ) result = df.apply(lambda x: x) tm.assert_frame_equal(result, df) result = df.apply(lambda x: x + pd.Timedelta("1day")) expected = DataFrame( - {"dt": pd.date_range("2015-01-02", periods=3, tz="Europe/Brussels")} + {"dt": date_range("2015-01-02", periods=3, tz="Europe/Brussels")} ) tm.assert_frame_equal(result, expected) @@ -1169,7 +1169,7 @@ def test_agg_multiple_mixed_no_warning(self): "A": [1, 2, 3], "B": [1.0, 2.0, 3.0], "C": ["foo", "bar", "baz"], - "D": pd.date_range("20130101", periods=3), + "D": date_range("20130101", periods=3), } ) expected = DataFrame( @@ -1285,7 +1285,7 @@ def test_nuiscance_columns(self): "A": [1, 2, 3], "B": [1.0, 2.0, 3.0], "C": ["foo", "bar", "baz"], - "D": pd.date_range("20130101", periods=3), + "D": date_range("20130101", periods=3), } ) diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 4cbdf61ff8dae..8480ae887aae0 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -212,7 +212,7 @@ def test_setitem_multi_index(self): it = ["jim", "joe", "jolie"], ["first", "last"], ["left", "center", "right"] cols = MultiIndex.from_product(it) - index = pd.date_range("20141006", periods=20) + index = date_range("20141006", periods=20) vals = np.random.randint(1, 1000, (len(index), len(cols))) df = DataFrame(vals, columns=cols, index=index) @@ -1349,7 +1349,7 @@ def test_loc_duplicates(self): # gh-17105 # insert a duplicate element to the index - trange = pd.date_range( + trange = date_range( start=Timestamp(year=2017, month=1, day=1), end=Timestamp(year=2017, month=1, day=5), ) @@ -1413,7 +1413,7 @@ def test_setitem_with_unaligned_tz_aware_datetime_column(self): # GH 12981 # Assignment of unaligned offset-aware datetime series. # Make sure timezone isn't lost - column = Series(pd.date_range("2015-01-01", periods=3, tz="utc"), name="dates") + column = Series(date_range("2015-01-01", periods=3, tz="utc"), name="dates") df = DataFrame({"dates": column}) df["dates"] = column[[1, 0, 2]] tm.assert_series_equal(df["dates"], column) @@ -1722,7 +1722,7 @@ def test_object_casting_indexing_wraps_datetimelike(): df = DataFrame( { "A": [1, 2], - "B": pd.date_range("2000", periods=2), + "B": date_range("2000", periods=2), "C": pd.timedelta_range("1 Day", periods=2), } ) diff --git a/pandas/tests/frame/methods/test_describe.py b/pandas/tests/frame/methods/test_describe.py index b7692eee16bf8..94dc31ce0bf51 100644 --- a/pandas/tests/frame/methods/test_describe.py +++ b/pandas/tests/frame/methods/test_describe.py @@ -271,7 +271,7 @@ def test_describe_tz_values(self, tz_naive_fixture): tm.assert_frame_equal(result, expected) def test_datetime_is_numeric_includes_datetime(self): - df = DataFrame({"a": pd.date_range("2012", periods=3), "b": [1, 2, 3]}) + df = DataFrame({"a": date_range("2012", periods=3), "b": [1, 2, 3]}) result = df.describe(datetime_is_numeric=True) expected = DataFrame( { diff --git a/pandas/tests/frame/methods/test_diff.py b/pandas/tests/frame/methods/test_diff.py index b8328b43a6b13..a5835ff9bfda2 100644 --- a/pandas/tests/frame/methods/test_diff.py +++ b/pandas/tests/frame/methods/test_diff.py @@ -75,7 +75,7 @@ def test_diff_datetime_axis0_with_nat(self, tz): @pytest.mark.parametrize("tz", [None, "UTC"]) def test_diff_datetime_with_nat_zero_periods(self, tz): # diff on NaT values should give NaT, not timedelta64(0) - dti = pd.date_range("2016-01-01", periods=4, tz=tz) + dti = date_range("2016-01-01", periods=4, tz=tz) ser = Series(dti) df = ser.to_frame() @@ -173,7 +173,7 @@ def test_diff_axis(self): def test_diff_period(self): # GH#32995 Don't pass an incorrect axis - pi = pd.date_range("2016-01-01", periods=3).to_period("D") + pi = date_range("2016-01-01", periods=3).to_period("D") df = DataFrame({"A": pi}) result = df.diff(1, axis=1) diff --git a/pandas/tests/frame/methods/test_reindex.py b/pandas/tests/frame/methods/test_reindex.py index 3e4e16955b44a..1917f9903cd6c 100644 --- a/pandas/tests/frame/methods/test_reindex.py +++ b/pandas/tests/frame/methods/test_reindex.py @@ -151,7 +151,7 @@ def test_reindex_methods_nearest_special(self): def test_reindex_nearest_tz(self, tz_aware_fixture): # GH26683 tz = tz_aware_fixture - idx = pd.date_range("2019-01-01", periods=5, tz=tz) + idx = date_range("2019-01-01", periods=5, tz=tz) df = DataFrame({"x": list(range(5))}, index=idx) expected = df.head(3) diff --git a/pandas/tests/frame/methods/test_reset_index.py b/pandas/tests/frame/methods/test_reset_index.py index 00d4a4277a42f..17e1ae102ca06 100644 --- a/pandas/tests/frame/methods/test_reset_index.py +++ b/pandas/tests/frame/methods/test_reset_index.py @@ -394,7 +394,7 @@ def test_reset_index_multiindex_columns(self): def test_reset_index_datetime(self, tz_naive_fixture): # GH#3950 tz = tz_naive_fixture - idx1 = pd.date_range("1/1/2011", periods=5, freq="D", tz=tz, name="idx1") + idx1 = date_range("1/1/2011", periods=5, freq="D", tz=tz, name="idx1") idx2 = Index(range(5), name="idx2", dtype="int64") idx = MultiIndex.from_arrays([idx1, idx2]) df = DataFrame( @@ -421,7 +421,7 @@ def test_reset_index_datetime(self, tz_naive_fixture): tm.assert_frame_equal(df.reset_index(), expected) - idx3 = pd.date_range( + idx3 = date_range( "1/1/2012", periods=5, freq="MS", tz="Europe/Paris", name="idx3" ) idx = MultiIndex.from_arrays([idx1, idx2, idx3]) @@ -460,7 +460,7 @@ def test_reset_index_datetime(self, tz_naive_fixture): # GH#7793 idx = MultiIndex.from_product( - [["a", "b"], pd.date_range("20130101", periods=3, tz=tz)] + [["a", "b"], date_range("20130101", periods=3, tz=tz)] ) df = DataFrame( np.arange(6, dtype="int64").reshape(6, 1), columns=["a"], index=idx diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index 4cf0b1febf0af..8d8b080ef6c8d 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -41,7 +41,7 @@ def read_csv(self, path, **kwargs): params = {"index_col": 0, "parse_dates": True} params.update(**kwargs) - return pd.read_csv(path, **params) + return read_csv(path, **params) def test_to_csv_from_csv1(self, float_frame, datetime_frame): @@ -123,7 +123,7 @@ def test_to_csv_from_csv3(self): df1.to_csv(path) df2.to_csv(path, mode="a", header=False) xp = pd.concat([df1, df2]) - rs = pd.read_csv(path, index_col=0) + rs = read_csv(path, index_col=0) rs.columns = [int(label) for label in rs.columns] xp.columns = [int(label) for label in xp.columns] tm.assert_frame_equal(xp, rs) @@ -139,7 +139,7 @@ def test_to_csv_from_csv4(self): ) df.to_csv(path) - result = pd.read_csv(path, index_col="dt_index") + result = read_csv(path, index_col="dt_index") result.index = pd.to_timedelta(result.index) # TODO: remove renaming when GH 10875 is solved result.index = result.index.rename("dt_index") @@ -153,7 +153,7 @@ def test_to_csv_from_csv5(self, timezone_frame): with tm.ensure_clean("__tmp_to_csv_from_csv5__") as path: timezone_frame.to_csv(path) - result = pd.read_csv(path, index_col=0, parse_dates=["A"]) + result = read_csv(path, index_col=0, parse_dates=["A"]) converter = ( lambda c: to_datetime(result[c]) @@ -166,7 +166,6 @@ def test_to_csv_from_csv5(self, timezone_frame): def test_to_csv_cols_reordering(self): # GH3454 - import pandas as pd chunksize = 5 N = int(chunksize * 2.5) @@ -177,17 +176,15 @@ def test_to_csv_cols_reordering(self): with tm.ensure_clean() as path: df.to_csv(path, columns=cols, chunksize=chunksize) - rs_c = pd.read_csv(path, index_col=0) + rs_c = read_csv(path, index_col=0) tm.assert_frame_equal(df[cols], rs_c, check_names=False) def test_to_csv_new_dupe_cols(self): - import pandas as pd - def _check_df(df, cols=None): with tm.ensure_clean() as path: df.to_csv(path, columns=cols, chunksize=chunksize) - rs_c = pd.read_csv(path, index_col=0) + rs_c = read_csv(path, index_col=0) # we wrote them in a different order # so compare them in that order @@ -999,7 +996,7 @@ def test_to_csv_path_is_none(self, float_frame): # Series.to_csv() csv_str = float_frame.to_csv(path_or_buf=None) assert isinstance(csv_str, str) - recons = pd.read_csv(StringIO(csv_str), index_col=0) + recons = read_csv(StringIO(csv_str), index_col=0) tm.assert_frame_equal(float_frame, recons) @pytest.mark.parametrize( @@ -1040,7 +1037,7 @@ def test_to_csv_compression(self, df, encoding, compression): df.to_csv(handles.handle, encoding=encoding) assert not handles.handle.closed - result = pd.read_csv( + result = read_csv( filename, compression=compression, encoding=encoding, @@ -1122,7 +1119,7 @@ def test_to_csv_with_dst_transitions(self): with tm.ensure_clean("csv_date_format_with_dst") as path: # make sure we are not failing on transitions - times = pd.date_range( + times = date_range( "2013-10-26 23:00", "2013-10-27 01:00", tz="Europe/London", @@ -1144,7 +1141,7 @@ def test_to_csv_with_dst_transitions(self): tm.assert_frame_equal(result, df) # GH11619 - idx = pd.date_range("2015-01-01", "2015-12-31", freq="H", tz="Europe/Paris") + idx = date_range("2015-01-01", "2015-12-31", freq="H", tz="Europe/Paris") idx = idx._with_freq(None) # freq does not round-trip idx._data._freq = None # otherwise there is trouble on unpickle df = DataFrame({"values": 1, "idx": idx}, index=idx) @@ -1250,7 +1247,7 @@ def test_to_csv_quoting(self): # presents with encoding? text_rows = ["a,b,c", '1,"test \r\n",3'] text = tm.convert_rows_list_to_csv_str(text_rows) - df = pd.read_csv(StringIO(text)) + df = read_csv(StringIO(text)) buf = StringIO() df.to_csv(buf, encoding="utf-8", index=False) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index f408a3ddde04e..44bc6855be272 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -2104,7 +2104,7 @@ class DatetimeSubclass(datetime): def test_with_mismatched_index_length_raises(self): # GH#33437 - dti = pd.date_range("2016-01-01", periods=3, tz="US/Pacific") + dti = date_range("2016-01-01", periods=3, tz="US/Pacific") with pytest.raises(ValueError, match="Shape of passed values"): DataFrame(dti, index=range(4)) diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index 196d4a0b3bb76..970616cb2dba9 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -825,7 +825,7 @@ def test_sum_nanops_timedelta(self): idx = ["a", "b", "c"] df = DataFrame({"a": [0, 0], "b": [0, np.nan], "c": [np.nan, np.nan]}) - df2 = df.apply(pd.to_timedelta) + df2 = df.apply(to_timedelta) # 0 by default result = df2.sum() @@ -855,9 +855,9 @@ def test_sum_bool(self, float_frame): def test_sum_mixed_datetime(self): # GH#30886 - df = DataFrame( - {"A": pd.date_range("2000", periods=4), "B": [1, 2, 3, 4]} - ).reindex([2, 3, 4]) + df = DataFrame({"A": date_range("2000", periods=4), "B": [1, 2, 3, 4]}).reindex( + [2, 3, 4] + ) result = df.sum() expected = Series({"B": 7.0}) @@ -887,7 +887,7 @@ def test_mean_datetimelike(self): df = DataFrame( { "A": np.arange(3), - "B": pd.date_range("2016-01-01", periods=3), + "B": date_range("2016-01-01", periods=3), "C": pd.timedelta_range("1D", periods=3), "D": pd.period_range("2016", periods=3, freq="A"), } @@ -906,7 +906,7 @@ def test_mean_datetimelike_numeric_only_false(self): df = DataFrame( { "A": np.arange(3), - "B": pd.date_range("2016-01-01", periods=3), + "B": date_range("2016-01-01", periods=3), "C": pd.timedelta_range("1D", periods=3), } ) @@ -977,7 +977,7 @@ def test_idxmax(self, float_frame, int_frame): def test_idxmax_mixed_dtype(self): # don't cast to object, which would raise in nanops - dti = pd.date_range("2016-01-01", periods=3) + dti = date_range("2016-01-01", periods=3) df = DataFrame({1: [0, 2, 1], 2: range(3)[::-1], 3: dti}) @@ -1265,8 +1265,8 @@ def test_min_max_dt64_api_consistency_with_NaT(self): # returned NaT for series. These tests check that the API is consistent in # min/max calls on empty Series/DataFrames. See GH:33704 for more # information - df = DataFrame({"x": pd.to_datetime([])}) - expected_dt_series = Series(pd.to_datetime([])) + df = DataFrame({"x": to_datetime([])}) + expected_dt_series = Series(to_datetime([])) # check axis 0 assert (df.min(axis=0).x is pd.NaT) == (expected_dt_series.min() is pd.NaT) assert (df.max(axis=0).x is pd.NaT) == (expected_dt_series.max() is pd.NaT) @@ -1294,7 +1294,7 @@ def test_min_max_dt64_api_consistency_empty_df(self): @pytest.mark.parametrize("method", ["min", "max"]) def test_preserve_timezone(self, initial: str, method): # GH 28552 - initial_dt = pd.to_datetime(initial) + initial_dt = to_datetime(initial) expected = Series([initial_dt]) df = DataFrame([expected]) result = getattr(df, method)(axis=1) @@ -1322,7 +1322,7 @@ def test_frame_any_with_timedelta(self): df = DataFrame( { "a": Series([0, 0]), - "t": Series([pd.to_timedelta(0, "s"), pd.to_timedelta(1, "ms")]), + "t": Series([to_timedelta(0, "s"), to_timedelta(1, "ms")]), } ) diff --git a/pandas/tests/frame/test_stack_unstack.py b/pandas/tests/frame/test_stack_unstack.py index e8ae9f6584ad6..72b6f699a72c8 100644 --- a/pandas/tests/frame/test_stack_unstack.py +++ b/pandas/tests/frame/test_stack_unstack.py @@ -150,7 +150,7 @@ def test_unstack_fill_frame(self): def test_unstack_fill_frame_datetime(self): # Test unstacking with date times - dv = pd.date_range("2012-01-01", periods=4).values + dv = date_range("2012-01-01", periods=4).values data = Series(dv) data.index = MultiIndex.from_tuples( [("x", "a"), ("x", "b"), ("y", "b"), ("z", "a")] @@ -600,7 +600,7 @@ def test_unstack_dtypes(self): "A": ["a"] * 5, "C": c, "D": d, - "B": pd.date_range("2012-01-01", periods=5), + "B": date_range("2012-01-01", periods=5), } ) @@ -934,7 +934,7 @@ def verify(df): df = DataFrame( { "1st": [1, 2, 1, 2, 1, 2], - "2nd": pd.date_range("2014-02-01", periods=6, freq="D"), + "2nd": date_range("2014-02-01", periods=6, freq="D"), "jim": 100 + np.arange(6), "joe": (np.random.randn(6) * 10).round(2), } @@ -1163,9 +1163,7 @@ def test_unstack_timezone_aware_values(): def test_stack_timezone_aware_values(): # GH 19420 - ts = pd.date_range( - freq="D", start="20180101", end="20180103", tz="America/New_York" - ) + ts = date_range(freq="D", start="20180101", end="20180103", tz="America/New_York") df = DataFrame({"A": ts}, index=["a", "b", "c"]) result = df.stack() expected = Series( diff --git a/pandas/tests/groupby/aggregate/test_other.py b/pandas/tests/groupby/aggregate/test_other.py index 5d0f6d6262899..eca8a75a0a807 100644 --- a/pandas/tests/groupby/aggregate/test_other.py +++ b/pandas/tests/groupby/aggregate/test_other.py @@ -435,7 +435,7 @@ def test_agg_over_numpy_arrays(): def test_agg_tzaware_non_datetime_result(): # discussed in GH#29589, fixed in GH#29641, operating on tzaware values # with function that is not dtype-preserving - dti = pd.date_range("2012-01-01", periods=4, tz="UTC") + dti = date_range("2012-01-01", periods=4, tz="UTC") df = DataFrame({"a": [0, 0, 1, 1], "b": dti}) gb = df.groupby("a") diff --git a/pandas/tests/groupby/test_function.py b/pandas/tests/groupby/test_function.py index 8d7fcbfcfe694..9b0877d77980a 100644 --- a/pandas/tests/groupby/test_function.py +++ b/pandas/tests/groupby/test_function.py @@ -552,7 +552,7 @@ def test_idxmin_idxmax_axis1(): tm.assert_series_equal(alt[indexer], res.droplevel("A")) - df["E"] = pd.date_range("2016-01-01", periods=10) + df["E"] = date_range("2016-01-01", periods=10) gb2 = df.groupby("A") msg = "reduction operation 'argmax' not allowed for this dtype" diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index e5021b7b4dd5f..94a0091846fe1 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -1231,7 +1231,7 @@ def test_groupby_list_infer_array_like(df): def test_groupby_keys_same_size_as_index(): # GH 11185 freq = "s" - index = pd.date_range( + index = date_range( start=Timestamp("2015-09-29T11:34:44-0700"), periods=2, freq=freq ) df = DataFrame([["A", 10], ["B", 15]], columns=["metric", "values"], index=index) @@ -1759,7 +1759,7 @@ def test_pivot_table_values_key_error(): # This test is designed to replicate the error in issue #14938 df = DataFrame( { - "eventDate": pd.date_range(datetime.today(), periods=20, freq="M").tolist(), + "eventDate": date_range(datetime.today(), periods=20, freq="M").tolist(), "thename": range(0, 20), } ) @@ -1815,7 +1815,7 @@ def test_groupby_agg_ohlc_non_first(): df = DataFrame( [[1], [1]], columns=["foo"], - index=pd.date_range("2018-01-01", periods=2, freq="D"), + index=date_range("2018-01-01", periods=2, freq="D"), ) expected = DataFrame( @@ -1829,7 +1829,7 @@ def test_groupby_agg_ohlc_non_first(): ("foo", "ohlc", "close"), ) ), - index=pd.date_range("2018-01-01", periods=2, freq="D"), + index=date_range("2018-01-01", periods=2, freq="D"), ) result = df.groupby(Grouper(freq="D")).agg(["sum", "ohlc"]) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 10e69ddcd5f80..f68ab43b84e42 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -608,7 +608,7 @@ def test_grouping_labels(self, mframe): def test_list_grouper_with_nat(self): # GH 14715 - df = DataFrame({"date": pd.date_range("1/1/2011", periods=365, freq="D")}) + df = DataFrame({"date": date_range("1/1/2011", periods=365, freq="D")}) df.iloc[-1] = pd.NaT grouper = pd.Grouper(key="date", freq="AS") diff --git a/pandas/tests/groupby/test_timegrouper.py b/pandas/tests/groupby/test_timegrouper.py index 28095c0b0c39f..8e68be1f03bab 100644 --- a/pandas/tests/groupby/test_timegrouper.py +++ b/pandas/tests/groupby/test_timegrouper.py @@ -434,7 +434,7 @@ def sumfunc_value(x): def test_groupby_groups_datetimeindex(self): # GH#1430 periods = 1000 - ind = pd.date_range(start="2012/1/1", freq="5min", periods=periods) + ind = date_range(start="2012/1/1", freq="5min", periods=periods) df = DataFrame( {"high": np.arange(periods), "low": np.arange(periods)}, index=ind ) @@ -445,7 +445,7 @@ def test_groupby_groups_datetimeindex(self): assert isinstance(list(groups.keys())[0], datetime) # GH#11442 - index = pd.date_range("2015/01/01", periods=5, name="date") + index = date_range("2015/01/01", periods=5, name="date") df = DataFrame({"A": [5, 6, 7, 8, 9], "B": [1, 2, 3, 4, 5]}, index=index) result = df.groupby(level="date").groups dates = ["2015-01-05", "2015-01-04", "2015-01-03", "2015-01-02", "2015-01-01"] @@ -672,9 +672,7 @@ def test_groupby_with_timezone_selection(self): df = DataFrame( { "factor": np.random.randint(0, 3, size=60), - "time": pd.date_range( - "01/01/2000 00:00", periods=60, freq="s", tz="UTC" - ), + "time": date_range("01/01/2000 00:00", periods=60, freq="s", tz="UTC"), } ) df1 = df.groupby("factor").max()["time"] @@ -693,7 +691,7 @@ def test_timezone_info(self): def test_datetime_count(self): df = DataFrame( - {"a": [1, 2, 3] * 2, "dates": pd.date_range("now", periods=6, freq="T")} + {"a": [1, 2, 3] * 2, "dates": date_range("now", periods=6, freq="T")} ) result = df.groupby("a").dates.count() expected = Series([2, 2, 2], index=Index([1, 2, 3], name="a"), name="dates") diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py index 72637400ff023..99fbfae41d500 100644 --- a/pandas/tests/groupby/transform/test_transform.py +++ b/pandas/tests/groupby/transform/test_transform.py @@ -98,7 +98,7 @@ def test_transform_fast(): { "grouping": [0, 1, 1, 3], "f": [1.1, 2.1, 3.1, 4.5], - "d": pd.date_range("2014-1-1", "2014-1-4"), + "d": date_range("2014-1-1", "2014-1-4"), "i": [1, 2, 3, 4], }, columns=["grouping", "f", "i", "d"], @@ -340,7 +340,7 @@ def test_transform_transformation_func(transformation_func): "A": ["foo", "foo", "foo", "foo", "bar", "bar", "baz"], "B": [1, 2, np.nan, 3, 3, np.nan, 4], }, - index=pd.date_range("2020-01-01", "2020-01-07"), + index=date_range("2020-01-01", "2020-01-07"), ) if transformation_func == "cumcount": @@ -406,7 +406,7 @@ def test_transform_function_aliases(df): def test_series_fast_transform_date(): # GH 13191 df = DataFrame( - {"grouping": [np.nan, 1, 1, 3], "d": pd.date_range("2014-1-1", "2014-1-4")} + {"grouping": [np.nan, 1, 1, 3], "d": date_range("2014-1-1", "2014-1-4")} ) result = df.groupby("grouping")["d"].transform("first") dates = [ @@ -642,7 +642,7 @@ def test_cython_transform_frame(op, args, targop): "float": s, "float_missing": s_missing, "int": [1, 1, 1, 1, 2] * 200, - "datetime": pd.date_range("1990-1-1", periods=1000), + "datetime": date_range("1990-1-1", periods=1000), "timedelta": pd.timedelta_range(1, freq="s", periods=1000), "string": strings * 50, "string_missing": strings_missing * 50, @@ -684,7 +684,7 @@ def test_cython_transform_frame(op, args, targop): # to apply separately and concat i = gb[["int"]].apply(targop) f = gb[["float", "float_missing"]].apply(targop) - expected = pd.concat([f, i], axis=1) + expected = concat([f, i], axis=1) else: expected = gb.apply(targop) @@ -754,7 +754,7 @@ def test_transform_numeric_ret(cols, exp, comp_func, agg_func, request): # GH 19200 df = DataFrame( - {"a": pd.date_range("2018-01-01", periods=3), "b": range(3), "c": range(7, 10)} + {"a": date_range("2018-01-01", periods=3), "b": range(3), "c": range(7, 10)} ) result = df.groupby("b")[cols].transform(agg_func) @@ -974,7 +974,7 @@ def test_groupby_transform_timezone_column(func): ) def test_groupby_transform_with_datetimes(func, values): # GH 15306 - dates = pd.date_range("1/1/2011", periods=10, freq="D") + dates = date_range("1/1/2011", periods=10, freq="D") stocks = DataFrame({"price": np.arange(10.0)}, index=dates) stocks["week_id"] = dates.isocalendar().week diff --git a/pandas/tests/indexes/multi/test_integrity.py b/pandas/tests/indexes/multi/test_integrity.py index f9ab0b3aceec4..b47dfc504cb84 100644 --- a/pandas/tests/indexes/multi/test_integrity.py +++ b/pandas/tests/indexes/multi/test_integrity.py @@ -4,6 +4,7 @@ import pytest from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike +from pandas.core.dtypes.missing import isna import pandas as pd from pandas import IntervalIndex, MultiIndex, RangeIndex @@ -161,7 +162,7 @@ def test_isna_behavior(idx): # isna(MI) msg = "isna is not defined for MultiIndex" with pytest.raises(NotImplementedError, match=msg): - pd.isna(idx) + isna(idx) def test_large_multiindex_error(): diff --git a/pandas/tests/indexes/period/test_setops.py b/pandas/tests/indexes/period/test_setops.py index 40d1263ff70cd..5f3a8b7210707 100644 --- a/pandas/tests/indexes/period/test_setops.py +++ b/pandas/tests/indexes/period/test_setops.py @@ -343,7 +343,7 @@ def test_difference_freq(self, sort): def test_intersection_equal_duplicates(self): # GH#38302 - idx = pd.period_range("2011-01-01", periods=2) + idx = period_range("2011-01-01", periods=2) idx_dup = idx.append(idx) result = idx_dup.intersection(idx_dup) tm.assert_index_equal(result, idx) diff --git a/pandas/tests/indexing/test_datetime.py b/pandas/tests/indexing/test_datetime.py index d00fe58265a2e..fc9331fce7163 100644 --- a/pandas/tests/indexing/test_datetime.py +++ b/pandas/tests/indexing/test_datetime.py @@ -221,7 +221,7 @@ def test_loc_setitem_with_existing_dst(self): start = Timestamp("2017-10-29 00:00:00+0200", tz="Europe/Madrid") end = Timestamp("2017-10-29 03:00:00+0100", tz="Europe/Madrid") ts = Timestamp("2016-10-10 03:00:00", tz="Europe/Madrid") - idx = pd.date_range(start, end, closed="left", freq="H") + idx = date_range(start, end, closed="left", freq="H") result = DataFrame(index=idx, columns=["value"]) result.loc[ts, "value"] = 12 expected = DataFrame( diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 89315b16937b1..c0338cb03b2f4 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -488,10 +488,10 @@ def test_loc_setitem_consistency_slice_column_len(self): Region_1,Site_2,3977723089,A,5/20/2015 8:33,5/20/2015 9:09,Yes,No""" df = pd.read_csv(StringIO(data), header=[0, 1], index_col=[0, 1, 2]) - df.loc[:, ("Respondent", "StartDate")] = pd.to_datetime( + df.loc[:, ("Respondent", "StartDate")] = to_datetime( df.loc[:, ("Respondent", "StartDate")] ) - df.loc[:, ("Respondent", "EndDate")] = pd.to_datetime( + df.loc[:, ("Respondent", "EndDate")] = to_datetime( df.loc[:, ("Respondent", "EndDate")] ) df.loc[:, ("Respondent", "Duration")] = ( @@ -531,7 +531,7 @@ def test_loc_modify_datetime(self): {"date": [1485264372711, 1485265925110, 1540215845888, 1540282121025]} ) - df["date_dt"] = pd.to_datetime(df["date"], unit="ms", cache=True) + df["date_dt"] = to_datetime(df["date"], unit="ms", cache=True) df.loc[:, "date_dt_cp"] = df.loc[:, "date_dt"] df.loc[[2, 3], "date_dt_cp"] = df.loc[[2, 3], "date_dt"] @@ -547,7 +547,7 @@ def test_loc_modify_datetime(self): ) columns = ["date_dt", "date_dt_cp"] - expected[columns] = expected[columns].apply(pd.to_datetime) + expected[columns] = expected[columns].apply(to_datetime) tm.assert_frame_equal(df, expected) @@ -765,8 +765,8 @@ def test_loc_coercion(self): def test_setitem_new_key_tz(self): # GH#12862 should not raise on assigning the second value vals = [ - pd.to_datetime(42).tz_localize("UTC"), - pd.to_datetime(666).tz_localize("UTC"), + to_datetime(42).tz_localize("UTC"), + to_datetime(666).tz_localize("UTC"), ] expected = Series(vals, index=["foo", "bar"]) @@ -1873,7 +1873,7 @@ def test_loc_with_positional_slice_deprecation(): def test_loc_slice_disallows_positional(): # GH#16121, GH#24612, GH#31810 - dti = pd.date_range("2016-01-01", periods=3) + dti = date_range("2016-01-01", periods=3) df = DataFrame(np.random.random((3, 2)), index=dti) ser = df[0] @@ -1905,7 +1905,7 @@ def test_loc_datetimelike_mismatched_dtypes(): df = DataFrame( np.random.randn(5, 3), columns=["a", "b", "c"], - index=pd.date_range("2012", freq="H", periods=5), + index=date_range("2012", freq="H", periods=5), ) # create dataframe with non-unique DatetimeIndex df = df.iloc[[0, 2, 2, 3]].copy() diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index c0e4f01d3e5a5..15fa2967d9896 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -246,7 +246,7 @@ def test_repr_deprecation_negative_int(self): def test_repr_chop_threshold(self): df = DataFrame([[0.1, 0.5], [0.5, -0.1]]) - pd.reset_option("display.chop_threshold") # default None + reset_option("display.chop_threshold") # default None assert repr(df) == " 0 1\n0 0.1 0.5\n1 0.5 -0.1" with option_context("display.chop_threshold", 0.2): @@ -999,14 +999,14 @@ def test_truncate_with_different_dtypes(self): + [datetime.datetime(2012, 1, 3)] * 10 ) - with pd.option_context("display.max_rows", 8): + with option_context("display.max_rows", 8): result = str(s) assert "object" in result # 12045 df = DataFrame({"text": ["some words"] + [None] * 9}) - with pd.option_context("display.max_rows", 8, "display.max_columns", 3): + with option_context("display.max_rows", 8, "display.max_columns", 3): result = str(df) assert "None" in result assert "NaN" not in result @@ -1114,7 +1114,7 @@ def test_unicode_problem_decoding_as_ascii(self): def test_string_repr_encoding(self, datapath): filepath = datapath("io", "parser", "data", "unicode_series.csv") - df = pd.read_csv(filepath, header=None, encoding="latin1") + df = read_csv(filepath, header=None, encoding="latin1") repr(df) repr(df[1]) @@ -1545,7 +1545,7 @@ def test_to_string_float_index(self): def test_to_string_complex_float_formatting(self): # GH #25514, 25745 - with pd.option_context("display.precision", 5): + with option_context("display.precision", 5): df = DataFrame( { "x": [ @@ -1782,7 +1782,7 @@ def test_repr_html_mathjax(self): df = DataFrame([[1, 2], [3, 4]]) assert "tex2jax_ignore" not in df._repr_html_() - with pd.option_context("display.html.use_mathjax", False): + with option_context("display.html.use_mathjax", False): assert "tex2jax_ignore" in df._repr_html_() def test_repr_html_wide(self): @@ -2847,7 +2847,7 @@ def test_output_display_precision_trailing_zeroes(self): # Issue #20359: trimming zeros while there is no decimal point # Happens when display precision is set to zero - with pd.option_context("display.precision", 0): + with option_context("display.precision", 0): s = Series([840.0, 4200.0]) expected_output = "0 840\n1 4200\ndtype: float64" assert str(s) == expected_output @@ -2856,7 +2856,7 @@ def test_output_significant_digits(self): # Issue #9764 # In case default display precision changes: - with pd.option_context("display.precision", 6): + with option_context("display.precision", 6): # DataFrame example from issue #9764 d = DataFrame( { @@ -2927,7 +2927,7 @@ def test_output_significant_digits(self): def test_too_long(self): # GH 10451 - with pd.option_context("display.precision", 4): + with option_context("display.precision", 4): # need both a number > 1e6 and something that normally formats to # having length > display.precision + 6 df = DataFrame({"x": [12345.6789]}) diff --git a/pandas/tests/io/formats/test_printing.py b/pandas/tests/io/formats/test_printing.py index f0d5ef19c4468..fa8fc715a22f9 100644 --- a/pandas/tests/io/formats/test_printing.py +++ b/pandas/tests/io/formats/test_printing.py @@ -109,7 +109,7 @@ def test_ambiguous_width(self): adj = fmt.EastAsianTextAdjustment() assert adj.len("¡¡ab") == 4 - with cf.option_context("display.unicode.ambiguous_as_wide", True): + with pd.option_context("display.unicode.ambiguous_as_wide", True): adj = fmt.EastAsianTextAdjustment() assert adj.len("¡¡ab") == 6 diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index a88dec84bd693..732f572b9190f 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -758,7 +758,7 @@ def test_to_html_render_links(render_links, expected, datapath): def test_ignore_display_max_colwidth(method, expected, max_colwidth): # see gh-17004 df = DataFrame([lorem_ipsum]) - with pd.option_context("display.max_colwidth", max_colwidth): + with option_context("display.max_colwidth", max_colwidth): result = getattr(df, method)() expected = expected(max_colwidth) assert expected in result @@ -777,7 +777,7 @@ def test_to_html_invalid_classes_type(classes): def test_to_html_round_column_headers(): # GH 17280 df = DataFrame([1], columns=[0.55555]) - with pd.option_context("display.precision", 3): + with option_context("display.precision", 3): html = df.to_html(notebook=False) notebook = df.to_html(notebook=True) assert "0.55555" in html diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 851e64a3f2478..c7484bb64b21c 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -134,7 +134,7 @@ def test_frame_default_orient(self, float_frame): @pytest.mark.parametrize("numpy", [True, False]) def test_roundtrip_simple(self, orient, convert_axes, numpy, dtype, float_frame): data = float_frame.to_json(orient=orient) - result = pd.read_json( + result = read_json( data, orient=orient, convert_axes=convert_axes, numpy=numpy, dtype=dtype ) @@ -147,7 +147,7 @@ def test_roundtrip_simple(self, orient, convert_axes, numpy, dtype, float_frame) @pytest.mark.parametrize("numpy", [True, False]) def test_roundtrip_intframe(self, orient, convert_axes, numpy, dtype, int_frame): data = int_frame.to_json(orient=orient) - result = pd.read_json( + result = read_json( data, orient=orient, convert_axes=convert_axes, numpy=numpy, dtype=dtype ) expected = int_frame @@ -178,7 +178,7 @@ def test_roundtrip_str_axes(self, orient, convert_axes, numpy, dtype): pytest.xfail("Can't decode directly to array") data = df.to_json(orient=orient) - result = pd.read_json( + result = read_json( data, orient=orient, convert_axes=convert_axes, numpy=numpy, dtype=dtype ) @@ -211,9 +211,7 @@ def test_roundtrip_categorical(self, orient, convert_axes, numpy): if numpy and orient in ("records", "values"): pytest.xfail(f"Orient {orient} is broken with numpy=True") - result = pd.read_json( - data, orient=orient, convert_axes=convert_axes, numpy=numpy - ) + result = read_json(data, orient=orient, convert_axes=convert_axes, numpy=numpy) expected = self.categorical.copy() expected.index = expected.index.astype(str) # Categorical not preserved @@ -228,9 +226,7 @@ def test_roundtrip_categorical(self, orient, convert_axes, numpy): @pytest.mark.parametrize("numpy", [True, False]) def test_roundtrip_empty(self, orient, convert_axes, numpy, empty_frame): data = empty_frame.to_json(orient=orient) - result = pd.read_json( - data, orient=orient, convert_axes=convert_axes, numpy=numpy - ) + result = read_json(data, orient=orient, convert_axes=convert_axes, numpy=numpy) expected = empty_frame.copy() # TODO: both conditions below are probably bugs @@ -247,9 +243,7 @@ def test_roundtrip_empty(self, orient, convert_axes, numpy, empty_frame): def test_roundtrip_timestamp(self, orient, convert_axes, numpy, datetime_frame): # TODO: improve coverage with date_format parameter data = datetime_frame.to_json(orient=orient) - result = pd.read_json( - data, orient=orient, convert_axes=convert_axes, numpy=numpy - ) + result = read_json(data, orient=orient, convert_axes=convert_axes, numpy=numpy) expected = datetime_frame.copy() if not convert_axes: # one off for ts handling @@ -279,9 +273,7 @@ def test_roundtrip_mixed(self, orient, convert_axes, numpy): df = DataFrame(data=values, index=index) data = df.to_json(orient=orient) - result = pd.read_json( - data, orient=orient, convert_axes=convert_axes, numpy=numpy - ) + result = read_json(data, orient=orient, convert_axes=convert_axes, numpy=numpy) expected = df.copy() expected = expected.assign(**expected.select_dtypes("number").astype(np.int64)) @@ -461,12 +453,12 @@ def test_v12_compat(self, datapath): dirpath = datapath("io", "json", "data") v12_json = os.path.join(dirpath, "tsframe_v012.json") - df_unser = pd.read_json(v12_json) + df_unser = read_json(v12_json) tm.assert_frame_equal(df, df_unser) df_iso = df.drop(["modified"], axis=1) v12_iso_json = os.path.join(dirpath, "tsframe_iso_v012.json") - df_unser_iso = pd.read_json(v12_iso_json) + df_unser_iso = read_json(v12_iso_json) tm.assert_frame_equal(df_iso, df_unser_iso) def test_blocks_compat_GH9037(self): @@ -555,7 +547,7 @@ def test_blocks_compat_GH9037(self): # JSON deserialisation always creates unicode strings df_mixed.columns = df_mixed.columns.astype("unicode") - df_roundtrip = pd.read_json(df_mixed.to_json(orient="split"), orient="split") + df_roundtrip = read_json(df_mixed.to_json(orient="split"), orient="split") tm.assert_frame_equal( df_mixed, df_roundtrip, @@ -628,7 +620,7 @@ def test_series_default_orient(self, string_series): @pytest.mark.parametrize("numpy", [True, False]) def test_series_roundtrip_simple(self, orient, numpy, string_series): data = string_series.to_json(orient=orient) - result = pd.read_json(data, typ="series", orient=orient, numpy=numpy) + result = read_json(data, typ="series", orient=orient, numpy=numpy) expected = string_series if orient in ("values", "records"): @@ -642,9 +634,7 @@ def test_series_roundtrip_simple(self, orient, numpy, string_series): @pytest.mark.parametrize("numpy", [True, False]) def test_series_roundtrip_object(self, orient, numpy, dtype, object_series): data = object_series.to_json(orient=orient) - result = pd.read_json( - data, typ="series", orient=orient, numpy=numpy, dtype=dtype - ) + result = read_json(data, typ="series", orient=orient, numpy=numpy, dtype=dtype) expected = object_series if orient in ("values", "records"): @@ -657,7 +647,7 @@ def test_series_roundtrip_object(self, orient, numpy, dtype, object_series): @pytest.mark.parametrize("numpy", [True, False]) def test_series_roundtrip_empty(self, orient, numpy, empty_series): data = empty_series.to_json(orient=orient) - result = pd.read_json(data, typ="series", orient=orient, numpy=numpy) + result = read_json(data, typ="series", orient=orient, numpy=numpy) expected = empty_series if orient in ("values", "records"): @@ -670,7 +660,7 @@ def test_series_roundtrip_empty(self, orient, numpy, empty_series): @pytest.mark.parametrize("numpy", [True, False]) def test_series_roundtrip_timeseries(self, orient, numpy, datetime_series): data = datetime_series.to_json(orient=orient) - result = pd.read_json(data, typ="series", orient=orient, numpy=numpy) + result = read_json(data, typ="series", orient=orient, numpy=numpy) expected = datetime_series if orient in ("values", "records"): @@ -685,7 +675,7 @@ def test_series_roundtrip_timeseries(self, orient, numpy, datetime_series): def test_series_roundtrip_numeric(self, orient, numpy, dtype): s = Series(range(6), index=["a", "b", "c", "d", "e", "f"]) data = s.to_json(orient=orient) - result = pd.read_json(data, typ="series", orient=orient, numpy=numpy) + result = read_json(data, typ="series", orient=orient, numpy=numpy) expected = s.copy() if orient in ("values", "records"): @@ -721,7 +711,7 @@ def test_series_with_dtype(self): def test_series_with_dtype_datetime(self, dtype, expected): s = Series(["2000-01-01"], dtype="datetime64[ns]") data = s.to_json() - result = pd.read_json(data, typ="series", dtype=dtype) + result = read_json(data, typ="series", dtype=dtype) tm.assert_series_equal(result, expected) def test_frame_from_json_precise_float(self): @@ -975,7 +965,7 @@ def test_round_trip_exception_(self): csv = "https://raw.github.com/hayd/lahman2012/master/csvs/Teams.csv" df = pd.read_csv(csv) s = df.to_json() - result = pd.read_json(s) + result = read_json(s) tm.assert_frame_equal(result.reindex(index=df.index, columns=df.columns), df) @tm.network @@ -999,17 +989,17 @@ def test_timedelta(self): s = Series([timedelta(23), timedelta(seconds=5)]) assert s.dtype == "timedelta64[ns]" - result = pd.read_json(s.to_json(), typ="series").apply(converter) + result = read_json(s.to_json(), typ="series").apply(converter) tm.assert_series_equal(result, s) s = Series([timedelta(23), timedelta(seconds=5)], index=pd.Index([0, 1])) assert s.dtype == "timedelta64[ns]" - result = pd.read_json(s.to_json(), typ="series").apply(converter) + result = read_json(s.to_json(), typ="series").apply(converter) tm.assert_series_equal(result, s) frame = DataFrame([timedelta(23), timedelta(seconds=5)]) assert frame[0].dtype == "timedelta64[ns]" - tm.assert_frame_equal(frame, pd.read_json(frame.to_json()).apply(converter)) + tm.assert_frame_equal(frame, read_json(frame.to_json()).apply(converter)) frame = DataFrame( { @@ -1019,7 +1009,7 @@ def test_timedelta(self): } ) - result = pd.read_json(frame.to_json(date_unit="ns")) + result = read_json(frame.to_json(date_unit="ns")) result["a"] = pd.to_timedelta(result.a, unit="ns") result["c"] = pd.to_datetime(result.c) tm.assert_frame_equal(frame, result) @@ -1030,7 +1020,7 @@ def test_mixed_timedelta_datetime(self): expected = DataFrame( {"a": [pd.Timedelta(frame.a[0]).value, Timestamp(frame.a[1]).value]} ) - result = pd.read_json(frame.to_json(date_unit="ns"), dtype={"a": "int64"}) + result = read_json(frame.to_json(date_unit="ns"), dtype={"a": "int64"}) tm.assert_frame_equal(result, expected, check_index_type=False) @pytest.mark.parametrize("as_object", [True, False]) @@ -1060,7 +1050,7 @@ def test_default_handler(self): value = object() frame = DataFrame({"a": [7, value]}) expected = DataFrame({"a": [7, str(value)]}) - result = pd.read_json(frame.to_json(default_handler=str)) + result = read_json(frame.to_json(default_handler=str)) tm.assert_frame_equal(expected, result, check_index_type=False) def test_default_handler_indirect(self): @@ -1293,14 +1283,14 @@ def test_to_jsonl(self): result = df.to_json(orient="records", lines=True) expected = '{"a":"foo}","b":"bar"}\n{"a":"foo\\"","b":"bar"}\n' assert result == expected - tm.assert_frame_equal(pd.read_json(result, lines=True), df) + tm.assert_frame_equal(read_json(result, lines=True), df) # GH15096: escaped characters in columns and data df = DataFrame([["foo\\", "bar"], ['foo"', "bar"]], columns=["a\\", "b"]) result = df.to_json(orient="records", lines=True) expected = '{"a\\\\":"foo\\\\","b":"bar"}\n{"a\\\\":"foo\\"","b":"bar"}\n' assert result == expected - tm.assert_frame_equal(pd.read_json(result, lines=True), df) + tm.assert_frame_equal(read_json(result, lines=True), df) # TODO: there is a near-identical test for pytables; can we share? def test_latin_encoding(self): @@ -1356,14 +1346,14 @@ def test_from_json_to_json_table_index_and_columns(self, index, columns): # GH25433 GH25435 expected = DataFrame([[1, 2], [3, 4]], index=index, columns=columns) dfjson = expected.to_json(orient="table") - result = pd.read_json(dfjson, orient="table") + result = read_json(dfjson, orient="table") tm.assert_frame_equal(result, expected) def test_from_json_to_json_table_dtypes(self): # GH21345 expected = DataFrame({"a": [1, 2], "b": [3.0, 4.0], "c": ["5", "6"]}) dfjson = expected.to_json(orient="table") - result = pd.read_json(dfjson, orient="table") + result = read_json(dfjson, orient="table") tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("dtype", [True, {"b": int, "c": int}]) @@ -1373,7 +1363,7 @@ def test_read_json_table_dtype_raises(self, dtype): dfjson = df.to_json(orient="table") msg = "cannot pass both dtype and orient='table'" with pytest.raises(ValueError, match=msg): - pd.read_json(dfjson, orient="table", dtype=dtype) + read_json(dfjson, orient="table", dtype=dtype) def test_read_json_table_convert_axes_raises(self): # GH25433 GH25435 @@ -1381,7 +1371,7 @@ def test_read_json_table_convert_axes_raises(self): dfjson = df.to_json(orient="table") msg = "cannot pass both convert_axes and orient='table'" with pytest.raises(ValueError, match=msg): - pd.read_json(dfjson, orient="table", convert_axes=True) + read_json(dfjson, orient="table", convert_axes=True) @pytest.mark.parametrize( "data, expected", @@ -1655,7 +1645,7 @@ def test_json_negative_indent_raises(self): def test_emca_262_nan_inf_support(self): # GH 12213 data = '["a", NaN, "NaN", Infinity, "Infinity", -Infinity, "-Infinity"]' - result = pd.read_json(data) + result = read_json(data) expected = DataFrame( ["a", np.nan, "NaN", np.inf, "Infinity", -np.inf, "-Infinity"] ) diff --git a/pandas/tests/io/json/test_readlines.py b/pandas/tests/io/json/test_readlines.py index 099d99507e136..c1cc933dabcd5 100644 --- a/pandas/tests/io/json/test_readlines.py +++ b/pandas/tests/io/json/test_readlines.py @@ -86,7 +86,7 @@ def test_readjson_chunks(lines_json_df, chunksize): def test_readjson_chunksize_requires_lines(lines_json_df): msg = "chunksize can only be passed if lines=True" with pytest.raises(ValueError, match=msg): - with pd.read_json(StringIO(lines_json_df), lines=False, chunksize=2) as _: + with read_json(StringIO(lines_json_df), lines=False, chunksize=2) as _: pass @@ -95,10 +95,10 @@ def test_readjson_chunks_series(): s = pd.Series({"A": 1, "B": 2}) strio = StringIO(s.to_json(lines=True, orient="records")) - unchunked = pd.read_json(strio, lines=True, typ="Series") + unchunked = read_json(strio, lines=True, typ="Series") strio = StringIO(s.to_json(lines=True, orient="records")) - with pd.read_json(strio, lines=True, typ="Series", chunksize=1) as reader: + with read_json(strio, lines=True, typ="Series", chunksize=1) as reader: chunked = pd.concat(reader) tm.assert_series_equal(chunked, unchunked) @@ -107,7 +107,7 @@ def test_readjson_chunks_series(): def test_readjson_each_chunk(lines_json_df): # Other tests check that the final result of read_json(chunksize=True) # is correct. This checks the intermediate chunks. - with pd.read_json(StringIO(lines_json_df), lines=True, chunksize=2) as reader: + with read_json(StringIO(lines_json_df), lines=True, chunksize=2) as reader: chunks = list(reader) assert chunks[0].shape == (2, 2) assert chunks[1].shape == (1, 2) @@ -117,9 +117,9 @@ def test_readjson_chunks_from_file(): with tm.ensure_clean("test.json") as path: df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) df.to_json(path, lines=True, orient="records") - with pd.read_json(path, lines=True, chunksize=1) as reader: + with read_json(path, lines=True, chunksize=1) as reader: chunked = pd.concat(reader) - unchunked = pd.read_json(path, lines=True) + unchunked = read_json(path, lines=True) tm.assert_frame_equal(unchunked, chunked) @@ -157,9 +157,7 @@ def test_readjson_invalid_chunksize(lines_json_df, chunksize): msg = r"'chunksize' must be an integer >=1" with pytest.raises(ValueError, match=msg): - with pd.read_json( - StringIO(lines_json_df), lines=True, chunksize=chunksize - ) as _: + with read_json(StringIO(lines_json_df), lines=True, chunksize=chunksize) as _: pass @@ -182,7 +180,7 @@ def test_readjson_chunks_multiple_empty_lines(chunksize): {"A":3,"B":6} """ orig = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) - test = pd.read_json(j, lines=True, chunksize=chunksize) + test = read_json(j, lines=True, chunksize=chunksize) if chunksize is not None: with test: test = pd.concat(test) @@ -208,7 +206,7 @@ def test_readjson_nrows(nrows): {"a": 3, "b": 4} {"a": 5, "b": 6} {"a": 7, "b": 8}""" - result = pd.read_json(jsonl, lines=True, nrows=nrows) + result = read_json(jsonl, lines=True, nrows=nrows) expected = DataFrame({"a": [1, 3, 5, 7], "b": [2, 4, 6, 8]}).iloc[:nrows] tm.assert_frame_equal(result, expected) @@ -236,7 +234,7 @@ def test_readjson_nrows_requires_lines(): {"a": 7, "b": 8}""" msg = "nrows can only be passed if lines=True" with pytest.raises(ValueError, match=msg): - pd.read_json(jsonl, lines=False, nrows=2) + read_json(jsonl, lines=False, nrows=2) def test_readjson_lines_chunks_fileurl(datapath): @@ -249,7 +247,7 @@ def test_readjson_lines_chunks_fileurl(datapath): ] os_path = datapath("io", "json", "data", "line_delimited.json") file_url = Path(os_path).as_uri() - with pd.read_json(file_url, lines=True, chunksize=1) as url_reader: + with read_json(file_url, lines=True, chunksize=1) as url_reader: for index, chuck in enumerate(url_reader): tm.assert_frame_equal(chuck, df_list_expected[index]) @@ -278,5 +276,5 @@ def __iter__(self): return iter(self.stringio) reader = MyReader(jsonl) - assert len(list(pd.read_json(reader, lines=True, chunksize=100))) > 1 + assert len(list(read_json(reader, lines=True, chunksize=100))) > 1 assert reader.read_count > 10 diff --git a/pandas/tests/io/parser/test_parse_dates.py b/pandas/tests/io/parser/test_parse_dates.py index c0b29d5019675..093e9fadc1223 100644 --- a/pandas/tests/io/parser/test_parse_dates.py +++ b/pandas/tests/io/parser/test_parse_dates.py @@ -1474,7 +1474,7 @@ def test_parse_timezone(all_parsers): dti = DatetimeIndex( list( - pd.date_range( + date_range( start="2018-01-04 09:01:00", end="2018-01-04 09:05:00", freq="1min", diff --git a/pandas/tests/io/parser/test_read_fwf.py b/pandas/tests/io/parser/test_read_fwf.py index 47dc543c61bd0..10d95fea61f15 100644 --- a/pandas/tests/io/parser/test_read_fwf.py +++ b/pandas/tests/io/parser/test_read_fwf.py @@ -11,7 +11,6 @@ import numpy as np import pytest -import pandas as pd from pandas import DataFrame, DatetimeIndex import pandas._testing as tm @@ -676,7 +675,7 @@ def test_binary_mode(): with tm.ensure_clean() as path: Path(path).write_text(data) with open(path, "rb") as file: - df = pd.read_fwf(file) + df = read_fwf(file) file.seek(0) tm.assert_frame_equal(df, df_reference) @@ -690,7 +689,7 @@ def test_encoding_mmap(memory_map): """ encoding = "iso8859_1" data = BytesIO(" 1 A Ä 2\n".encode(encoding)) - df = pd.read_fwf( + df = read_fwf( data, header=None, widths=[2, 2, 2, 2], diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py index 3f4c21389daed..51eb19dcbcefe 100644 --- a/pandas/tests/io/pytables/test_store.py +++ b/pandas/tests/io/pytables/test_store.py @@ -820,7 +820,7 @@ def test_complibs_default_settings(self, setup_path): # default value with ensure_clean_path(setup_path) as tmpfile: df.to_hdf(tmpfile, "df", complevel=9) - result = pd.read_hdf(tmpfile, "df") + result = read_hdf(tmpfile, "df") tm.assert_frame_equal(result, df) with tables.open_file(tmpfile, mode="r") as h5file: @@ -831,7 +831,7 @@ def test_complibs_default_settings(self, setup_path): # Set complib and check to see if compression is disabled with ensure_clean_path(setup_path) as tmpfile: df.to_hdf(tmpfile, "df", complib="zlib") - result = pd.read_hdf(tmpfile, "df") + result = read_hdf(tmpfile, "df") tm.assert_frame_equal(result, df) with tables.open_file(tmpfile, mode="r") as h5file: @@ -842,7 +842,7 @@ def test_complibs_default_settings(self, setup_path): # Check if not setting complib or complevel results in no compression with ensure_clean_path(setup_path) as tmpfile: df.to_hdf(tmpfile, "df") - result = pd.read_hdf(tmpfile, "df") + result = read_hdf(tmpfile, "df") tm.assert_frame_equal(result, df) with tables.open_file(tmpfile, mode="r") as h5file: @@ -887,7 +887,7 @@ def test_complibs(self, setup_path): # Write and read file to see if data is consistent df.to_hdf(tmpfile, gname, complib=lib, complevel=lvl) - result = pd.read_hdf(tmpfile, gname) + result = read_hdf(tmpfile, gname) tm.assert_frame_equal(result, df) # Open file and check metadata @@ -1295,7 +1295,7 @@ def test_read_missing_key_close_store(self, setup_path): df.to_hdf(path, "k1") with pytest.raises(KeyError, match="'No object named k2 in the file'"): - pd.read_hdf(path, "k2") + read_hdf(path, "k2") # smoke test to test that file is properly closed after # read with KeyError before another write @@ -1310,11 +1310,11 @@ def test_read_missing_key_opened_store(self, setup_path): with HDFStore(path, "r") as store: with pytest.raises(KeyError, match="'No object named k2 in the file'"): - pd.read_hdf(store, "k2") + read_hdf(store, "k2") # Test that the file is still open after a KeyError and that we can # still read from it. - pd.read_hdf(store, "k1") + read_hdf(store, "k1") def test_append_frame_column_oriented(self, setup_path): with ensure_clean_store(setup_path) as store: @@ -1455,14 +1455,12 @@ def check_col(key, name, size): # just make sure there is a longer string: df2 = df.copy().reset_index().assign(C="longer").set_index("C") store.append("ss3", df2) - tm.assert_frame_equal(store.select("ss3"), pd.concat([df, df2])) + tm.assert_frame_equal(store.select("ss3"), concat([df, df2])) # same as above, with a Series store.put("ss4", df["B"], format="table", min_itemsize={"index": 6}) store.append("ss4", df2["B"]) - tm.assert_series_equal( - store.select("ss4"), pd.concat([df["B"], df2["B"]]) - ) + tm.assert_series_equal(store.select("ss4"), concat([df["B"], df2["B"]])) # with nans _maybe_remove(store, "df") @@ -1540,14 +1538,12 @@ def test_to_hdf_with_min_itemsize(self, setup_path): # just make sure there is a longer string: df2 = df.copy().reset_index().assign(C="longer").set_index("C") df2.to_hdf(path, "ss3", append=True, format="table") - tm.assert_frame_equal(pd.read_hdf(path, "ss3"), pd.concat([df, df2])) + tm.assert_frame_equal(read_hdf(path, "ss3"), concat([df, df2])) # same as above, with a Series df["B"].to_hdf(path, "ss4", format="table", min_itemsize={"index": 6}) df2["B"].to_hdf(path, "ss4", append=True, format="table") - tm.assert_series_equal( - pd.read_hdf(path, "ss4"), pd.concat([df["B"], df2["B"]]) - ) + tm.assert_series_equal(read_hdf(path, "ss4"), concat([df["B"], df2["B"]])) @pytest.mark.parametrize("format", ["fixed", "table"]) def test_to_hdf_errors(self, format, setup_path): @@ -1558,7 +1554,7 @@ def test_to_hdf_errors(self, format, setup_path): # GH 20835 ser.to_hdf(path, "table", format=format, errors="surrogatepass") - result = pd.read_hdf(path, "table", errors="surrogatepass") + result = read_hdf(path, "table", errors="surrogatepass") tm.assert_series_equal(result, ser) def test_append_with_data_columns(self, setup_path): @@ -2410,11 +2406,8 @@ def test_invalid_terms(self, setup_path): def test_same_name_scoping(self, setup_path): with ensure_clean_store(setup_path) as store: - - import pandas as pd - df = DataFrame( - np.random.randn(20, 2), index=pd.date_range("20130101", periods=20) + np.random.randn(20, 2), index=date_range("20130101", periods=20) ) store.put("df", df, format="table") expected = df[df.index > Timestamp("20130105")] @@ -3952,7 +3945,7 @@ def test_start_stop_fixed(self, setup_path): # fixed, GH 8287 df = DataFrame( {"A": np.random.rand(20), "B": np.random.rand(20)}, - index=pd.date_range("20130101", periods=20), + index=date_range("20130101", periods=20), ) store.put("df", df) @@ -4006,7 +3999,7 @@ def test_path_pathlib(self, setup_path): df = tm.makeDataFrame() result = tm.round_trip_pathlib( - lambda p: df.to_hdf(p, "df"), lambda p: pd.read_hdf(p, "df") + lambda p: df.to_hdf(p, "df"), lambda p: read_hdf(p, "df") ) tm.assert_frame_equal(df, result) @@ -4036,7 +4029,7 @@ def writer(path): def reader(path): with HDFStore(path) as store: - return pd.read_hdf(store, "df") + return read_hdf(store, "df") result = tm.round_trip_pathlib(writer, reader) tm.assert_frame_equal(df, result) @@ -4044,7 +4037,7 @@ def reader(path): def test_pickle_path_localpath(self, setup_path): df = tm.makeDataFrame() result = tm.round_trip_pathlib( - lambda p: df.to_hdf(p, "df"), lambda p: pd.read_hdf(p, "df") + lambda p: df.to_hdf(p, "df"), lambda p: read_hdf(p, "df") ) tm.assert_frame_equal(df, result) @@ -4057,7 +4050,7 @@ def writer(path): def reader(path): with HDFStore(path) as store: - return pd.read_hdf(store, "df") + return read_hdf(store, "df") result = tm.round_trip_localpath(writer, reader) tm.assert_frame_equal(df, result) @@ -4729,7 +4722,7 @@ def test_to_hdf_with_object_column_names(self, setup_path): with ensure_clean_path(setup_path) as path: with catch_warnings(record=True): df.to_hdf(path, "df", format="table", data_columns=True) - result = pd.read_hdf(path, "df", where=f"index = [{df.index[0]}]") + result = read_hdf(path, "df", where=f"index = [{df.index[0]}]") assert len(result) def test_read_hdf_open_store(self, setup_path): @@ -4942,7 +4935,7 @@ def test_read_hdf_series_mode_r(self, format, setup_path): series = tm.makeFloatSeries() with ensure_clean_path(setup_path) as path: series.to_hdf(path, key="data", format=format) - result = pd.read_hdf(path, key="data", mode="r") + result = read_hdf(path, key="data", mode="r") tm.assert_series_equal(result, series) def test_fspath(self): @@ -4987,7 +4980,7 @@ def test_select_empty_where(self, where): with ensure_clean_path("empty_where.h5") as path: with HDFStore(path) as store: store.put("df", df, "t") - result = pd.read_hdf(store, "df", where=where) + result = read_hdf(store, "df", where=where) tm.assert_frame_equal(result, df) @pytest.mark.parametrize( @@ -5014,7 +5007,7 @@ def test_unsuppored_hdf_file_error(self, datapath): ) with pytest.raises(ValueError, match=message): - pd.read_hdf(data_path) + read_hdf(data_path) @pytest.mark.parametrize("bad_version", [(1, 2), (1,), [], "12", "123"]) diff --git a/pandas/tests/io/pytables/test_timezones.py b/pandas/tests/io/pytables/test_timezones.py index 9ee44b58d6ced..7bcf2ed6a99df 100644 --- a/pandas/tests/io/pytables/test_timezones.py +++ b/pandas/tests/io/pytables/test_timezones.py @@ -415,7 +415,7 @@ def test_legacy_datetimetz_object(datapath, setup_path): def test_dst_transitions(setup_path): # make sure we are not failing on transitions with ensure_clean_store(setup_path) as store: - times = pd.date_range( + times = date_range( "2013-10-26 23:00", "2013-10-27 01:00", tz="Europe/London", @@ -435,7 +435,7 @@ def test_dst_transitions(setup_path): def test_read_with_where_tz_aware_index(setup_path): # GH 11926 periods = 10 - dts = pd.date_range("20151201", periods=periods, freq="D", tz="UTC") + dts = date_range("20151201", periods=periods, freq="D", tz="UTC") mi = pd.MultiIndex.from_arrays([dts, range(periods)], names=["DATE", "NO"]) expected = DataFrame({"MYCOL": 0}, index=mi) diff --git a/pandas/tests/io/test_clipboard.py b/pandas/tests/io/test_clipboard.py index 440c370857eef..2c2787765d47d 100644 --- a/pandas/tests/io/test_clipboard.py +++ b/pandas/tests/io/test_clipboard.py @@ -3,7 +3,6 @@ import numpy as np import pytest -import pandas as pd from pandas import DataFrame, get_option, read_clipboard import pandas._testing as tm @@ -209,7 +208,7 @@ def test_read_clipboard_infer_excel(self, request, mock_clipboard): """.strip() ) mock_clipboard[request.node.name] = text - df = pd.read_clipboard(**clip_kwargs) + df = read_clipboard(**clip_kwargs) # excel data is parsed correctly assert df.iloc[1][1] == "Harry Carney" @@ -223,7 +222,7 @@ def test_read_clipboard_infer_excel(self, request, mock_clipboard): """.strip() ) mock_clipboard[request.node.name] = text - res = pd.read_clipboard(**clip_kwargs) + res = read_clipboard(**clip_kwargs) text = dedent( """ @@ -233,7 +232,7 @@ def test_read_clipboard_infer_excel(self, request, mock_clipboard): """.strip() ) mock_clipboard[request.node.name] = text - exp = pd.read_clipboard(**clip_kwargs) + exp = read_clipboard(**clip_kwargs) tm.assert_frame_equal(res, exp) @@ -243,7 +242,7 @@ def test_invalid_encoding(self, df): with pytest.raises(ValueError, match=msg): df.to_clipboard(encoding="ascii") with pytest.raises(NotImplementedError, match=msg): - pd.read_clipboard(encoding="ascii") + read_clipboard(encoding="ascii") @pytest.mark.parametrize("enc", ["UTF-8", "utf-8", "utf8"]) def test_round_trip_valid_encodings(self, enc, df): diff --git a/pandas/tests/io/test_feather.py b/pandas/tests/io/test_feather.py index fc83026f67930..ab0b3b08a11e8 100644 --- a/pandas/tests/io/test_feather.py +++ b/pandas/tests/io/test_feather.py @@ -177,12 +177,12 @@ def test_write_with_index(self): def test_path_pathlib(self): df = tm.makeDataFrame().reset_index() - result = tm.round_trip_pathlib(df.to_feather, pd.read_feather) + result = tm.round_trip_pathlib(df.to_feather, read_feather) tm.assert_frame_equal(df, result) def test_path_localpath(self): df = tm.makeDataFrame().reset_index() - result = tm.round_trip_localpath(df.to_feather, pd.read_feather) + result = tm.round_trip_localpath(df.to_feather, read_feather) tm.assert_frame_equal(df, result) @td.skip_if_no("pyarrow", min_version="0.16.1.dev") @@ -198,6 +198,6 @@ def test_http_path(self, feather_file): "https://raw.githubusercontent.com/pandas-dev/pandas/master/" "pandas/tests/io/data/feather/feather-0_3_1.feather" ) - expected = pd.read_feather(feather_file) - res = pd.read_feather(url) + expected = read_feather(feather_file) + res = read_feather(url) tm.assert_frame_equal(expected, res) diff --git a/pandas/tests/io/test_parquet.py b/pandas/tests/io/test_parquet.py index 822b412916726..f775212a69c63 100644 --- a/pandas/tests/io/test_parquet.py +++ b/pandas/tests/io/test_parquet.py @@ -354,7 +354,7 @@ def test_parquet_read_from_url(self, df_compat, engine): "https://raw.githubusercontent.com/pandas-dev/pandas/" "master/pandas/tests/io/data/parquet/simple.parquet" ) - df = pd.read_parquet(url) + df = read_parquet(url) tm.assert_frame_equal(df, df_compat) @@ -600,7 +600,7 @@ def test_to_bytes_without_path_or_buf_provided(self, pa, df_full): assert isinstance(buf_bytes, bytes) buf_stream = BytesIO(buf_bytes) - res = pd.read_parquet(buf_stream) + res = read_parquet(buf_stream) tm.assert_frame_equal(df_full, res) @@ -735,7 +735,7 @@ def test_s3_roundtrip_for_dir( def test_read_file_like_obj_support(self, df_compat): buffer = BytesIO() df_compat.to_parquet(buffer) - df_from_buf = pd.read_parquet(buffer) + df_from_buf = read_parquet(buffer) tm.assert_frame_equal(df_compat, df_from_buf) @td.skip_if_no("pyarrow") @@ -743,7 +743,7 @@ def test_expand_user(self, df_compat, monkeypatch): monkeypatch.setenv("HOME", "TestingUser") monkeypatch.setenv("USERPROFILE", "TestingUser") with pytest.raises(OSError, match=r".*TestingUser.*"): - pd.read_parquet("~/file.parquet") + read_parquet("~/file.parquet") with pytest.raises(OSError, match=r".*TestingUser.*"): df_compat.to_parquet("~/file.parquet") diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 52869f3f2fd42..883f1ba20575e 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -1576,7 +1576,7 @@ def check(col): ) # GH11216 - df = pd.read_sql_query("select * from types_test_data", self.conn) + df = read_sql_query("select * from types_test_data", self.conn) if not hasattr(df, "DateColWithTz"): pytest.skip("no column with datetime with time zone") @@ -1586,7 +1586,7 @@ def check(col): col = df.DateColWithTz assert is_datetime64tz_dtype(col.dtype) - df = pd.read_sql_query( + df = read_sql_query( "select * from types_test_data", self.conn, parse_dates=["DateColWithTz"] ) if not hasattr(df, "DateColWithTz"): @@ -1596,11 +1596,9 @@ def check(col): assert str(col.dt.tz) == "UTC" check(df.DateColWithTz) - df = pd.concat( + df = concat( list( - pd.read_sql_query( - "select * from types_test_data", self.conn, chunksize=1 - ) + read_sql_query("select * from types_test_data", self.conn, chunksize=1) ), ignore_index=True, ) @@ -2835,7 +2833,7 @@ def test_chunksize_read_type(self): sql.to_sql(frame, name="test", con=self.conn) query = "select * from test" chunksize = 5 - chunk_gen = pd.read_sql_query( + chunk_gen = read_sql_query( sql=query, con=self.conn, chunksize=chunksize, index_col="index" ) chunk_df = next(chunk_gen) diff --git a/pandas/tests/io/test_stata.py b/pandas/tests/io/test_stata.py index 24944281419c3..fd6e8c4f93bbd 100644 --- a/pandas/tests/io/test_stata.py +++ b/pandas/tests/io/test_stata.py @@ -634,7 +634,7 @@ def test_105(self): # Data obtained from: # http://go.worldbank.org/ZXY29PVJ21 dpath = os.path.join(self.dirpath, "S4_EDUC1.dta") - df = pd.read_stata(dpath) + df = read_stata(dpath) df0 = [[1, 1, 3, -2], [2, 1, 2, -2], [4, 1, 1, -2]] df0 = DataFrame(df0) df0.columns = ["clustnum", "pri_schl", "psch_num", "psch_dis"] @@ -1532,7 +1532,7 @@ def test_value_labels_iterator(self, write_index): with tm.ensure_clean() as path: df.to_stata(path, write_index=write_index) - with pd.read_stata(path, iterator=True) as dta_iter: + with read_stata(path, iterator=True) as dta_iter: value_labels = dta_iter.value_labels() assert value_labels == {"A": {0: "A", 1: "B", 2: "C", 3: "E"}} @@ -1542,7 +1542,7 @@ def test_set_index(self): df.index.name = "index" with tm.ensure_clean() as path: df.to_stata(path) - reread = pd.read_stata(path, index_col="index") + reread = read_stata(path, index_col="index") tm.assert_frame_equal(df, reread) @pytest.mark.parametrize( @@ -1682,7 +1682,7 @@ def test_nonfile_writing(self, version): bio.seek(0) with open(path, "wb") as dta: dta.write(bio.read()) - reread = pd.read_stata(path, index_col="index") + reread = read_stata(path, index_col="index") tm.assert_frame_equal(df, reread) def test_gzip_writing(self): @@ -1693,7 +1693,7 @@ def test_gzip_writing(self): with gzip.GzipFile(path, "wb") as gz: df.to_stata(gz, version=114) with gzip.GzipFile(path, "rb") as gz: - reread = pd.read_stata(gz, index_col="index") + reread = read_stata(gz, index_col="index") tm.assert_frame_equal(df, reread) def test_unicode_dta_118(self): @@ -1864,8 +1864,8 @@ def test_backward_compat(version, datapath): data_base = datapath("io", "data", "stata") ref = os.path.join(data_base, "stata-compat-118.dta") old = os.path.join(data_base, f"stata-compat-{version}.dta") - expected = pd.read_stata(ref) - old_dta = pd.read_stata(old) + expected = read_stata(ref) + old_dta = read_stata(old) tm.assert_frame_equal(old_dta, expected, check_dtype=False) @@ -1975,7 +1975,7 @@ def test_iterator_value_labels(): with tm.ensure_clean() as path: df.to_stata(path, write_index=False) expected = pd.Index(["a_label", "b_label", "c_label"], dtype="object") - with pd.read_stata(path, chunksize=100) as reader: + with read_stata(path, chunksize=100) as reader: for j, chunk in enumerate(reader): for i in range(2): tm.assert_index_equal(chunk.dtypes[i].categories, expected) diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index a6aa6c02d1a79..bd13699254c21 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -669,7 +669,7 @@ def test_plot_scatter(self): def test_raise_error_on_datetime_time_data(self): # GH 8113, datetime.time type is not supported by matplotlib in scatter df = DataFrame(np.random.randn(10), columns=["a"]) - df["dtime"] = pd.date_range(start="2014-01-01", freq="h", periods=10).time + df["dtime"] = date_range(start="2014-01-01", freq="h", periods=10).time msg = "must be a string or a number, not 'datetime.time'" with pytest.raises(TypeError, match=msg): @@ -677,7 +677,7 @@ def test_raise_error_on_datetime_time_data(self): def test_scatterplot_datetime_data(self): # GH 30391 - dates = pd.date_range(start=date(2019, 1, 1), periods=12, freq="W") + dates = date_range(start=date(2019, 1, 1), periods=12, freq="W") vals = np.random.normal(0, 1, len(dates)) df = DataFrame({"dates": dates, "vals": vals}) diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index cb64b2423696f..ff112c9bbe642 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -89,7 +89,7 @@ def check_missing(res): elif dtype == "Int64": return res is pd.NA else: - return pd.isna(res) + return isna(res) obj = klass([None], dtype=dtype) assert check_missing(getattr(obj, opname)()) @@ -183,7 +183,7 @@ def test_argminmax(self): def test_same_tz_min_max_axis_1(self, op, expected_col): # GH 10390 df = DataFrame( - pd.date_range("2016-01-01 00:00:00", periods=3, tz="UTC"), columns=["a"] + date_range("2016-01-01 00:00:00", periods=3, tz="UTC"), columns=["a"] ) df["b"] = df.a.subtract(Timedelta(seconds=3600)) result = getattr(df, op)(axis=1) @@ -384,13 +384,13 @@ def test_minmax_tz(self, tz_naive_fixture): def test_minmax_nat_datetime64(self, op): # Return NaT obj = DatetimeIndex([]) - assert pd.isna(getattr(obj, op)()) + assert isna(getattr(obj, op)()) obj = DatetimeIndex([pd.NaT]) - assert pd.isna(getattr(obj, op)()) + assert isna(getattr(obj, op)()) obj = DatetimeIndex([pd.NaT, pd.NaT, pd.NaT]) - assert pd.isna(getattr(obj, op)()) + assert isna(getattr(obj, op)()) def test_numpy_minmax_integer(self): # GH#26125 @@ -446,7 +446,7 @@ def test_numpy_minmax_range(self): # is the same as basic integer index def test_numpy_minmax_datetime64(self): - dr = pd.date_range(start="2016-01-15", end="2016-01-20") + dr = date_range(start="2016-01-15", end="2016-01-20") assert np.min(dr) == Timestamp("2016-01-15 00:00:00", freq="D") assert np.max(dr) == Timestamp("2016-01-20 00:00:00", freq="D") @@ -585,7 +585,7 @@ def test_empty(self, method, unit, use_bottleneck, dtype): assert result == unit result = getattr(s, method)(min_count=1) - assert pd.isna(result) + assert isna(result) # Skipna, default result = getattr(s, method)(skipna=True) @@ -596,13 +596,13 @@ def test_empty(self, method, unit, use_bottleneck, dtype): assert result == unit result = getattr(s, method)(skipna=True, min_count=1) - assert pd.isna(result) + assert isna(result) result = getattr(s, method)(skipna=False, min_count=0) assert result == unit result = getattr(s, method)(skipna=False, min_count=1) - assert pd.isna(result) + assert isna(result) # All-NA s = Series([np.nan], dtype=dtype) @@ -615,7 +615,7 @@ def test_empty(self, method, unit, use_bottleneck, dtype): assert result == unit result = getattr(s, method)(min_count=1) - assert pd.isna(result) + assert isna(result) # Skipna, default result = getattr(s, method)(skipna=True) @@ -626,7 +626,7 @@ def test_empty(self, method, unit, use_bottleneck, dtype): assert result == unit result = getattr(s, method)(skipna=True, min_count=1) - assert pd.isna(result) + assert isna(result) # Mix of valid, empty s = Series([np.nan, 1], dtype=dtype) @@ -654,18 +654,18 @@ def test_empty(self, method, unit, use_bottleneck, dtype): s = Series([1], dtype=dtype) result = getattr(s, method)(min_count=2) - assert pd.isna(result) + assert isna(result) result = getattr(s, method)(skipna=False, min_count=2) - assert pd.isna(result) + assert isna(result) s = Series([np.nan], dtype=dtype) result = getattr(s, method)(min_count=2) - assert pd.isna(result) + assert isna(result) s = Series([np.nan, 1], dtype=dtype) result = getattr(s, method)(min_count=2) - assert pd.isna(result) + assert isna(result) @pytest.mark.parametrize("method, unit", [("sum", 0.0), ("prod", 1.0)]) def test_empty_multi(self, method, unit): @@ -713,7 +713,7 @@ def test_ops_consistency_on_empty(self, method): # float result = getattr(Series(dtype=float), method)() - assert pd.isna(result) + assert isna(result) # timedelta64[ns] tdser = Series([], dtype="m8[ns]") @@ -817,7 +817,7 @@ def test_idxmin(self): # skipna or no assert string_series[string_series.idxmin()] == string_series.min() - assert pd.isna(string_series.idxmin(skipna=False)) + assert isna(string_series.idxmin(skipna=False)) # no NaNs nona = string_series.dropna() @@ -826,10 +826,10 @@ def test_idxmin(self): # all NaNs allna = string_series * np.nan - assert pd.isna(allna.idxmin()) + assert isna(allna.idxmin()) # datetime64[ns] - s = Series(pd.date_range("20130102", periods=6)) + s = Series(date_range("20130102", periods=6)) result = s.idxmin() assert result == 0 @@ -847,7 +847,7 @@ def test_idxmax(self): # skipna or no assert string_series[string_series.idxmax()] == string_series.max() - assert pd.isna(string_series.idxmax(skipna=False)) + assert isna(string_series.idxmax(skipna=False)) # no NaNs nona = string_series.dropna() @@ -856,9 +856,7 @@ def test_idxmax(self): # all NaNs allna = string_series * np.nan - assert pd.isna(allna.idxmax()) - - from pandas import date_range + assert isna(allna.idxmax()) s = Series(date_range("20130102", periods=6)) result = s.idxmax() @@ -998,7 +996,7 @@ def test_any_all_datetimelike(self): def test_timedelta64_analytics(self): # index min/max - dti = pd.date_range("2012-1-1", periods=3, freq="D") + dti = date_range("2012-1-1", periods=3, freq="D") td = Series(dti) - Timestamp("20120101") result = td.idxmin() @@ -1018,8 +1016,8 @@ def test_timedelta64_analytics(self): assert result == 2 # abs - s1 = Series(pd.date_range("20120101", periods=3)) - s2 = Series(pd.date_range("20120102", periods=3)) + s1 = Series(date_range("20120101", periods=3)) + s2 = Series(date_range("20120102", periods=3)) expected = Series(s2 - s1) result = np.abs(s1 - s2) @@ -1124,7 +1122,7 @@ def test_minmax_nat_dataframe(self, nat_df): assert nat_df.max(skipna=False)[0] is pd.NaT def test_min_max(self): - rng = pd.date_range("1/1/2000", "12/31/2000") + rng = date_range("1/1/2000", "12/31/2000") rng2 = rng.take(np.random.permutation(len(rng))) the_min = rng2.min() @@ -1138,7 +1136,7 @@ def test_min_max(self): assert rng.max() == rng[-1] def test_min_max_series(self): - rng = pd.date_range("1/1/2000", periods=10, freq="4h") + rng = date_range("1/1/2000", periods=10, freq="4h") lvls = ["A", "A", "A", "B", "B", "B", "C", "C", "C", "C"] df = DataFrame({"TS": rng, "V": np.random.randn(len(rng)), "L": lvls}) diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index c23a22448fbb0..017f113cd1985 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -112,12 +112,12 @@ def test_resample_basic(series, closed, expected): def test_resample_integerarray(): # GH 25580, resample on IntegerArray ts = Series( - range(9), index=pd.date_range("1/1/2000", periods=9, freq="T"), dtype="Int64" + range(9), index=date_range("1/1/2000", periods=9, freq="T"), dtype="Int64" ) result = ts.resample("3T").sum() expected = Series( [3, 12, 21], - index=pd.date_range("1/1/2000", periods=3, freq="3T"), + index=date_range("1/1/2000", periods=3, freq="3T"), dtype="Int64", ) tm.assert_series_equal(result, expected) @@ -125,7 +125,7 @@ def test_resample_integerarray(): result = ts.resample("3T").mean() expected = Series( [1, 4, 7], - index=pd.date_range("1/1/2000", periods=3, freq="3T"), + index=date_range("1/1/2000", periods=3, freq="3T"), dtype="Float64", ) tm.assert_series_equal(result, expected) @@ -516,8 +516,8 @@ def test_resample_ohlc(series): def test_resample_ohlc_result(): # GH 12332 - index = pd.date_range("1-1-2000", "2-15-2000", freq="h") - index = index.union(pd.date_range("4-15-2000", "5-15-2000", freq="h")) + index = date_range("1-1-2000", "2-15-2000", freq="h") + index = index.union(date_range("4-15-2000", "5-15-2000", freq="h")) s = Series(range(len(index)), index=index) a = s.loc[:"4-15-2000"].resample("30T").ohlc() @@ -792,7 +792,7 @@ def test_resample_bad_offset(offset): def test_resample_origin_prime_freq(): # GH 31809 start, end = "2000-10-01 23:30:00", "2000-10-02 00:30:00" - rng = pd.date_range(start, end, freq="7min") + rng = date_range(start, end, freq="7min") ts = Series(np.random.randn(len(rng)), index=rng) exp_rng = date_range("2000-10-01 23:14:00", "2000-10-02 00:22:00", freq="17min") @@ -850,7 +850,7 @@ def test_resample_origin_with_tz(): def test_resample_origin_epoch_with_tz_day_vs_24h(): # GH 34474 start, end = "2000-10-01 23:30:00+0500", "2000-12-02 00:30:00+0500" - rng = pd.date_range(start, end, freq="7min") + rng = date_range(start, end, freq="7min") random_values = np.random.randn(len(rng)) ts_1 = Series(random_values, index=rng) @@ -867,7 +867,7 @@ def test_resample_origin_epoch_with_tz_day_vs_24h(): # check that we have the similar results with two different timezones (+2H and +5H) start, end = "2000-10-01 23:30:00+0200", "2000-12-02 00:30:00+0200" - rng = pd.date_range(start, end, freq="7min") + rng = date_range(start, end, freq="7min") ts_2 = Series(random_values, index=rng) result_5 = ts_2.resample("D", origin="epoch").mean() result_6 = ts_2.resample("24H", origin="epoch").mean() @@ -890,7 +890,7 @@ def _create_series(values, timestamps, freq="D"): # test classical behavior of origin in a DST context start = Timestamp("2013-11-02", tz=tz) end = Timestamp("2013-11-03 23:59", tz=tz) - rng = pd.date_range(start, end, freq="1h") + rng = date_range(start, end, freq="1h") ts = Series(np.ones(len(rng)), index=rng) expected = _create_series([24.0, 25.0], ["2013-11-02", "2013-11-03"]) @@ -901,7 +901,7 @@ def _create_series(values, timestamps, freq="D"): # test complex behavior of origin/offset in a DST context start = Timestamp("2013-11-03", tz=tz) end = Timestamp("2013-11-03 23:59", tz=tz) - rng = pd.date_range(start, end, freq="1h") + rng = date_range(start, end, freq="1h") ts = Series(np.ones(len(rng)), index=rng) expected_ts = ["2013-11-02 22:00-05:00", "2013-11-03 22:00-06:00"] @@ -956,7 +956,7 @@ def test_period_with_agg(): # aggregate a period resampler with a lambda s2 = Series( np.random.randint(0, 5, 50), - index=pd.period_range("2012-01-01", freq="H", periods=50), + index=period_range("2012-01-01", freq="H", periods=50), dtype="float64", ) @@ -990,7 +990,7 @@ def test_resample_dtype_preservation(): df = DataFrame( { - "date": pd.date_range(start="2016-01-01", periods=4, freq="W"), + "date": date_range(start="2016-01-01", periods=4, freq="W"), "group": [1, 1, 2, 2], "val": Series([5, 6, 7, 8], dtype="int32"), } @@ -1009,7 +1009,7 @@ def test_resample_dtype_coercion(): # GH 16361 df = {"a": [1, 3, 1, 4]} - df = DataFrame(df, index=pd.date_range("2017-01-01", "2017-01-04")) + df = DataFrame(df, index=date_range("2017-01-01", "2017-01-04")) expected = df.astype("float64").resample("H").mean()["a"].interpolate("cubic") @@ -1043,12 +1043,12 @@ def test_nanosecond_resample_error(): # Resampling using pd.tseries.offsets.Nano as period start = 1443707890427 exp_start = 1443707890400 - indx = pd.date_range(start=pd.to_datetime(start), periods=10, freq="100n") + indx = date_range(start=pd.to_datetime(start), periods=10, freq="100n") ts = Series(range(len(indx)), index=indx) r = ts.resample(pd.tseries.offsets.Nano(100)) result = r.agg("mean") - exp_indx = pd.date_range(start=pd.to_datetime(exp_start), periods=10, freq="100n") + exp_indx = date_range(start=pd.to_datetime(exp_start), periods=10, freq="100n") exp = Series(range(len(exp_indx)), index=exp_indx) tm.assert_series_equal(result, exp) @@ -1115,8 +1115,8 @@ def test_resample_anchored_multiday(): # # See: https://github.com/pandas-dev/pandas/issues/8683 - index1 = pd.date_range("2014-10-14 23:06:23.206", periods=3, freq="400L") - index2 = pd.date_range("2014-10-15 23:00:00", periods=2, freq="2200L") + index1 = date_range("2014-10-14 23:06:23.206", periods=3, freq="400L") + index2 = date_range("2014-10-15 23:00:00", periods=2, freq="2200L") index = index1.union(index2) s = Series(np.random.randn(5), index=index) @@ -1161,7 +1161,7 @@ def test_anchored_lowercase_buglet(): def test_upsample_apply_functions(): # #1596 - rng = pd.date_range("2012-06-12", periods=4, freq="h") + rng = date_range("2012-06-12", periods=4, freq="h") ts = Series(np.random.randn(len(rng)), index=rng) @@ -1170,7 +1170,7 @@ def test_upsample_apply_functions(): def test_resample_not_monotonic(): - rng = pd.date_range("2012-06-12", periods=200, freq="h") + rng = date_range("2012-06-12", periods=200, freq="h") ts = Series(np.random.randn(len(rng)), index=rng) ts = ts.take(np.random.permutation(len(ts))) @@ -1242,12 +1242,12 @@ def test_resample_consistency(): # GH 6418 # resample with bfill / limit / reindex consistency - i30 = pd.date_range("2002-02-02", periods=4, freq="30T") + i30 = date_range("2002-02-02", periods=4, freq="30T") s = Series(np.arange(4.0), index=i30) s[2] = np.NaN # Upsample by factor 3 with reindex() and resample() methods: - i10 = pd.date_range(i30[0], i30[-1], freq="10T") + i10 = date_range(i30[0], i30[-1], freq="10T") s10 = s.reindex(index=i10, method="bfill") s10_2 = s.reindex(index=i10, method="bfill", limit=2) @@ -1351,8 +1351,8 @@ def test_resample_nunique_preserves_column_level_names(): def test_resample_nunique_with_date_gap(): # GH 13453 - index = pd.date_range("1-1-2000", "2-15-2000", freq="h") - index2 = pd.date_range("4-15-2000", "5-15-2000", freq="h") + index = date_range("1-1-2000", "2-15-2000", freq="h") + index2 = date_range("4-15-2000", "5-15-2000", freq="h") index3 = index.append(index2) s = Series(range(len(index3)), index=index3, dtype="int64") r = s.resample("M") @@ -1449,7 +1449,7 @@ def test_groupby_with_dst_time_change(): df = DataFrame([1, 2], index=index) result = df.groupby(Grouper(freq="1d")).last() - expected_index_values = pd.date_range( + expected_index_values = date_range( "2016-11-02", "2016-11-24", freq="d", tz="America/Chicago" ) @@ -1574,11 +1574,11 @@ def test_downsample_across_dst_weekly(): ) tm.assert_frame_equal(result, expected) - idx = pd.date_range("2013-04-01", "2013-05-01", tz="Europe/London", freq="H") + idx = date_range("2013-04-01", "2013-05-01", tz="Europe/London", freq="H") s = Series(index=idx, dtype=np.float64) result = s.resample("W").mean() expected = Series( - index=pd.date_range("2013-04-07", freq="W", periods=5, tz="Europe/London"), + index=date_range("2013-04-07", freq="W", periods=5, tz="Europe/London"), dtype=np.float64, ) tm.assert_series_equal(result, expected) @@ -1588,7 +1588,7 @@ def test_downsample_dst_at_midnight(): # GH 25758 start = datetime(2018, 11, 3, 12) end = datetime(2018, 11, 5, 12) - index = pd.date_range(start, end, freq="1H") + index = date_range(start, end, freq="1H") index = index.tz_localize("UTC").tz_convert("America/Havana") data = list(range(len(index))) dataframe = DataFrame(data, index=index) @@ -1668,7 +1668,7 @@ def f(data, add_arg): tm.assert_series_equal(result, expected) # Testing dataframe - df = DataFrame({"A": 1, "B": 2}, index=pd.date_range("2017", periods=10)) + df = DataFrame({"A": 1, "B": 2}, index=date_range("2017", periods=10)) result = df.groupby("A").resample("D").agg(f, multiplier) expected = df.groupby("A").resample("D").mean().multiply(multiplier) tm.assert_frame_equal(result, expected) @@ -1694,7 +1694,7 @@ def test_resample_equivalent_offsets(n1, freq1, n2, freq2, k): # GH 24127 n1_ = n1 * k n2_ = n2 * k - s = Series(0, index=pd.date_range("19910905 13:00", "19911005 07:00", freq=freq1)) + s = Series(0, index=date_range("19910905 13:00", "19911005 07:00", freq=freq1)) s = s + range(len(s)) result1 = s.resample(str(n1_) + freq1).mean() @@ -1784,10 +1784,10 @@ def test_resample_calendar_day_with_dst( first: str, last: str, freq_in: str, freq_out: str, exp_last: str ): # GH 35219 - ts = Series(1.0, pd.date_range(first, last, freq=freq_in, tz="Europe/Amsterdam")) + ts = Series(1.0, date_range(first, last, freq=freq_in, tz="Europe/Amsterdam")) result = ts.resample(freq_out).pad() expected = Series( - 1.0, pd.date_range(first, exp_last, freq=freq_out, tz="Europe/Amsterdam") + 1.0, date_range(first, exp_last, freq=freq_out, tz="Europe/Amsterdam") ) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/resample/test_deprecated.py b/pandas/tests/resample/test_deprecated.py index b3d568678afb5..3bf63a0cb0ae3 100644 --- a/pandas/tests/resample/test_deprecated.py +++ b/pandas/tests/resample/test_deprecated.py @@ -40,7 +40,7 @@ def _create_index(*args, **kwargs): def test_deprecating_on_loffset_and_base(): # GH 31809 - idx = pd.date_range("2001-01-01", periods=4, freq="T") + idx = date_range("2001-01-01", periods=4, freq="T") df = DataFrame(data=4 * [range(2)], index=idx, columns=["a", "b"]) with tm.assert_produces_warning(FutureWarning): @@ -231,7 +231,7 @@ def test_loffset_returns_datetimeindex(frame, kind, agg_arg): ) def test_resample_with_non_zero_base(start, end, start_freq, end_freq, base, offset): # GH 23882 - s = Series(0, index=pd.period_range(start, end, freq=start_freq)) + s = Series(0, index=period_range(start, end, freq=start_freq)) s = s + np.arange(len(s)) with tm.assert_produces_warning(FutureWarning): result = s.resample(end_freq, base=base).mean() diff --git a/pandas/tests/resample/test_period_index.py b/pandas/tests/resample/test_period_index.py index e83196e9c7d56..15c9de427a8ea 100644 --- a/pandas/tests/resample/test_period_index.py +++ b/pandas/tests/resample/test_period_index.py @@ -213,9 +213,9 @@ def test_resample_basic(self): ) def test_resample_count(self, freq, expected_vals): # GH12774 - series = Series(1, index=pd.period_range(start="2000", periods=100)) + series = Series(1, index=period_range(start="2000", periods=100)) result = series.resample(freq).count() - expected_index = pd.period_range( + expected_index = period_range( start="2000", freq=freq, periods=len(expected_vals) ) expected = Series(expected_vals, index=expected_index) @@ -224,9 +224,7 @@ def test_resample_count(self, freq, expected_vals): def test_resample_same_freq(self, resample_method): # GH12770 - series = Series( - range(3), index=pd.period_range(start="2000", periods=3, freq="M") - ) + series = Series(range(3), index=period_range(start="2000", periods=3, freq="M")) expected = series result = getattr(series.resample("M"), resample_method)() @@ -239,7 +237,7 @@ def test_resample_incompat_freq(self): ) with pytest.raises(IncompatibleFrequency, match=msg): Series( - range(3), index=pd.period_range(start="2000", periods=3, freq="M") + range(3), index=period_range(start="2000", periods=3, freq="M") ).resample("W").mean() def test_with_local_timezone_pytz(self): @@ -250,7 +248,7 @@ def test_with_local_timezone_pytz(self): # 1 day later end = datetime(year=2013, month=11, day=2, hour=0, minute=0, tzinfo=pytz.utc) - index = pd.date_range(start, end, freq="H") + index = date_range(start, end, freq="H") series = Series(1, index=index) series = series.tz_convert(local_timezone) @@ -259,14 +257,14 @@ def test_with_local_timezone_pytz(self): # Create the expected series # Index is moved back a day with the timezone conversion from UTC to # Pacific - expected_index = pd.period_range(start=start, end=end, freq="D") - offsets.Day() + expected_index = period_range(start=start, end=end, freq="D") - offsets.Day() expected = Series(1, index=expected_index) tm.assert_series_equal(result, expected) def test_resample_with_pytz(self): # GH 13238 s = Series( - 2, index=pd.date_range("2017-01-01", periods=48, freq="H", tz="US/Eastern") + 2, index=date_range("2017-01-01", periods=48, freq="H", tz="US/Eastern") ) result = s.resample("D").mean() expected = Series( @@ -291,7 +289,7 @@ def test_with_local_timezone_dateutil(self): year=2013, month=11, day=2, hour=0, minute=0, tzinfo=dateutil.tz.tzutc() ) - index = pd.date_range(start, end, freq="H", name="idx") + index = date_range(start, end, freq="H", name="idx") series = Series(1, index=index) series = series.tz_convert(local_timezone) @@ -301,7 +299,7 @@ def test_with_local_timezone_dateutil(self): # Index is moved back a day with the timezone conversion from UTC to # Pacific expected_index = ( - pd.period_range(start=start, end=end, freq="D", name="idx") - offsets.Day() + period_range(start=start, end=end, freq="D", name="idx") - offsets.Day() ) expected = Series(1, index=expected_index) tm.assert_series_equal(result, expected) @@ -332,7 +330,7 @@ def test_resample_nonexistent_time_bin_edge(self): def test_resample_ambiguous_time_bin_edge(self): # GH 10117 - idx = pd.date_range( + idx = date_range( "2014-10-25 22:00:00", "2014-10-26 00:30:00", freq="30T", tz="Europe/London" ) expected = Series(np.zeros(len(idx)), index=idx) @@ -816,7 +814,7 @@ def test_resample_with_only_nat(self): ) def test_resample_with_offset(self, start, end, start_freq, end_freq, offset): # GH 23882 & 31809 - s = Series(0, index=pd.period_range(start, end, freq=start_freq)) + s = Series(0, index=period_range(start, end, freq=start_freq)) s = s + np.arange(len(s)) result = s.resample(end_freq, offset=offset).mean() result = result.to_timestamp(end_freq) @@ -858,7 +856,7 @@ def test_get_period_range_edges(self, first, last, freq, exp_first, exp_last): def test_sum_min_count(self): # GH 19974 - index = pd.date_range(start="2018", freq="M", periods=6) + index = date_range(start="2018", freq="M", periods=6) data = np.ones(6) data[3:6] = np.nan s = Series(data, index).to_period() diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index 2cd9bb70385bf..13a8850bfefd0 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -54,7 +54,7 @@ def test_groupby_resample_api(): # when appropriate df = DataFrame( { - "date": pd.date_range(start="2016-01-01", periods=4, freq="W"), + "date": date_range(start="2016-01-01", periods=4, freq="W"), "group": [1, 1, 2, 2], "val": [5, 6, 7, 8], } @@ -62,8 +62,8 @@ def test_groupby_resample_api(): # replication step i = ( - pd.date_range("2016-01-03", periods=8).tolist() - + pd.date_range("2016-01-17", periods=8).tolist() + date_range("2016-01-03", periods=8).tolist() + + date_range("2016-01-17", periods=8).tolist() ) index = pd.MultiIndex.from_arrays([[1] * 8 + [2] * 8, i], names=["group", "date"]) expected = DataFrame({"val": [5] * 7 + [6] + [7] * 7 + [8]}, index=index) @@ -79,7 +79,7 @@ def test_groupby_resample_on_api(): df = DataFrame( { "key": ["A", "B"] * 5, - "dates": pd.date_range("2016-01-01", periods=10), + "dates": date_range("2016-01-01", periods=10), "values": np.random.randn(10), } ) @@ -143,7 +143,7 @@ def test_api_compat_before_use(): # make sure that we are setting the binner # on these attributes for attr in ["groups", "ngroups", "indices"]: - rng = pd.date_range("1/1/2012", periods=100, freq="S") + rng = date_range("1/1/2012", periods=100, freq="S") ts = Series(np.arange(len(rng)), index=rng) rs = ts.resample("30s") @@ -172,12 +172,12 @@ def tests_skip_nuisance(test_frame): def test_downsample_but_actually_upsampling(): # this is reindex / asfreq - rng = pd.date_range("1/1/2012", periods=100, freq="S") + rng = date_range("1/1/2012", periods=100, freq="S") ts = Series(np.arange(len(rng), dtype="int64"), index=rng) result = ts.resample("20s").asfreq() expected = Series( [0, 20, 40, 60, 80], - index=pd.date_range("2012-01-01 00:00:00", freq="20s", periods=5), + index=date_range("2012-01-01 00:00:00", freq="20s", periods=5), ) tm.assert_series_equal(result, expected) @@ -188,7 +188,7 @@ def test_combined_up_downsampling_of_irregular(): # ts2.resample('2s').mean().ffill() # preserve these semantics - rng = pd.date_range("1/1/2012", periods=100, freq="S") + rng = date_range("1/1/2012", periods=100, freq="S") ts = Series(np.arange(len(rng)), index=rng) ts2 = ts.iloc[[0, 1, 2, 3, 5, 7, 11, 15, 16, 25, 30]] @@ -249,7 +249,7 @@ def test_transform(): def test_fillna(): # need to upsample here - rng = pd.date_range("1/1/2012", periods=10, freq="2S") + rng = date_range("1/1/2012", periods=10, freq="2S") ts = Series(np.arange(len(rng), dtype="int64"), index=rng) r = ts.resample("s") @@ -286,7 +286,7 @@ def test_agg_consistency(): # similar aggregations with and w/o selection list df = DataFrame( np.random.randn(1000, 3), - index=pd.date_range("1/1/2012", freq="S", periods=1000), + index=date_range("1/1/2012", freq="S", periods=1000), columns=["A", "B", "C"], ) @@ -573,7 +573,7 @@ def test_agg_with_datetime_index_list_agg_func(col_name): # We catch these errors and move on to the correct branch. df = DataFrame( list(range(200)), - index=pd.date_range( + index=date_range( start="2017-01-01", freq="15min", periods=200, tz="Europe/Berlin" ), columns=[col_name], @@ -581,9 +581,7 @@ def test_agg_with_datetime_index_list_agg_func(col_name): result = df.resample("1d").aggregate(["mean"]) expected = DataFrame( [47.5, 143.5, 195.5], - index=pd.date_range( - start="2017-01-01", freq="D", periods=3, tz="Europe/Berlin" - ), + index=date_range(start="2017-01-01", freq="D", periods=3, tz="Europe/Berlin"), columns=pd.MultiIndex(levels=[[col_name], ["mean"]], codes=[[0], [0]]), ) tm.assert_frame_equal(result, expected) @@ -591,7 +589,7 @@ def test_agg_with_datetime_index_list_agg_func(col_name): def test_resample_agg_readonly(): # GH#31710 cython needs to allow readonly data - index = pd.date_range("2020-01-01", "2020-01-02", freq="1h") + index = date_range("2020-01-01", "2020-01-02", freq="1h") arr = np.zeros_like(index) arr.setflags(write=False) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 86955ac4e4d22..a4cd0fff4111f 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -73,7 +73,7 @@ def f(x): df = DataFrame( { - "date": pd.date_range(start="2016-01-01", periods=4, freq="W"), + "date": date_range(start="2016-01-01", periods=4, freq="W"), "group": [1, 1, 2, 2], "val": [5, 6, 7, 8], } @@ -107,7 +107,7 @@ def test_getitem_multiple(): # GH 13174 # multiple calls after selection causing an issue with aliasing data = [{"id": 1, "buyer": "A"}, {"id": 2, "buyer": "B"}] - df = DataFrame(data, index=pd.date_range("2016-01-01", periods=2)) + df = DataFrame(data, index=date_range("2016-01-01", periods=2)) r = df.groupby("id").resample("1D") result = r["buyer"].count() expected = Series( @@ -127,7 +127,7 @@ def test_getitem_multiple(): def test_groupby_resample_on_api_with_getitem(): # GH 17813 df = DataFrame( - {"id": list("aabbb"), "date": pd.date_range("1-1-2016", periods=5), "data": 1} + {"id": list("aabbb"), "date": date_range("1-1-2016", periods=5), "data": 1} ) exp = df.set_index("date").groupby("id").resample("2D")["data"].sum() result = df.groupby("id").resample("2D", on="date")["data"].sum() @@ -141,7 +141,7 @@ def test_groupby_with_origin(): start, end = "1/1/2000 00:00:00", "1/31/2000 00:00" middle = "1/15/2000 00:00:00" - rng = pd.date_range(start, end, freq="1231min") # prime number + rng = date_range(start, end, freq="1231min") # prime number ts = Series(np.random.randn(len(rng)), index=rng) ts2 = ts[middle:end] @@ -179,7 +179,7 @@ def test_nearest(): # GH 17496 # Resample nearest - index = pd.date_range("1/1/2000", periods=3, freq="T") + index = date_range("1/1/2000", periods=3, freq="T") result = Series(range(3), index=index).resample("20s").nearest() expected = Series( @@ -264,7 +264,7 @@ def f(x): def test_apply_with_mutated_index(): # GH 15169 - index = pd.date_range("1-1-2015", "12-31-15", freq="D") + index = date_range("1-1-2015", "12-31-15", freq="D") df = DataFrame(data={"col1": np.random.rand(len(index))}, index=index) def f(x): @@ -339,7 +339,7 @@ def test_median_duplicate_columns(): df = DataFrame( np.random.randn(20, 3), columns=list("aaa"), - index=pd.date_range("2012-01-01", periods=20, freq="s"), + index=date_range("2012-01-01", periods=20, freq="s"), ) df2 = df.copy() df2.columns = ["a", "b", "c"] @@ -353,11 +353,11 @@ def test_apply_to_one_column_of_df(): # GH: 36951 df = DataFrame( {"col": range(10), "col1": range(10, 20)}, - index=pd.date_range("2012-01-01", periods=10, freq="20min"), + index=date_range("2012-01-01", periods=10, freq="20min"), ) result = df.resample("H").apply(lambda group: group.col.sum()) expected = Series( - [3, 12, 21, 9], index=pd.date_range("2012-01-01", periods=4, freq="H") + [3, 12, 21, 9], index=date_range("2012-01-01", periods=4, freq="H") ) tm.assert_series_equal(result, expected) result = df.resample("H").apply(lambda group: group["col"].sum()) diff --git a/pandas/tests/resample/test_time_grouper.py b/pandas/tests/resample/test_time_grouper.py index c12111e20a4b1..7421e74ac2bc4 100644 --- a/pandas/tests/resample/test_time_grouper.py +++ b/pandas/tests/resample/test_time_grouper.py @@ -56,7 +56,7 @@ def test_numpy_reduction(): def test_apply_iteration(): # #2300 N = 1000 - ind = pd.date_range(start="2000-01-01", freq="D", periods=N) + ind = date_range(start="2000-01-01", freq="D", periods=N) df = DataFrame({"open": 1, "close": 2}, index=ind) tg = Grouper(freq="M") @@ -167,7 +167,7 @@ def test_aggregate_normal(resample_method): ], ) def test_resample_entirely_nat_window(method, method_args, unit): - s = Series([0] * 2 + [np.nan] * 2, index=pd.date_range("2017", periods=4)) + s = Series([0] * 2 + [np.nan] * 2, index=date_range("2017", periods=4)) result = methodcaller(method, **method_args)(s.resample("2d")) expected = Series( [0.0, unit], index=pd.DatetimeIndex(["2017-01-01", "2017-01-03"], freq="2D") @@ -278,7 +278,7 @@ def test_repr(): ], ) def test_upsample_sum(method, method_args, expected_values): - s = Series(1, index=pd.date_range("2017", periods=2, freq="H")) + s = Series(1, index=date_range("2017", periods=2, freq="H")) resampled = s.resample("30T") index = pd.DatetimeIndex( ["2017-01-01T00:00:00", "2017-01-01T00:30:00", "2017-01-01T01:00:00"], @@ -295,7 +295,7 @@ def test_groupby_resample_interpolate(): df = DataFrame(d) - df["week_starting"] = pd.date_range("01/01/2018", periods=3, freq="W") + df["week_starting"] = date_range("01/01/2018", periods=3, freq="W") result = ( df.set_index("week_starting") diff --git a/pandas/tests/resample/test_timedelta.py b/pandas/tests/resample/test_timedelta.py index 1c440b889b146..b8cb8e56675d9 100644 --- a/pandas/tests/resample/test_timedelta.py +++ b/pandas/tests/resample/test_timedelta.py @@ -46,7 +46,7 @@ def test_resample_with_timedeltas(): expected = DataFrame({"A": np.arange(1480)}) expected = expected.groupby(expected.index // 30).sum() - expected.index = pd.timedelta_range("0 days", freq="30T", periods=50) + expected.index = timedelta_range("0 days", freq="30T", periods=50) df = DataFrame( {"A": np.arange(1480)}, index=pd.to_timedelta(np.arange(1480), unit="T") @@ -62,18 +62,16 @@ def test_resample_with_timedeltas(): def test_resample_single_period_timedelta(): - s = Series(list(range(5)), index=pd.timedelta_range("1 day", freq="s", periods=5)) + s = Series(list(range(5)), index=timedelta_range("1 day", freq="s", periods=5)) result = s.resample("2s").sum() - expected = Series( - [1, 5, 4], index=pd.timedelta_range("1 day", freq="2s", periods=3) - ) + expected = Series([1, 5, 4], index=timedelta_range("1 day", freq="2s", periods=3)) tm.assert_series_equal(result, expected) def test_resample_timedelta_idempotency(): # GH 12072 - index = pd.timedelta_range("0", periods=9, freq="10L") + index = timedelta_range("0", periods=9, freq="10L") series = Series(range(9), index=index) result = series.resample("10L").mean() expected = series @@ -143,10 +141,10 @@ def test_resample_timedelta_values(): def test_resample_timedelta_edge_case(start, end, freq, resample_freq): # GH 33498 # check that the timedelta bins does not contains an extra bin - idx = pd.timedelta_range(start=start, end=end, freq=freq) + idx = timedelta_range(start=start, end=end, freq=freq) s = Series(np.arange(len(idx)), index=idx) result = s.resample(resample_freq).min() - expected_index = pd.timedelta_range(freq=resample_freq, start=start, end=end) + expected_index = timedelta_range(freq=resample_freq, start=start, end=end) tm.assert_index_equal(result.index, expected_index) assert result.index.freq == expected_index.freq assert not np.isnan(result[-1]) @@ -156,13 +154,13 @@ def test_resample_with_timedelta_yields_no_empty_groups(): # GH 10603 df = DataFrame( np.random.normal(size=(10000, 4)), - index=pd.timedelta_range(start="0s", periods=10000, freq="3906250n"), + index=timedelta_range(start="0s", periods=10000, freq="3906250n"), ) result = df.loc["1s":, :].resample("3s").apply(lambda x: len(x)) expected = DataFrame( [[768.0] * 4] * 12 + [[528.0] * 4], - index=pd.timedelta_range(start="1s", periods=13, freq="3s"), + index=timedelta_range(start="1s", periods=13, freq="3s"), ) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/reshape/concat/test_concat.py b/pandas/tests/reshape/concat/test_concat.py index 4750f9b0c40a3..84cbdc80f0370 100644 --- a/pandas/tests/reshape/concat/test_concat.py +++ b/pandas/tests/reshape/concat/test_concat.py @@ -410,21 +410,21 @@ def __getitem__(self, index): except KeyError as err: raise IndexError from err - tm.assert_frame_equal(pd.concat(CustomIterator1(), ignore_index=True), expected) + tm.assert_frame_equal(concat(CustomIterator1(), ignore_index=True), expected) class CustomIterator2(abc.Iterable): def __iter__(self): yield df1 yield df2 - tm.assert_frame_equal(pd.concat(CustomIterator2(), ignore_index=True), expected) + tm.assert_frame_equal(concat(CustomIterator2(), ignore_index=True), expected) def test_concat_order(self): # GH 17344 dfs = [DataFrame(index=range(3), columns=["a", 1, None])] dfs += [DataFrame(index=range(3), columns=[None, 1, "a"]) for i in range(100)] - result = pd.concat(dfs, sort=True).columns + result = concat(dfs, sort=True).columns expected = dfs[0].columns tm.assert_index_equal(result, expected) @@ -432,16 +432,16 @@ def test_concat_different_extension_dtypes_upcasts(self): a = Series(pd.array([1, 2], dtype="Int64")) b = Series(to_decimal([1, 2])) - result = pd.concat([a, b], ignore_index=True) + result = concat([a, b], ignore_index=True) expected = Series([1, 2, Decimal(1), Decimal(2)], dtype=object) tm.assert_series_equal(result, expected) def test_concat_ordered_dict(self): # GH 21510 - expected = pd.concat( + expected = concat( [Series(range(3)), Series(range(4))], keys=["First", "Another"] ) - result = pd.concat({"First": Series(range(3)), "Another": Series(range(4))}) + result = concat({"First": Series(range(3)), "Another": Series(range(4))}) tm.assert_series_equal(result, expected) @@ -456,7 +456,7 @@ def test_concat_no_unnecessary_upcast(dt, pdt): pdt(np.array([np.nan], dtype=dt, ndmin=dims)), pdt(np.array([5], dtype=dt, ndmin=dims)), ] - x = pd.concat(dfs) + x = concat(dfs) assert x.values.dtype == dt @@ -470,7 +470,7 @@ def test_concat_will_upcast(dt, pdt): pdt(np.array([np.nan], ndmin=dims)), pdt(np.array([5], dtype=dt, ndmin=dims)), ] - x = pd.concat(dfs) + x = concat(dfs) assert x.values.dtype == "float64" @@ -480,7 +480,7 @@ def test_concat_empty_and_non_empty_frame_regression(dtype): df1 = DataFrame({"foo": [1]}).astype(dtype) df2 = DataFrame({"foo": []}) expected = df1 - result = pd.concat([df1, df2]) + result = concat([df1, df2]) tm.assert_frame_equal(result, expected) @@ -490,7 +490,7 @@ def test_concat_sparse(): expected = DataFrame(data=[[0, 0], [1, 1], [2, 2]]).astype( pd.SparseDtype(np.int64, 0) ) - result = pd.concat([a, a], axis=1) + result = concat([a, a], axis=1) tm.assert_frame_equal(result, expected) @@ -501,7 +501,7 @@ def test_concat_dense_sparse(): expected = Series(data=[1, None, 1], index=[0, 1, 0]).astype( pd.SparseDtype(np.float64, None) ) - result = pd.concat([a, b], axis=0) + result = concat([a, b], axis=0) tm.assert_series_equal(result, expected) @@ -539,11 +539,11 @@ def test_concat_frame_axis0_extension_dtypes(): df1 = DataFrame({"a": pd.array([1, 2, 3], dtype="Int64")}) df2 = DataFrame({"a": np.array([4, 5, 6])}) - result = pd.concat([df1, df2], ignore_index=True) + result = concat([df1, df2], ignore_index=True) expected = DataFrame({"a": [1, 2, 3, 4, 5, 6]}, dtype="Int64") tm.assert_frame_equal(result, expected) - result = pd.concat([df2, df1], ignore_index=True) + result = concat([df2, df1], ignore_index=True) expected = DataFrame({"a": [4, 5, 6, 1, 2, 3]}, dtype="Int64") tm.assert_frame_equal(result, expected) @@ -552,7 +552,7 @@ def test_concat_preserves_extension_int64_dtype(): # GH 24768 df_a = DataFrame({"a": [-1]}, dtype="Int64") df_b = DataFrame({"b": [1]}, dtype="Int64") - result = pd.concat([df_a, df_b], ignore_index=True) + result = concat([df_a, df_b], ignore_index=True) expected = DataFrame({"a": [-1, None], "b": [None, 1]}, dtype="Int64") tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/reshape/concat/test_dataframe.py b/pandas/tests/reshape/concat/test_dataframe.py index 295846ee1b264..62d8310e5158d 100644 --- a/pandas/tests/reshape/concat/test_dataframe.py +++ b/pandas/tests/reshape/concat/test_dataframe.py @@ -12,7 +12,7 @@ def test_concat_multiple_frames_dtypes(self): # GH#2759 A = DataFrame(data=np.ones((10, 2)), columns=["foo", "bar"], dtype=np.float64) B = DataFrame(data=np.ones((10, 2)), dtype=np.float32) - results = pd.concat((A, B), axis=1).dtypes + results = concat((A, B), axis=1).dtypes expected = Series( [np.dtype("float64")] * 2 + [np.dtype("float32")] * 2, index=["foo", "bar", 0, 1], @@ -23,7 +23,7 @@ def test_concat_tuple_keys(self): # GH#14438 df1 = DataFrame(np.ones((2, 2)), columns=list("AB")) df2 = DataFrame(np.ones((3, 2)) * 2, columns=list("AB")) - results = pd.concat((df1, df2), keys=[("bee", "bah"), ("bee", "boo")]) + results = concat((df1, df2), keys=[("bee", "bah"), ("bee", "boo")]) expected = DataFrame( { "A": { @@ -48,7 +48,7 @@ def test_concat_named_keys(self): # GH#14252 df = DataFrame({"foo": [1, 2], "bar": [0.1, 0.2]}) index = Index(["a", "b"], name="baz") - concatted_named_from_keys = pd.concat([df, df], keys=index) + concatted_named_from_keys = concat([df, df], keys=index) expected_named = DataFrame( {"foo": [1, 2, 1, 2], "bar": [0.1, 0.2, 0.1, 0.2]}, index=pd.MultiIndex.from_product((["a", "b"], [0, 1]), names=["baz", None]), @@ -56,12 +56,10 @@ def test_concat_named_keys(self): tm.assert_frame_equal(concatted_named_from_keys, expected_named) index_no_name = Index(["a", "b"], name=None) - concatted_named_from_names = pd.concat( - [df, df], keys=index_no_name, names=["baz"] - ) + concatted_named_from_names = concat([df, df], keys=index_no_name, names=["baz"]) tm.assert_frame_equal(concatted_named_from_names, expected_named) - concatted_unnamed = pd.concat([df, df], keys=index_no_name) + concatted_unnamed = concat([df, df], keys=index_no_name) expected_unnamed = DataFrame( {"foo": [1, 2, 1, 2], "bar": [0.1, 0.2, 0.1, 0.2]}, index=pd.MultiIndex.from_product((["a", "b"], [0, 1]), names=[None, None]), @@ -76,13 +74,13 @@ def test_concat_axis_parameter(self): # Index/row/0 DataFrame expected_index = DataFrame({"A": [0.1, 0.2, 0.3, 0.4]}, index=[0, 1, 0, 1]) - concatted_index = pd.concat([df1, df2], axis="index") + concatted_index = concat([df1, df2], axis="index") tm.assert_frame_equal(concatted_index, expected_index) - concatted_row = pd.concat([df1, df2], axis="rows") + concatted_row = concat([df1, df2], axis="rows") tm.assert_frame_equal(concatted_row, expected_index) - concatted_0 = pd.concat([df1, df2], axis=0) + concatted_0 = concat([df1, df2], axis=0) tm.assert_frame_equal(concatted_0, expected_index) # Columns/1 DataFrame @@ -90,10 +88,10 @@ def test_concat_axis_parameter(self): [[0.1, 0.3], [0.2, 0.4]], index=[0, 1], columns=["A", "A"] ) - concatted_columns = pd.concat([df1, df2], axis="columns") + concatted_columns = concat([df1, df2], axis="columns") tm.assert_frame_equal(concatted_columns, expected_columns) - concatted_1 = pd.concat([df1, df2], axis=1) + concatted_1 = concat([df1, df2], axis=1) tm.assert_frame_equal(concatted_1, expected_columns) series1 = Series([0.1, 0.2]) @@ -102,13 +100,13 @@ def test_concat_axis_parameter(self): # Index/row/0 Series expected_index_series = Series([0.1, 0.2, 0.3, 0.4], index=[0, 1, 0, 1]) - concatted_index_series = pd.concat([series1, series2], axis="index") + concatted_index_series = concat([series1, series2], axis="index") tm.assert_series_equal(concatted_index_series, expected_index_series) - concatted_row_series = pd.concat([series1, series2], axis="rows") + concatted_row_series = concat([series1, series2], axis="rows") tm.assert_series_equal(concatted_row_series, expected_index_series) - concatted_0_series = pd.concat([series1, series2], axis=0) + concatted_0_series = concat([series1, series2], axis=0) tm.assert_series_equal(concatted_0_series, expected_index_series) # Columns/1 Series @@ -116,15 +114,15 @@ def test_concat_axis_parameter(self): [[0.1, 0.3], [0.2, 0.4]], index=[0, 1], columns=[0, 1] ) - concatted_columns_series = pd.concat([series1, series2], axis="columns") + concatted_columns_series = concat([series1, series2], axis="columns") tm.assert_frame_equal(concatted_columns_series, expected_columns_series) - concatted_1_series = pd.concat([series1, series2], axis=1) + concatted_1_series = concat([series1, series2], axis=1) tm.assert_frame_equal(concatted_1_series, expected_columns_series) # Testing ValueError with pytest.raises(ValueError, match="No axis named"): - pd.concat([series1, series2], axis="something") + concat([series1, series2], axis="something") def test_concat_numerical_names(self): # GH#15262, GH#12223 @@ -137,7 +135,7 @@ def test_concat_numerical_names(self): ) ), ) - result = pd.concat((df.iloc[:2, :], df.iloc[-2:, :])) + result = concat((df.iloc[:2, :], df.iloc[-2:, :])) expected = DataFrame( {"col": [0, 1, 7, 8]}, dtype="int32", @@ -150,7 +148,7 @@ def test_concat_numerical_names(self): def test_concat_astype_dup_col(self): # GH#23049 df = DataFrame([{"a": "b"}]) - df = pd.concat([df, df], axis=1) + df = concat([df, df], axis=1) result = df.astype("category") expected = DataFrame( diff --git a/pandas/tests/reshape/concat/test_datetimes.py b/pandas/tests/reshape/concat/test_datetimes.py index 92181e7dffc50..332c3c8f30562 100644 --- a/pandas/tests/reshape/concat/test_datetimes.py +++ b/pandas/tests/reshape/concat/test_datetimes.py @@ -44,15 +44,15 @@ def test_concat_datetime_datetime64_frame(self): df1 = DataFrame({"date": ind, "test": range(10)}) # it works! - pd.concat([df1, df2_obj]) + concat([df1, df2_obj]) def test_concat_datetime_timezone(self): # GH 18523 - idx1 = pd.date_range("2011-01-01", periods=3, freq="H", tz="Europe/Paris") - idx2 = pd.date_range(start=idx1[0], end=idx1[-1], freq="H") + idx1 = date_range("2011-01-01", periods=3, freq="H", tz="Europe/Paris") + idx2 = date_range(start=idx1[0], end=idx1[-1], freq="H") df1 = DataFrame({"a": [1, 2, 3]}, index=idx1) df2 = DataFrame({"b": [1, 2, 3]}, index=idx2) - result = pd.concat([df1, df2], axis=1) + result = concat([df1, df2], axis=1) exp_idx = ( DatetimeIndex( @@ -73,9 +73,9 @@ def test_concat_datetime_timezone(self): tm.assert_frame_equal(result, expected) - idx3 = pd.date_range("2011-01-01", periods=3, freq="H", tz="Asia/Tokyo") + idx3 = date_range("2011-01-01", periods=3, freq="H", tz="Asia/Tokyo") df3 = DataFrame({"b": [1, 2, 3]}, index=idx3) - result = pd.concat([df1, df3], axis=1) + result = concat([df1, df3], axis=1) exp_idx = DatetimeIndex( [ @@ -104,9 +104,7 @@ def test_concat_datetime_timezone(self): tm.assert_frame_equal(result, expected) # GH 13783: Concat after resample - result = pd.concat( - [df1.resample("H").mean(), df2.resample("H").mean()], sort=True - ) + result = concat([df1.resample("H").mean(), df2.resample("H").mean()], sort=True) expected = DataFrame( {"a": [1, 2, 3] + [np.nan] * 3, "b": [np.nan] * 3 + [1, 2, 3]}, index=idx1.append(idx1), @@ -116,14 +114,14 @@ def test_concat_datetime_timezone(self): def test_concat_datetimeindex_freq(self): # GH 3232 # Monotonic index result - dr = pd.date_range("01-Jan-2013", periods=100, freq="50L", tz="UTC") + dr = date_range("01-Jan-2013", periods=100, freq="50L", tz="UTC") data = list(range(100)) expected = DataFrame(data, index=dr) - result = pd.concat([expected[:50], expected[50:]]) + result = concat([expected[:50], expected[50:]]) tm.assert_frame_equal(result, expected) # Non-monotonic index result - result = pd.concat([expected[50:], expected[:50]]) + result = concat([expected[50:], expected[:50]]) expected = DataFrame(data[50:] + data[:50], index=dr[50:].append(dr[:50])) expected.index._data.freq = None tm.assert_frame_equal(result, expected) @@ -179,21 +177,21 @@ def test_concat_NaT_series(self): # all NaT with tz expected = Series(pd.NaT, index=range(4), dtype="datetime64[ns, US/Eastern]") - result = pd.concat([y, y], ignore_index=True) + result = concat([y, y], ignore_index=True) tm.assert_series_equal(result, expected) # without tz - x = Series(pd.date_range("20151124 08:00", "20151124 09:00", freq="1h")) - y = Series(pd.date_range("20151124 10:00", "20151124 11:00", freq="1h")) + x = Series(date_range("20151124 08:00", "20151124 09:00", freq="1h")) + y = Series(date_range("20151124 10:00", "20151124 11:00", freq="1h")) y[:] = pd.NaT expected = Series([x[0], x[1], pd.NaT, pd.NaT]) - result = pd.concat([x, y], ignore_index=True) + result = concat([x, y], ignore_index=True) tm.assert_series_equal(result, expected) # all NaT without tz x[:] = pd.NaT expected = Series(pd.NaT, index=range(4), dtype="datetime64[ns]") - result = pd.concat([x, y], ignore_index=True) + result = concat([x, y], ignore_index=True) tm.assert_series_equal(result, expected) @pytest.mark.parametrize("tz", [None, "UTC"]) @@ -215,7 +213,7 @@ def test_concat_NaT_dataframes(self, tz): ] ) - result = pd.concat([first, second], axis=0) + result = concat([first, second], axis=0) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("tz1", [None, "UTC"]) @@ -228,7 +226,7 @@ def test_concat_NaT_dataframes_all_NaT_axis_0(self, tz1, tz2, s): first = DataFrame([[pd.NaT], [pd.NaT]]).apply(lambda x: x.dt.tz_localize(tz1)) second = DataFrame([s]).apply(lambda x: x.dt.tz_localize(tz2)) - result = pd.concat([first, second], axis=0) + result = concat([first, second], axis=0) expected = DataFrame(Series([pd.NaT, pd.NaT, s], index=[0, 1, 0])) expected = expected.apply(lambda x: x.dt.tz_localize(tz2)) if tz1 != tz2: @@ -249,7 +247,7 @@ def test_concat_NaT_dataframes_all_NaT_axis_1(self, tz1, tz2): 1: Series([pd.NaT, pd.NaT]).dt.tz_localize(tz2), } ) - result = pd.concat([first, second], axis=1) + result = concat([first, second], axis=1) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("tz1", [None, "UTC"]) @@ -278,7 +276,7 @@ def test_concat_NaT_series_dataframe_all_NaT(self, tz1, tz2): if tz1 != tz2: expected = expected.astype(object) - result = pd.concat([first, second]) + result = concat([first, second]) tm.assert_frame_equal(result, expected) @@ -306,7 +304,7 @@ def test_concat_tz_series(self): second = DataFrame([[datetime(2016, 1, 2)]]) second[0] = second[0].dt.tz_localize("UTC") - result = pd.concat([first, second]) + result = concat([first, second]) assert result[0].dtype == "datetime64[ns, UTC]" # Concatenating two London times @@ -316,7 +314,7 @@ def test_concat_tz_series(self): second = DataFrame([[datetime(2016, 1, 2)]]) second[0] = second[0].dt.tz_localize("Europe/London") - result = pd.concat([first, second]) + result = concat([first, second]) assert result[0].dtype == "datetime64[ns, Europe/London]" # Concatenating 2+1 London times @@ -326,7 +324,7 @@ def test_concat_tz_series(self): second = DataFrame([[datetime(2016, 1, 3)]]) second[0] = second[0].dt.tz_localize("Europe/London") - result = pd.concat([first, second]) + result = concat([first, second]) assert result[0].dtype == "datetime64[ns, Europe/London]" # Concat'ing 1+2 London times @@ -336,7 +334,7 @@ def test_concat_tz_series(self): second = DataFrame([[datetime(2016, 1, 2)], [datetime(2016, 1, 3)]]) second[0] = second[0].dt.tz_localize("Europe/London") - result = pd.concat([first, second]) + result = concat([first, second]) assert result[0].dtype == "datetime64[ns, Europe/London]" def test_concat_tz_series_tzlocal(self): @@ -379,7 +377,7 @@ def test_concat_tz_frame(self): ) # concat - df3 = pd.concat([df2.A.to_frame(), df2.B.to_frame()], axis=1) + df3 = concat([df2.A.to_frame(), df2.B.to_frame()], axis=1) tm.assert_frame_equal(df2, df3) def test_concat_multiple_tzs(self): @@ -393,15 +391,15 @@ def test_concat_multiple_tzs(self): df2 = DataFrame({"time": [ts2]}) df3 = DataFrame({"time": [ts3]}) - results = pd.concat([df1, df2]).reset_index(drop=True) + results = concat([df1, df2]).reset_index(drop=True) expected = DataFrame({"time": [ts1, ts2]}, dtype=object) tm.assert_frame_equal(results, expected) - results = pd.concat([df1, df3]).reset_index(drop=True) + results = concat([df1, df3]).reset_index(drop=True) expected = DataFrame({"time": [ts1, ts3]}, dtype=object) tm.assert_frame_equal(results, expected) - results = pd.concat([df2, df3]).reset_index(drop=True) + results = concat([df2, df3]).reset_index(drop=True) expected = DataFrame({"time": [ts2, ts3]}) tm.assert_frame_equal(results, expected) @@ -439,7 +437,7 @@ def test_concat_tz_not_aligned(self): ts = pd.to_datetime([1, 2]).tz_localize("UTC") a = DataFrame({"A": ts}) b = DataFrame({"A": ts, "B": ts}) - result = pd.concat([a, b], sort=True, ignore_index=True) + result = concat([a, b], sort=True, ignore_index=True) expected = DataFrame( {"A": list(ts) + list(ts), "B": [pd.NaT, pd.NaT] + list(ts)} ) @@ -467,7 +465,7 @@ def test_concat_tz_NaT(self, t1): df1 = DataFrame([[ts1, ts2]]) df2 = DataFrame([[ts3]]) - result = pd.concat([df1, df2]) + result = concat([df1, df2]) expected = DataFrame([[ts1, ts2], [ts3, pd.NaT]], index=[0, 0]) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/reshape/concat/test_empty.py b/pandas/tests/reshape/concat/test_empty.py index dea04e98088e8..87816cc574aaf 100644 --- a/pandas/tests/reshape/concat/test_empty.py +++ b/pandas/tests/reshape/concat/test_empty.py @@ -43,7 +43,7 @@ def test_concat_empty_series(self): # GH 11082 s1 = Series([1, 2, 3], name="x") s2 = Series(name="y", dtype="float64") - res = pd.concat([s1, s2], axis=1) + res = concat([s1, s2], axis=1) exp = DataFrame( {"x": [1, 2, 3], "y": [np.nan, np.nan, np.nan]}, index=Index([0, 1, 2], dtype="O"), @@ -52,7 +52,7 @@ def test_concat_empty_series(self): s1 = Series([1, 2, 3], name="x") s2 = Series(name="y", dtype="float64") - res = pd.concat([s1, s2], axis=0) + res = concat([s1, s2], axis=0) # name will be reset exp = Series([1, 2, 3]) tm.assert_series_equal(res, exp) @@ -60,7 +60,7 @@ def test_concat_empty_series(self): # empty Series with no name s1 = Series([1, 2, 3], name="x") s2 = Series(name=None, dtype="float64") - res = pd.concat([s1, s2], axis=1) + res = concat([s1, s2], axis=1) exp = DataFrame( {"x": [1, 2, 3], 0: [np.nan, np.nan, np.nan]}, columns=["x", 0], @@ -103,7 +103,7 @@ def test_concat_empty_series_timelike(self, tz, values): ], ) def test_concat_empty_series_dtypes(self, left, right, expected): - result = pd.concat([Series(dtype=left), Series(dtype=right)]) + result = concat([Series(dtype=left), Series(dtype=right)]) assert result.dtype == expected @pytest.mark.parametrize( @@ -112,10 +112,10 @@ def test_concat_empty_series_dtypes(self, left, right, expected): def test_concat_empty_series_dtypes_match_roundtrips(self, dtype): dtype = np.dtype(dtype) - result = pd.concat([Series(dtype=dtype)]) + result = concat([Series(dtype=dtype)]) assert result.dtype == dtype - result = pd.concat([Series(dtype=dtype), Series(dtype=dtype)]) + result = concat([Series(dtype=dtype), Series(dtype=dtype)]) assert result.dtype == dtype def test_concat_empty_series_dtypes_roundtrips(self): @@ -158,13 +158,13 @@ def get_result_type(dtype, dtype2): continue expected = get_result_type(dtype, dtype2) - result = pd.concat([Series(dtype=dtype), Series(dtype=dtype2)]).dtype + result = concat([Series(dtype=dtype), Series(dtype=dtype2)]).dtype assert result.kind == expected def test_concat_empty_series_dtypes_triple(self): assert ( - pd.concat( + concat( [Series(dtype="M8[ns]"), Series(dtype=np.bool_), Series(dtype=np.int64)] ).dtype == np.object_ @@ -173,14 +173,14 @@ def test_concat_empty_series_dtypes_triple(self): def test_concat_empty_series_dtype_category_with_array(self): # GH#18515 assert ( - pd.concat( + concat( [Series(np.array([]), dtype="category"), Series(dtype="float64")] ).dtype == "float64" ) def test_concat_empty_series_dtypes_sparse(self): - result = pd.concat( + result = concat( [ Series(dtype="float64").astype("Sparse"), Series(dtype="float64").astype("Sparse"), @@ -188,14 +188,14 @@ def test_concat_empty_series_dtypes_sparse(self): ) assert result.dtype == "Sparse[float64]" - result = pd.concat( + result = concat( [Series(dtype="float64").astype("Sparse"), Series(dtype="float64")] ) # TODO: release-note: concat sparse dtype expected = pd.SparseDtype(np.float64) assert result.dtype == expected - result = pd.concat( + result = concat( [Series(dtype="float64").astype("Sparse"), Series(dtype="object")] ) # TODO: release-note: concat sparse dtype @@ -208,7 +208,7 @@ def test_concat_empty_df_object_dtype(self, dtype): df_1 = DataFrame({"Row": [0, 1, 1], "EmptyCol": np.nan, "NumberCol": [1, 2, 3]}) df_1["Row"] = df_1["Row"].astype(dtype) df_2 = DataFrame(columns=df_1.columns) - result = pd.concat([df_1, df_2], axis=0) + result = concat([df_1, df_2], axis=0) expected = df_1.copy() expected["EmptyCol"] = expected["EmptyCol"].astype(object) # TODO: why? tm.assert_frame_equal(result, expected) @@ -219,12 +219,12 @@ def test_concat_empty_dataframe_dtypes(self): df["b"] = df["b"].astype(np.int32) df["c"] = df["c"].astype(np.float64) - result = pd.concat([df, df]) + result = concat([df, df]) assert result["a"].dtype == np.bool_ assert result["b"].dtype == np.int32 assert result["c"].dtype == np.float64 - result = pd.concat([df, df.astype(np.float64)]) + result = concat([df, df.astype(np.float64)]) assert result["a"].dtype == np.object_ assert result["b"].dtype == np.float64 assert result["c"].dtype == np.float64 @@ -236,7 +236,7 @@ def test_concat_inner_join_empty(self): df_expected = DataFrame({"a": []}, index=[], dtype="int64") for how, expected in [("inner", df_expected), ("outer", df_a)]: - result = pd.concat([df_a, df_empty], axis=1, join=how) + result = concat([df_a, df_empty], axis=1, join=how) tm.assert_frame_equal(result, expected) def test_empty_dtype_coerce(self): diff --git a/pandas/tests/reshape/concat/test_index.py b/pandas/tests/reshape/concat/test_index.py index 3fc886893b55a..06c819af27499 100644 --- a/pandas/tests/reshape/concat/test_index.py +++ b/pandas/tests/reshape/concat/test_index.py @@ -54,7 +54,7 @@ def test_concat_same_index_names(self, name_in1, name_in2, name_in3, name_out): frames = [ DataFrame({c: [0, 1, 2]}, index=i) for i, c in zip(indices, ["x", "y", "z"]) ] - result = pd.concat(frames, axis=1) + result = concat(frames, axis=1) exp_ind = Index(["a", "b", "c", "d", "e"], name=name_out) expected = DataFrame( @@ -107,7 +107,7 @@ def test_default_index(self): # is_series and ignore_index s1 = Series([1, 2, 3], name="x") s2 = Series([4, 5, 6], name="y") - res = pd.concat([s1, s2], axis=1, ignore_index=True) + res = concat([s1, s2], axis=1, ignore_index=True) assert isinstance(res.columns, pd.RangeIndex) exp = DataFrame([[1, 4], [2, 5], [3, 6]]) # use check_index_type=True to check the result have @@ -117,7 +117,7 @@ def test_default_index(self): # is_series and all inputs have no names s1 = Series([1, 2, 3]) s2 = Series([4, 5, 6]) - res = pd.concat([s1, s2], axis=1, ignore_index=False) + res = concat([s1, s2], axis=1, ignore_index=False) assert isinstance(res.columns, pd.RangeIndex) exp = DataFrame([[1, 4], [2, 5], [3, 6]]) exp.columns = pd.RangeIndex(2) @@ -127,11 +127,11 @@ def test_default_index(self): df1 = DataFrame({"A": [1, 2], "B": [5, 6]}) df2 = DataFrame({"A": [3, 4], "B": [7, 8]}) - res = pd.concat([df1, df2], axis=0, ignore_index=True) + res = concat([df1, df2], axis=0, ignore_index=True) exp = DataFrame([[1, 5], [2, 6], [3, 7], [4, 8]], columns=["A", "B"]) tm.assert_frame_equal(res, exp, check_index_type=True, check_column_type=True) - res = pd.concat([df1, df2], axis=1, ignore_index=True) + res = concat([df1, df2], axis=1, ignore_index=True) exp = DataFrame([[1, 5, 3, 7], [2, 6, 4, 8]]) tm.assert_frame_equal(res, exp, check_index_type=True, check_column_type=True) @@ -255,7 +255,7 @@ def test_concat_multiindex_dfs_with_deepcopy(self): names=["testname", None, None], ) expected = DataFrame([[0], [1]], index=expected_index) - result_copy = pd.concat(deepcopy(example_dict), names=["testname"]) + result_copy = concat(deepcopy(example_dict), names=["testname"]) tm.assert_frame_equal(result_copy, expected) - result_no_copy = pd.concat(example_dict, names=["testname"]) + result_no_copy = concat(example_dict, names=["testname"]) tm.assert_frame_equal(result_no_copy, expected) diff --git a/pandas/tests/reshape/concat/test_series.py b/pandas/tests/reshape/concat/test_series.py index 2d681e792914c..9b114c3f195f0 100644 --- a/pandas/tests/reshape/concat/test_series.py +++ b/pandas/tests/reshape/concat/test_series.py @@ -1,7 +1,6 @@ import numpy as np import pytest -import pandas as pd from pandas import ( DataFrame, DatetimeIndex, @@ -48,7 +47,7 @@ def test_concat_empty_and_non_empty_series_regression(self): s2 = Series([], dtype=object) expected = s1 - result = pd.concat([s1, s2]) + result = concat([s1, s2]) tm.assert_series_equal(result, expected) def test_concat_series_axis1(self, sort=sort): @@ -117,7 +116,7 @@ def test_concat_series_name_npscalar_tuple(self, s1name, s2name): # GH21015 s1 = Series({"a": 1, "b": 2}, name=s1name) s2 = Series({"c": 5, "d": 6}, name=s2name) - result = pd.concat([s1, s2]) + result = concat([s1, s2]) expected = Series({"a": 1, "b": 2, "c": 5, "d": 6}) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/reshape/merge/test_merge.py b/pandas/tests/reshape/merge/test_merge.py index d430856776269..74f252d5b81e8 100644 --- a/pandas/tests/reshape/merge/test_merge.py +++ b/pandas/tests/reshape/merge/test_merge.py @@ -124,7 +124,7 @@ def test_merge_inner_join_empty(self): # GH 15328 df_empty = DataFrame() df_a = DataFrame({"a": [1, 2]}, index=[0, 1], dtype="int64") - result = pd.merge(df_empty, df_a, left_index=True, right_index=True) + result = merge(df_empty, df_a, left_index=True, right_index=True) expected = DataFrame({"a": []}, index=[], dtype="int64") tm.assert_frame_equal(result, expected) @@ -142,7 +142,7 @@ def test_merge_non_string_columns(self): right = left.astype(float) expected = left - result = pd.merge(left, right) + result = merge(left, right) tm.assert_frame_equal(expected, result) def test_merge_index_as_on_arg(self): @@ -439,7 +439,7 @@ def test_merge_left_empty_right_empty(self, join_type, kwarg): dtype=object, ) - result = pd.merge(left, right, how=join_type, **kwarg) + result = merge(left, right, how=join_type, **kwarg) tm.assert_frame_equal(result, exp_in) def test_merge_left_empty_right_notempty(self): @@ -463,15 +463,15 @@ def test_merge_left_empty_right_notempty(self): exp_in.index = exp_in.index.astype(object) def check1(exp, kwarg): - result = pd.merge(left, right, how="inner", **kwarg) + result = merge(left, right, how="inner", **kwarg) tm.assert_frame_equal(result, exp) - result = pd.merge(left, right, how="left", **kwarg) + result = merge(left, right, how="left", **kwarg) tm.assert_frame_equal(result, exp) def check2(exp, kwarg): - result = pd.merge(left, right, how="right", **kwarg) + result = merge(left, right, how="right", **kwarg) tm.assert_frame_equal(result, exp) - result = pd.merge(left, right, how="outer", **kwarg) + result = merge(left, right, how="outer", **kwarg) tm.assert_frame_equal(result, exp) for kwarg in [ @@ -512,15 +512,15 @@ def test_merge_left_notempty_right_empty(self): exp_in.index = exp_in.index.astype(object) def check1(exp, kwarg): - result = pd.merge(left, right, how="inner", **kwarg) + result = merge(left, right, how="inner", **kwarg) tm.assert_frame_equal(result, exp) - result = pd.merge(left, right, how="right", **kwarg) + result = merge(left, right, how="right", **kwarg) tm.assert_frame_equal(result, exp) def check2(exp, kwarg): - result = pd.merge(left, right, how="left", **kwarg) + result = merge(left, right, how="left", **kwarg) tm.assert_frame_equal(result, exp) - result = pd.merge(left, right, how="outer", **kwarg) + result = merge(left, right, how="outer", **kwarg) tm.assert_frame_equal(result, exp) for kwarg in [ @@ -780,7 +780,7 @@ def test_merge_on_datetime64tz(self): "value_y": [np.nan, 1, 2, 3], } ) - result = pd.merge(left, right, on="key", how="outer") + result = merge(left, right, on="key", how="outer") tm.assert_frame_equal(result, expected) left = DataFrame( @@ -804,7 +804,7 @@ def test_merge_on_datetime64tz(self): + list(pd.date_range("20151011", periods=2, tz="US/Eastern")), } ) - result = pd.merge(left, right, on="key", how="outer") + result = merge(left, right, on="key", how="outer") tm.assert_frame_equal(result, expected) assert result["value_x"].dtype == "datetime64[ns, US/Eastern]" assert result["value_y"].dtype == "datetime64[ns, US/Eastern]" @@ -854,7 +854,7 @@ def test_merge_datetime64tz_with_dst_transition(self): } ) df2["date"] = df2["date"].dt.tz_localize("UTC").dt.tz_convert("Europe/Madrid") - result = pd.merge(df1, df2, how="outer", on="date") + result = merge(df1, df2, how="outer", on="date") expected = DataFrame( { "date": pd.date_range( @@ -897,7 +897,7 @@ def test_merge_on_periods(self): "value_y": [np.nan, 1, 2, 3], } ) - result = pd.merge(left, right, on="key", how="outer") + result = merge(left, right, on="key", how="outer") tm.assert_frame_equal(result, expected) left = DataFrame( @@ -916,7 +916,7 @@ def test_merge_on_periods(self): "value_y": [pd.NaT] + list(exp_y), } ) - result = pd.merge(left, right, on="key", how="outer") + result = merge(left, right, on="key", how="outer") tm.assert_frame_equal(result, expected) assert result["value_x"].dtype == "Period[D]" assert result["value_y"].dtype == "Period[D]" @@ -1295,7 +1295,7 @@ def test_merge_two_empty_df_no_division_error(self): ) def test_merge_on_index_with_more_values(self, how, index, expected_index): # GH 24212 - # pd.merge gets [0, 1, 2, -1, -1, -1] as left_indexer, ensure that + # merge gets [0, 1, 2, -1, -1, -1] as left_indexer, ensure that # -1 is interpreted as a missing value instead of the last element df1 = DataFrame({"a": [0, 1, 2], "key": [0, 1, 2]}, index=index) df2 = DataFrame({"b": [0, 1, 2, 3, 4, 5]}) @@ -1407,7 +1407,7 @@ def test_different(self, right_vals): # GH 9780 # We allow merging on object and categorical cols and cast # categorical cols to object - result = pd.merge(left, right, on="A") + result = merge(left, right, on="A") assert is_object_dtype(result.A.dtype) @pytest.mark.parametrize("d1", [np.int64, np.int32, np.int16, np.int8, np.uint8]) @@ -1507,9 +1507,9 @@ def test_merge_incompat_infer_boolean_object(self): df2 = DataFrame({"key": [True, False]}) expected = DataFrame({"key": [True, False]}, dtype=object) - result = pd.merge(df1, df2, on="key") + result = merge(df1, df2, on="key") tm.assert_frame_equal(result, expected) - result = pd.merge(df2, df1, on="key") + result = merge(df2, df1, on="key") tm.assert_frame_equal(result, expected) # with missing value @@ -1517,9 +1517,9 @@ def test_merge_incompat_infer_boolean_object(self): df2 = DataFrame({"key": [True, False]}) expected = DataFrame({"key": [True, False]}, dtype=object) - result = pd.merge(df1, df2, on="key") + result = merge(df1, df2, on="key") tm.assert_frame_equal(result, expected) - result = pd.merge(df2, df1, on="key") + result = merge(df2, df1, on="key") tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( @@ -1541,9 +1541,9 @@ def test_merge_incompat_dtypes_are_ok(self, df1_vals, df2_vals): df1 = DataFrame({"A": df1_vals}) df2 = DataFrame({"A": df2_vals}) - result = pd.merge(df1, df2, on=["A"]) + result = merge(df1, df2, on=["A"]) assert is_object_dtype(result.A.dtype) - result = pd.merge(df2, df1, on=["A"]) + result = merge(df2, df1, on=["A"]) assert is_object_dtype(result.A.dtype) @pytest.mark.parametrize( @@ -1582,7 +1582,7 @@ def test_merge_incompat_dtypes_error(self, df1_vals, df2_vals): ) msg = re.escape(msg) with pytest.raises(ValueError, match=msg): - pd.merge(df1, df2, on=["A"]) + merge(df1, df2, on=["A"]) # Check that error still raised when swapping order of dataframes msg = ( @@ -1592,7 +1592,7 @@ def test_merge_incompat_dtypes_error(self, df1_vals, df2_vals): ) msg = re.escape(msg) with pytest.raises(ValueError, match=msg): - pd.merge(df2, df1, on=["A"]) + merge(df2, df1, on=["A"]) @pytest.fixture @@ -1619,7 +1619,7 @@ def right(): class TestMergeCategorical: def test_identical(self, left): # merging on the same, should preserve dtypes - merged = pd.merge(left, left, on="X") + merged = merge(left, left, on="X") result = merged.dtypes.sort_index() expected = Series( [CategoricalDtype(categories=["foo", "bar"]), np.dtype("O"), np.dtype("O")], @@ -1630,7 +1630,7 @@ def test_identical(self, left): def test_basic(self, left, right): # we have matching Categorical dtypes in X # so should preserve the merged column - merged = pd.merge(left, right, on="X") + merged = merge(left, right, on="X") result = merged.dtypes.sort_index() expected = Series( [ @@ -1657,7 +1657,7 @@ def test_merge_categorical(self): "b": {0: "g", 1: "g", 2: "g", 3: "g", 4: "g"}, } ) - df = pd.merge(left, right, how="left", left_on="b", right_on="c") + df = merge(left, right, how="left", left_on="b", right_on="c") # object-object expected = df.copy() @@ -1667,14 +1667,14 @@ def test_merge_categorical(self): # because we don't have any matching rows cright = right.copy() cright["d"] = cright["d"].astype("category") - result = pd.merge(left, cright, how="left", left_on="b", right_on="c") + result = merge(left, cright, how="left", left_on="b", right_on="c") expected["d"] = expected["d"].astype(CategoricalDtype(["null"])) tm.assert_frame_equal(result, expected) # cat-object cleft = left.copy() cleft["b"] = cleft["b"].astype("category") - result = pd.merge(cleft, cright, how="left", left_on="b", right_on="c") + result = merge(cleft, cright, how="left", left_on="b", right_on="c") tm.assert_frame_equal(result, expected) # cat-cat @@ -1682,7 +1682,7 @@ def test_merge_categorical(self): cright["d"] = cright["d"].astype("category") cleft = left.copy() cleft["b"] = cleft["b"].astype("category") - result = pd.merge(cleft, cright, how="left", left_on="b", right_on="c") + result = merge(cleft, cright, how="left", left_on="b", right_on="c") tm.assert_frame_equal(result, expected) def tests_merge_categorical_unordered_equal(self): @@ -1700,7 +1700,7 @@ def tests_merge_categorical_unordered_equal(self): "Right": ["C1", "B1", "A1"], } ) - result = pd.merge(df1, df2, on=["Foo"]) + result = merge(df1, df2, on=["Foo"]) expected = DataFrame( { "Foo": Categorical(["A", "B", "C"]), @@ -1714,7 +1714,7 @@ def test_other_columns(self, left, right): # non-merge columns should preserve if possible right = right.assign(Z=right.Z.astype("category")) - merged = pd.merge(left, right, on="X") + merged = merge(left, right, on="X") result = merged.dtypes.sort_index() expected = Series( [ @@ -1747,7 +1747,7 @@ def test_dtype_on_merged_different(self, change, join_type, left, right): assert is_categorical_dtype(left.X.values.dtype) # assert not left.X.values._categories_match_up_to_permutation(right.X.values) - merged = pd.merge(left, right, on="X", how=join_type) + merged = merge(left, right, on="X", how=join_type) result = merged.dtypes.sort_index() expected = Series( @@ -1791,7 +1791,7 @@ def test_self_join_multiple_categories(self): df = df.apply(lambda x: x.astype("category")) # self-join should equal ourselves - result = pd.merge(df, df, on=list(df.columns)) + result = merge(df, df, on=list(df.columns)) tm.assert_frame_equal(result, df) @@ -1817,14 +1817,14 @@ def test_dtype_on_categorical_dates(self): ], columns=["date", "num2", "num4"], ) - result_outer = pd.merge(df, df2, how="outer", on=["date"]) + result_outer = merge(df, df2, how="outer", on=["date"]) tm.assert_frame_equal(result_outer, expected_outer) expected_inner = DataFrame( [[pd.Timestamp("2001-01-01").date(), 1.1, 1.3]], columns=["date", "num2", "num4"], ) - result_inner = pd.merge(df, df2, how="inner", on=["date"]) + result_inner = merge(df, df2, how="inner", on=["date"]) tm.assert_frame_equal(result_inner, expected_inner) @pytest.mark.parametrize("ordered", [True, False]) @@ -1852,7 +1852,7 @@ def test_merging_with_bool_or_int_cateorical_column( def test_merge_on_int_array(self): # GH 23020 df = DataFrame({"A": Series([1, 2, np.nan], dtype="Int64"), "B": 1}) - result = pd.merge(df, df, on="A") + result = merge(df, df, on="A") expected = DataFrame( {"A": Series([1, 2, np.nan], dtype="Int64"), "B_x": 1, "B_y": 1} ) @@ -1918,7 +1918,7 @@ class TestMergeOnIndexes: ], ) def test_merge_on_indexes(self, left_df, right_df, how, sort, expected): - result = pd.merge( + result = merge( left_df, right_df, left_index=True, right_index=True, how=how, sort=sort ) tm.assert_frame_equal(result, expected) @@ -1981,7 +1981,7 @@ def test_merge_series(on, left_on, right_on, left_index, right_index, nm): index=pd.MultiIndex.from_product([["a", "b"], [1]], names=["outer", "inner"]), ) if nm is not None: - result = pd.merge( + result = merge( a, b, on=on, @@ -1994,7 +1994,7 @@ def test_merge_series(on, left_on, right_on, left_index, right_index, nm): else: msg = "Cannot merge a Series without a name" with pytest.raises(ValueError, match=msg): - result = pd.merge( + result = merge( a, b, on=on, @@ -2033,7 +2033,7 @@ def test_merge_suffix(col1, col2, kwargs, expected_cols): result = a.merge(b, left_index=True, right_index=True, **kwargs) tm.assert_frame_equal(result, expected) - result = pd.merge(a, b, left_index=True, right_index=True, **kwargs) + result = merge(a, b, left_index=True, right_index=True, **kwargs) tm.assert_frame_equal(result, expected) @@ -2079,7 +2079,7 @@ def test_merge_suffix_error(col1, col2, suffixes): # TODO: might reconsider current raise behaviour, see issue 24782 msg = "columns overlap but no suffix specified" with pytest.raises(ValueError, match=msg): - pd.merge(a, b, left_index=True, right_index=True, suffixes=suffixes) + merge(a, b, left_index=True, right_index=True, suffixes=suffixes) @pytest.mark.parametrize("suffixes", [{"left", "right"}, {"left": 0, "right": 0}]) @@ -2088,7 +2088,7 @@ def test_merge_suffix_warns(suffixes): b = DataFrame({"b": [3, 4, 5]}) with tm.assert_produces_warning(FutureWarning): - pd.merge(a, b, left_index=True, right_index=True, suffixes={"left", "right"}) + merge(a, b, left_index=True, right_index=True, suffixes={"left", "right"}) @pytest.mark.parametrize( @@ -2103,7 +2103,7 @@ def test_merge_suffix_length_error(col1, col2, suffixes, msg): b = DataFrame({col2: [3, 4, 5]}) with pytest.raises(ValueError, match=msg): - pd.merge(a, b, left_index=True, right_index=True, suffixes=suffixes) + merge(a, b, left_index=True, right_index=True, suffixes=suffixes) @pytest.mark.parametrize("cat_dtype", ["one", "two"]) @@ -2173,7 +2173,7 @@ def test_merge_on_cat_and_ext_array(): left = right.copy() left["a"] = left["a"].astype("category") - result = pd.merge(left, right, how="inner", on="a") + result = merge(left, right, how="inner", on="a") expected = right.copy() tm.assert_frame_equal(result, expected) @@ -2217,7 +2217,7 @@ def test_merge_datetime_upcast_dtype(): df2 = DataFrame( {"y": ["1", "2", "3"], "z": pd.to_datetime(["2000", "2001", "2002"])} ) - result = pd.merge(df1, df2, how="left", on="y") + result = merge(df1, df2, how="left", on="y") expected = DataFrame( { "x": ["a", "b", "c"], @@ -2364,7 +2364,7 @@ def test_merge_right_left_index(): # GH#38616 left = DataFrame({"x": [1, 1], "z": ["foo", "foo"]}) right = DataFrame({"x": [1, 1], "z": ["foo", "foo"]}) - result = pd.merge(left, right, how="right", left_index=True, right_on="x") + result = merge(left, right, how="right", left_index=True, right_on="x") expected = DataFrame( { "x": [1, 1], diff --git a/pandas/tests/reshape/merge/test_merge_asof.py b/pandas/tests/reshape/merge/test_merge_asof.py index 5cb7bdd603517..8e61759774718 100644 --- a/pandas/tests/reshape/merge/test_merge_asof.py +++ b/pandas/tests/reshape/merge/test_merge_asof.py @@ -42,14 +42,14 @@ def test_examples1(self): {"a": [1, 5, 10], "left_val": ["a", "b", "c"], "right_val": [1, 3, 7]} ) - result = pd.merge_asof(left, right, on="a") + result = merge_asof(left, right, on="a") tm.assert_frame_equal(result, expected) def test_examples2(self): """ doc-string examples """ trades = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( [ "20160525 13:30:00.023", "20160525 13:30:00.038", @@ -67,7 +67,7 @@ def test_examples2(self): quotes = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( [ "20160525 13:30:00.023", "20160525 13:30:00.023", @@ -95,15 +95,13 @@ def test_examples2(self): columns=["time", "ticker", "bid", "ask"], ) - pd.merge_asof(trades, quotes, on="time", by="ticker") + merge_asof(trades, quotes, on="time", by="ticker") - pd.merge_asof( - trades, quotes, on="time", by="ticker", tolerance=Timedelta("2ms") - ) + merge_asof(trades, quotes, on="time", by="ticker", tolerance=Timedelta("2ms")) expected = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( [ "20160525 13:30:00.023", "20160525 13:30:00.038", @@ -121,7 +119,7 @@ def test_examples2(self): columns=["time", "ticker", "price", "quantity", "bid", "ask"], ) - result = pd.merge_asof( + result = merge_asof( trades, quotes, on="time", @@ -142,7 +140,7 @@ def test_examples3(self): {"a": [1, 5, 10], "left_val": ["a", "b", "c"], "right_val": [1, 6, np.nan]} ) - result = pd.merge_asof(left, right, on="a", direction="forward") + result = merge_asof(left, right, on="a", direction="forward") tm.assert_frame_equal(result, expected) def test_examples4(self): @@ -156,7 +154,7 @@ def test_examples4(self): {"a": [1, 5, 10], "left_val": ["a", "b", "c"], "right_val": [1, 6, 7]} ) - result = pd.merge_asof(left, right, on="a", direction="nearest") + result = merge_asof(left, right, on="a", direction="nearest") tm.assert_frame_equal(result, expected) def test_basic(self): @@ -277,7 +275,7 @@ def test_multiby(self): # GH13936 trades = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( [ "20160525 13:30:00.023", "20160525 13:30:00.023", @@ -296,7 +294,7 @@ def test_multiby(self): quotes = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( [ "20160525 13:30:00.023", "20160525 13:30:00.023", @@ -316,7 +314,7 @@ def test_multiby(self): expected = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( [ "20160525 13:30:00.023", "20160525 13:30:00.023", @@ -335,14 +333,14 @@ def test_multiby(self): columns=["time", "ticker", "exch", "price", "quantity", "bid", "ask"], ) - result = pd.merge_asof(trades, quotes, on="time", by=["ticker", "exch"]) + result = merge_asof(trades, quotes, on="time", by=["ticker", "exch"]) tm.assert_frame_equal(result, expected) def test_multiby_heterogeneous_types(self): # GH13936 trades = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( [ "20160525 13:30:00.023", "20160525 13:30:00.023", @@ -361,7 +359,7 @@ def test_multiby_heterogeneous_types(self): quotes = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( [ "20160525 13:30:00.023", "20160525 13:30:00.023", @@ -381,7 +379,7 @@ def test_multiby_heterogeneous_types(self): expected = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( [ "20160525 13:30:00.023", "20160525 13:30:00.023", @@ -400,42 +398,42 @@ def test_multiby_heterogeneous_types(self): columns=["time", "ticker", "exch", "price", "quantity", "bid", "ask"], ) - result = pd.merge_asof(trades, quotes, on="time", by=["ticker", "exch"]) + result = merge_asof(trades, quotes, on="time", by=["ticker", "exch"]) tm.assert_frame_equal(result, expected) def test_multiby_indexed(self): # GH15676 left = pd.DataFrame( [ - [pd.to_datetime("20160602"), 1, "a"], - [pd.to_datetime("20160602"), 2, "a"], - [pd.to_datetime("20160603"), 1, "b"], - [pd.to_datetime("20160603"), 2, "b"], + [to_datetime("20160602"), 1, "a"], + [to_datetime("20160602"), 2, "a"], + [to_datetime("20160603"), 1, "b"], + [to_datetime("20160603"), 2, "b"], ], columns=["time", "k1", "k2"], ).set_index("time") right = pd.DataFrame( [ - [pd.to_datetime("20160502"), 1, "a", 1.0], - [pd.to_datetime("20160502"), 2, "a", 2.0], - [pd.to_datetime("20160503"), 1, "b", 3.0], - [pd.to_datetime("20160503"), 2, "b", 4.0], + [to_datetime("20160502"), 1, "a", 1.0], + [to_datetime("20160502"), 2, "a", 2.0], + [to_datetime("20160503"), 1, "b", 3.0], + [to_datetime("20160503"), 2, "b", 4.0], ], columns=["time", "k1", "k2", "value"], ).set_index("time") expected = pd.DataFrame( [ - [pd.to_datetime("20160602"), 1, "a", 1.0], - [pd.to_datetime("20160602"), 2, "a", 2.0], - [pd.to_datetime("20160603"), 1, "b", 3.0], - [pd.to_datetime("20160603"), 2, "b", 4.0], + [to_datetime("20160602"), 1, "a", 1.0], + [to_datetime("20160602"), 2, "a", 2.0], + [to_datetime("20160603"), 1, "b", 3.0], + [to_datetime("20160603"), 2, "b", 4.0], ], columns=["time", "k1", "k2", "value"], ).set_index("time") - result = pd.merge_asof( + result = merge_asof( left, right, left_index=True, right_index=True, by=["k1", "k2"] ) @@ -444,7 +442,7 @@ def test_multiby_indexed(self): with pytest.raises( MergeError, match="left_by and right_by must be same length" ): - pd.merge_asof( + merge_asof( left, right, left_index=True, @@ -624,7 +622,7 @@ def test_tolerance_forward(self): {"a": [1, 5, 10], "left_val": ["a", "b", "c"], "right_val": [1, np.nan, 11]} ) - result = pd.merge_asof(left, right, on="a", direction="forward", tolerance=1) + result = merge_asof(left, right, on="a", direction="forward", tolerance=1) tm.assert_frame_equal(result, expected) def test_tolerance_nearest(self): @@ -637,7 +635,7 @@ def test_tolerance_nearest(self): {"a": [1, 5, 10], "left_val": ["a", "b", "c"], "right_val": [1, np.nan, 11]} ) - result = pd.merge_asof(left, right, on="a", direction="nearest", tolerance=1) + result = merge_asof(left, right, on="a", direction="nearest", tolerance=1) tm.assert_frame_equal(result, expected) def test_tolerance_tz(self): @@ -645,7 +643,7 @@ def test_tolerance_tz(self): left = pd.DataFrame( { "date": pd.date_range( - start=pd.to_datetime("2016-01-02"), + start=to_datetime("2016-01-02"), freq="D", periods=5, tz=pytz.timezone("UTC"), @@ -656,7 +654,7 @@ def test_tolerance_tz(self): right = pd.DataFrame( { "date": pd.date_range( - start=pd.to_datetime("2016-01-01"), + start=to_datetime("2016-01-01"), freq="D", periods=5, tz=pytz.timezone("UTC"), @@ -664,12 +662,12 @@ def test_tolerance_tz(self): "value2": list("ABCDE"), } ) - result = pd.merge_asof(left, right, on="date", tolerance=Timedelta("1 day")) + result = merge_asof(left, right, on="date", tolerance=Timedelta("1 day")) expected = pd.DataFrame( { "date": pd.date_range( - start=pd.to_datetime("2016-01-02"), + start=to_datetime("2016-01-02"), freq="D", periods=5, tz=pytz.timezone("UTC"), @@ -695,7 +693,7 @@ def test_tolerance_float(self): } ) - result = pd.merge_asof(left, right, on="a", direction="nearest", tolerance=0.5) + result = merge_asof(left, right, on="a", direction="nearest", tolerance=0.5) tm.assert_frame_equal(result, expected) def test_index_tolerance(self): @@ -704,7 +702,7 @@ def test_index_tolerance(self): trades = self.trades.set_index("time") quotes = self.quotes.set_index("time") - result = pd.merge_asof( + result = merge_asof( trades, quotes, left_index=True, @@ -732,7 +730,7 @@ def test_allow_exact_matches_forward(self): {"a": [1, 5, 10], "left_val": ["a", "b", "c"], "right_val": [2, 7, 11]} ) - result = pd.merge_asof( + result = merge_asof( left, right, on="a", direction="forward", allow_exact_matches=False ) tm.assert_frame_equal(result, expected) @@ -747,7 +745,7 @@ def test_allow_exact_matches_nearest(self): {"a": [1, 5, 10], "left_val": ["a", "b", "c"], "right_val": [2, 3, 11]} ) - result = pd.merge_asof( + result = merge_asof( left, right, on="a", direction="nearest", allow_exact_matches=False ) tm.assert_frame_equal(result, expected) @@ -768,38 +766,38 @@ def test_allow_exact_matches_and_tolerance(self): def test_allow_exact_matches_and_tolerance2(self): # GH 13695 df1 = pd.DataFrame( - {"time": pd.to_datetime(["2016-07-15 13:30:00.030"]), "username": ["bob"]} + {"time": to_datetime(["2016-07-15 13:30:00.030"]), "username": ["bob"]} ) df2 = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( ["2016-07-15 13:30:00.000", "2016-07-15 13:30:00.030"] ), "version": [1, 2], } ) - result = pd.merge_asof(df1, df2, on="time") + result = merge_asof(df1, df2, on="time") expected = pd.DataFrame( { - "time": pd.to_datetime(["2016-07-15 13:30:00.030"]), + "time": to_datetime(["2016-07-15 13:30:00.030"]), "username": ["bob"], "version": [2], } ) tm.assert_frame_equal(result, expected) - result = pd.merge_asof(df1, df2, on="time", allow_exact_matches=False) + result = merge_asof(df1, df2, on="time", allow_exact_matches=False) expected = pd.DataFrame( { - "time": pd.to_datetime(["2016-07-15 13:30:00.030"]), + "time": to_datetime(["2016-07-15 13:30:00.030"]), "username": ["bob"], "version": [1], } ) tm.assert_frame_equal(result, expected) - result = pd.merge_asof( + result = merge_asof( df1, df2, on="time", @@ -808,7 +806,7 @@ def test_allow_exact_matches_and_tolerance2(self): ) expected = pd.DataFrame( { - "time": pd.to_datetime(["2016-07-15 13:30:00.030"]), + "time": to_datetime(["2016-07-15 13:30:00.030"]), "username": ["bob"], "version": [np.nan], } @@ -819,7 +817,7 @@ def test_allow_exact_matches_and_tolerance3(self): # GH 13709 df1 = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( ["2016-07-15 13:30:00.030", "2016-07-15 13:30:00.030"] ), "username": ["bob", "charlie"], @@ -827,14 +825,14 @@ def test_allow_exact_matches_and_tolerance3(self): ) df2 = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( ["2016-07-15 13:30:00.000", "2016-07-15 13:30:00.030"] ), "version": [1, 2], } ) - result = pd.merge_asof( + result = merge_asof( df1, df2, on="time", @@ -843,7 +841,7 @@ def test_allow_exact_matches_and_tolerance3(self): ) expected = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( ["2016-07-15 13:30:00.030", "2016-07-15 13:30:00.030"] ), "username": ["bob", "charlie"], @@ -862,7 +860,7 @@ def test_allow_exact_matches_and_tolerance_forward(self): {"a": [1, 5, 10], "left_val": ["a", "b", "c"], "right_val": [np.nan, 6, 11]} ) - result = pd.merge_asof( + result = merge_asof( left, right, on="a", @@ -882,7 +880,7 @@ def test_allow_exact_matches_and_tolerance_nearest(self): {"a": [1, 5, 10], "left_val": ["a", "b", "c"], "right_val": [np.nan, 4, 11]} ) - result = pd.merge_asof( + result = merge_asof( left, right, on="a", @@ -919,7 +917,7 @@ def test_forward_by(self): } ) - result = pd.merge_asof(left, right, on="a", by="b", direction="forward") + result = merge_asof(left, right, on="a", by="b", direction="forward") tm.assert_frame_equal(result, expected) def test_nearest_by(self): @@ -949,14 +947,14 @@ def test_nearest_by(self): } ) - result = pd.merge_asof(left, right, on="a", by="b", direction="nearest") + result = merge_asof(left, right, on="a", by="b", direction="nearest") tm.assert_frame_equal(result, expected) def test_by_int(self): # we specialize by type, so test that this is correct df1 = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( [ "20160525 13:30:00.020", "20160525 13:30:00.030", @@ -973,7 +971,7 @@ def test_by_int(self): df2 = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( [ "20160525 13:30:00.015", "20160525 13:30:00.020", @@ -991,11 +989,11 @@ def test_by_int(self): columns=["time", "key", "value2"], ) - result = pd.merge_asof(df1, df2, on="time", by="key") + result = merge_asof(df1, df2, on="time", by="key") expected = pd.DataFrame( { - "time": pd.to_datetime( + "time": to_datetime( [ "20160525 13:30:00.020", "20160525 13:30:00.030", @@ -1030,7 +1028,7 @@ def test_on_float(self): df1 = df1.sort_values("price").reset_index(drop=True) - result = pd.merge_asof(df1, df2, on="price") + result = merge_asof(df1, df2, on="price") expected = pd.DataFrame( { @@ -1060,7 +1058,7 @@ def test_on_specialized_type(self, any_real_dtype): df2.value = dtype(df2.value) df1 = df1.sort_values("value").reset_index(drop=True) - result = pd.merge_asof(df1, df2, on="value") + result = merge_asof(df1, df2, on="value") expected = pd.DataFrame( { @@ -1095,7 +1093,7 @@ def test_on_specialized_type_by_int(self, any_real_dtype): df2.value = dtype(df2.value) df1 = df1.sort_values("value").reset_index(drop=True) - result = pd.merge_asof(df1, df2, on="value", by="key") + result = merge_asof(df1, df2, on="value", by="key") expected = pd.DataFrame( { @@ -1143,7 +1141,7 @@ def test_on_float_by_int(self): df1 = df1.sort_values("price").reset_index(drop=True) df2 = df2.sort_values("price").reset_index(drop=True) - result = pd.merge_asof(df1, df2, on="price", by="exch") + result = merge_asof(df1, df2, on="price", by="exch") expected = pd.DataFrame( { @@ -1236,7 +1234,7 @@ def test_merge_by_col_tz_aware(self): "values": ["b"], } ) - result = pd.merge_asof(left, right, by="by_col", on="on_col") + result = merge_asof(left, right, by="by_col", on="on_col") expected = pd.DataFrame( [[pd.Timestamp("2018-01-01", tz="UTC"), 2, "a", "b"]], columns=["by_col", "on_col", "values_x", "values_y"], @@ -1261,7 +1259,7 @@ def test_by_mixed_tz_aware(self): "value": ["b"], } ) - result = pd.merge_asof(left, right, by=["by_col1", "by_col2"], on="on_col") + result = merge_asof(left, right, by=["by_col1", "by_col2"], on="on_col") expected = pd.DataFrame( [[pd.Timestamp("2018-01-01", tz="UTC"), "HELLO", 2, "a"]], columns=["by_col1", "by_col2", "on_col", "value_x"], @@ -1299,7 +1297,7 @@ def test_timedelta_tolerance_nearest(self): expected["time"] = pd.to_timedelta(expected["time"], "ms") - result = pd.merge_asof( + result = merge_asof( left, right, on="time", tolerance=Timedelta("1ms"), direction="nearest" ) @@ -1318,7 +1316,7 @@ def test_int_type_tolerance(self, any_int_dtype): ) expected["a"] = expected["a"].astype(any_int_dtype) - result = pd.merge_asof(left, right, on="a", tolerance=10) + result = merge_asof(left, right, on="a", tolerance=10) tm.assert_frame_equal(result, expected) def test_merge_index_column_tz(self): @@ -1326,7 +1324,7 @@ def test_merge_index_column_tz(self): index = pd.date_range("2019-10-01", freq="30min", periods=5, tz="UTC") left = pd.DataFrame([0.9, 0.8, 0.7, 0.6], columns=["xyz"], index=index[1:]) right = pd.DataFrame({"from_date": index, "abc": [2.46] * 4 + [2.19]}) - result = pd.merge_asof( + result = merge_asof( left=left, right=right, left_index=True, right_on=["from_date"] ) expected = pd.DataFrame( @@ -1339,7 +1337,7 @@ def test_merge_index_column_tz(self): ) tm.assert_frame_equal(result, expected) - result = pd.merge_asof( + result = merge_asof( left=right, right=left, right_index=True, left_on=["from_date"] ) expected = pd.DataFrame( @@ -1365,7 +1363,7 @@ def test_left_index_right_index_tolerance(self): expected = pd.DataFrame( {"val1": "foo", "val2": "bar"}, index=pd.DatetimeIndex(dr1) ) - result = pd.merge_asof( + result = merge_asof( df1, df2, left_index=True, diff --git a/pandas/tests/reshape/merge/test_multi.py b/pandas/tests/reshape/merge/test_multi.py index 673c97740594f..d9154b9a37c4f 100644 --- a/pandas/tests/reshape/merge/test_multi.py +++ b/pandas/tests/reshape/merge/test_multi.py @@ -106,7 +106,7 @@ def test_merge_on_multikey(self, left, right, join_type): on_cols = ["key1", "key2"] result = left.join(right, on=on_cols, how=join_type).reset_index(drop=True) - expected = pd.merge(left, right.reset_index(), on=on_cols, how=join_type) + expected = merge(left, right.reset_index(), on=on_cols, how=join_type) tm.assert_frame_equal(result, expected) @@ -114,7 +114,7 @@ def test_merge_on_multikey(self, left, right, join_type): drop=True ) - expected = pd.merge( + expected = merge( left, right.reset_index(), on=on_cols, how=join_type, sort=True ) @@ -200,7 +200,7 @@ def test_merge_multiple_cols_with_mixed_cols_index(self): name="Amount", ) df = DataFrame({"lev1": list("AAABBB"), "lev2": [1, 2, 3, 1, 2, 3], "col": 0}) - result = pd.merge(df, s.reset_index(), on=["lev1", "lev2"]) + result = merge(df, s.reset_index(), on=["lev1", "lev2"]) expected = DataFrame( { "lev1": list("AAABBB"), @@ -808,7 +808,7 @@ def test_join_multi_multi( ): # Multi-index join tests expected = ( - pd.merge( + merge( left_multi.reset_index(), right_multi.reset_index(), how=join_type, @@ -829,7 +829,7 @@ def test_join_multi_empty_frames( right_multi = right_multi.drop(columns=right_multi.columns) expected = ( - pd.merge( + merge( left_multi.reset_index(), right_multi.reset_index(), how=join_type, @@ -885,7 +885,7 @@ def test_single_common_level(self): ) result = left.join(right) - expected = pd.merge( + expected = merge( left.reset_index(), right.reset_index(), on=["key"], how="inner" ).set_index(["key", "X", "Y"]) diff --git a/pandas/tests/reshape/test_cut.py b/pandas/tests/reshape/test_cut.py index 4786b8c35a5b1..374bda786bbc6 100644 --- a/pandas/tests/reshape/test_cut.py +++ b/pandas/tests/reshape/test_cut.py @@ -671,7 +671,7 @@ def test_cut_unordered_with_series_labels(): s = Series([1, 2, 3, 4, 5]) bins = Series([0, 2, 4, 6]) labels = Series(["a", "b", "c"]) - result = pd.cut(s, bins=bins, labels=labels, ordered=False) + result = cut(s, bins=bins, labels=labels, ordered=False) expected = Series(["a", "a", "b", "b", "c"], dtype="category") tm.assert_series_equal(result, expected) @@ -680,4 +680,4 @@ def test_cut_no_warnings(): df = DataFrame({"value": np.random.randint(0, 100, 20)}) labels = [f"{i} - {i + 9}" for i in range(0, 100, 10)] with tm.assert_produces_warning(False): - df["group"] = pd.cut(df.value, range(0, 105, 10), right=False, labels=labels) + df["group"] = cut(df.value, range(0, 105, 10), right=False, labels=labels) diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 1f39302845ae9..147196a4b0030 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -297,7 +297,7 @@ def test_pandas_dtypes(self, col): def test_preserve_category(self): # GH 15853 data = DataFrame({"A": [1, 2], "B": pd.Categorical(["X", "Y"])}) - result = pd.melt(data, ["B"], ["A"]) + result = melt(data, ["B"], ["A"]) expected = DataFrame( {"B": pd.Categorical(["X", "Y"]), "variable": ["A", "A"], "value": [1, 2]} ) @@ -663,7 +663,7 @@ def test_stubs(self): stubs = ["inc", "edu"] # TODO: unused? - df_long = pd.wide_to_long(df, stubs, i="id", j="age") # noqa + df_long = wide_to_long(df, stubs, i="id", j="age") # noqa assert stubs == ["inc", "edu"] @@ -1050,10 +1050,8 @@ def test_col_substring_of_stubname(self): "PA3": {0: 0.34, 1: 0.70, 2: 0.52, 3: 0.98, 4: 0.67}, } wide_df = DataFrame.from_dict(wide_data) - expected = pd.wide_to_long( - wide_df, stubnames=["PA"], i=["node_id", "A"], j="time" - ) - result = pd.wide_to_long(wide_df, stubnames="PA", i=["node_id", "A"], j="time") + expected = wide_to_long(wide_df, stubnames=["PA"], i=["node_id", "A"], j="time") + result = wide_to_long(wide_df, stubnames="PA", i=["node_id", "A"], j="time") tm.assert_frame_equal(result, expected) def test_warn_of_column_name_value(self): diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index f9b2a02920841..085f88878ad34 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -193,7 +193,7 @@ def test_pivot_table_categorical(self): ["c", "d", "c", "d"], categories=["c", "d", "y"], ordered=True ) df = DataFrame({"A": cat1, "B": cat2, "values": [1, 2, 3, 4]}) - result = pd.pivot_table(df, values="values", index=["A", "B"], dropna=True) + result = pivot_table(df, values="values", index=["A", "B"], dropna=True) exp_index = MultiIndex.from_arrays([cat1, cat2], names=["A", "B"]) expected = DataFrame({"values": [1, 2, 3, 4]}, index=exp_index) @@ -298,7 +298,7 @@ def test_pivot_with_interval_index_margins(self): } ) - pivot_tab = pd.pivot_table( + pivot_tab = pivot_table( df, index="C", columns="B", values="A", aggfunc="sum", margins=True ) @@ -405,7 +405,7 @@ def test_pivot_no_values(self): df = DataFrame( { "A": [1, 2, 3, 4, 5], - "dt": pd.date_range("2011-01-01", freq="D", periods=5), + "dt": date_range("2011-01-01", freq="D", periods=5), }, index=idx, ) @@ -488,7 +488,7 @@ def test_pivot_index_with_nan(self, method): # GH9491 df = DataFrame( { - "a": pd.date_range("2014-02-01", periods=6, freq="D"), + "a": date_range("2014-02-01", periods=6, freq="D"), "c": 100 + np.arange(6), } ) @@ -601,7 +601,7 @@ def test_pivot_tz_in_values(self): df = df.set_index("ts").reset_index() mins = df.ts.map(lambda x: x.replace(hour=0, minute=0, second=0, microsecond=0)) - result = pd.pivot_table( + result = pivot_table( df.set_index("ts").reset_index(), values="ts", index=["uid"], @@ -1097,7 +1097,7 @@ def test_pivot_columns_lexsorted(self): iproduct = np.random.randint(0, len(products), n) items["Index"] = products["Index"][iproduct] items["Symbol"] = products["Symbol"][iproduct] - dr = pd.date_range(date(2000, 1, 1), date(2010, 12, 31)) + dr = date_range(date(2000, 1, 1), date(2010, 12, 31)) dates = dr[np.random.randint(0, len(dr), n)] items["Year"] = dates.year items["Month"] = dates.month @@ -1660,17 +1660,17 @@ def test_pivot_table_with_iterator_values(self): # GH 12017 aggs = {"D": "sum", "E": "mean"} - pivot_values_list = pd.pivot_table( + pivot_values_list = pivot_table( self.data, index=["A"], values=list(aggs.keys()), aggfunc=aggs ) - pivot_values_keys = pd.pivot_table( + pivot_values_keys = pivot_table( self.data, index=["A"], values=aggs.keys(), aggfunc=aggs ) tm.assert_frame_equal(pivot_values_keys, pivot_values_list) agg_values_gen = (value for value in aggs.keys()) - pivot_values_gen = pd.pivot_table( + pivot_values_gen = pivot_table( self.data, index=["A"], values=agg_values_gen, aggfunc=aggs ) tm.assert_frame_equal(pivot_values_gen, pivot_values_list) @@ -1745,7 +1745,7 @@ def test_margins_casted_to_float(self, observed): } ) - result = pd.pivot_table(df, index="D", margins=True) + result = pivot_table(df, index="D", margins=True) expected = DataFrame( {"A": [3, 7, 5], "B": [2.5, 6.5, 4.5], "C": [2, 5, 3.5]}, index=Index(["X", "Y", "All"], name="D"), @@ -1883,7 +1883,7 @@ def test_pivot_margins_name_unicode(self): # issue #13292 greek = "\u0394\u03bf\u03ba\u03b9\u03bc\u03ae" frame = DataFrame({"foo": [1, 2, 3]}) - table = pd.pivot_table( + table = pivot_table( frame, index=["foo"], aggfunc=len, margins=True, margins_name=greek ) index = Index([1, 2, 3, greek], dtype="object", name="foo") @@ -2002,7 +2002,7 @@ def ret_sum(x): def ret_none(x): return np.nan - result = pd.pivot_table( + result = pivot_table( df, columns="fruit", aggfunc=[ret_sum, ret_none, ret_one], dropna=dropna ) @@ -2024,7 +2024,7 @@ def test_pivot_table_aggfunc_scalar_dropna(self, dropna): {"A": ["one", "two", "one"], "x": [3, np.nan, 2], "y": [1, np.nan, np.nan]} ) - result = pd.pivot_table(df, columns="A", aggfunc=np.mean, dropna=dropna) + result = pivot_table(df, columns="A", aggfunc=np.mean, dropna=dropna) data = [[2.5, np.nan], [1, np.nan]] col = Index(["one", "two"], name="A") diff --git a/pandas/tests/scalar/timestamp/test_unary_ops.py b/pandas/tests/scalar/timestamp/test_unary_ops.py index 88f99a6784ba1..9b251053b4a7f 100644 --- a/pandas/tests/scalar/timestamp/test_unary_ops.py +++ b/pandas/tests/scalar/timestamp/test_unary_ops.py @@ -322,24 +322,24 @@ def test_replace_tzinfo(self): tzinfo = pytz.timezone("CET").localize(dt, is_dst=False).tzinfo result_dt = dt.replace(tzinfo=tzinfo) - result_pd = Timestamp(dt).replace(tzinfo=tzinfo) + result_pandas = Timestamp(dt).replace(tzinfo=tzinfo) # datetime.timestamp() converts in the local timezone with tm.set_timezone("UTC"): - assert result_dt.timestamp() == result_pd.timestamp() + assert result_dt.timestamp() == result_pandas.timestamp() - assert result_dt == result_pd - assert result_dt == result_pd.to_pydatetime() + assert result_dt == result_pandas + assert result_dt == result_pandas.to_pydatetime() result_dt = dt.replace(tzinfo=tzinfo).replace(tzinfo=None) - result_pd = Timestamp(dt).replace(tzinfo=tzinfo).replace(tzinfo=None) + result_pandas = Timestamp(dt).replace(tzinfo=tzinfo).replace(tzinfo=None) # datetime.timestamp() converts in the local timezone with tm.set_timezone("UTC"): - assert result_dt.timestamp() == result_pd.timestamp() + assert result_dt.timestamp() == result_pandas.timestamp() - assert result_dt == result_pd - assert result_dt == result_pd.to_pydatetime() + assert result_dt == result_pandas + assert result_dt == result_pandas.to_pydatetime() @pytest.mark.parametrize( "tz, normalize", diff --git a/pandas/tests/series/accessors/test_dt_accessor.py b/pandas/tests/series/accessors/test_dt_accessor.py index 7a84f642aebc2..ee39f1ad8948d 100644 --- a/pandas/tests/series/accessors/test_dt_accessor.py +++ b/pandas/tests/series/accessors/test_dt_accessor.py @@ -229,11 +229,11 @@ def get_dir(s): # 11295 # ambiguous time error on the conversions - s = Series(pd.date_range("2015-01-01", "2016-01-01", freq="T"), name="xxx") + s = Series(date_range("2015-01-01", "2016-01-01", freq="T"), name="xxx") s = s.dt.tz_localize("UTC").dt.tz_convert("America/Chicago") results = get_dir(s) tm.assert_almost_equal(results, sorted(set(ok_for_dt + ok_for_dt_methods))) - exp_values = pd.date_range( + exp_values = date_range( "2015-01-01", "2016-01-01", freq="T", tz="UTC" ).tz_convert("America/Chicago") # freq not preserved by tz_localize above @@ -624,7 +624,7 @@ def test_dt_accessor_invalid(self, ser): assert not hasattr(ser, "dt") def test_dt_accessor_updates_on_inplace(self): - s = Series(pd.date_range("2018-01-01", periods=10)) + s = Series(date_range("2018-01-01", periods=10)) s[2] = None return_value = s.fillna(pd.Timestamp("2018-01-01"), inplace=True) assert return_value is None diff --git a/pandas/tests/series/indexing/test_datetime.py b/pandas/tests/series/indexing/test_datetime.py index a15ef11f9c292..107bb4faea9dc 100644 --- a/pandas/tests/series/indexing/test_datetime.py +++ b/pandas/tests/series/indexing/test_datetime.py @@ -491,7 +491,7 @@ def test_indexing_over_size_cutoff_period_index(monkeypatch): monkeypatch.setattr(libindex, "_SIZE_CUTOFF", 1000) n = 1100 - idx = pd.period_range("1/1/2000", freq="T", periods=n) + idx = period_range("1/1/2000", freq="T", periods=n) assert idx._engine.over_size_threshold s = Series(np.random.randn(len(idx)), index=idx) diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index dbc751dd614a1..6ed0b79c95222 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -70,7 +70,7 @@ def test_basic_getitem_with_labels(datetime_series): # GH12089 # with tz for values s = Series( - pd.date_range("2011-01-01", periods=3, tz="US/Eastern"), index=["a", "b", "c"] + date_range("2011-01-01", periods=3, tz="US/Eastern"), index=["a", "b", "c"] ) expected = Timestamp("2011-01-01", tz="US/Eastern") result = s.loc["a"] @@ -178,12 +178,12 @@ def test_getitem_box_float64(datetime_series): def test_series_box_timestamp(): - rng = pd.date_range("20090415", "20090519", freq="B") + rng = date_range("20090415", "20090519", freq="B") ser = Series(rng) assert isinstance(ser[5], Timestamp) - rng = pd.date_range("20090415", "20090519", freq="B") + rng = date_range("20090415", "20090519", freq="B") ser = Series(rng, index=rng) assert isinstance(ser[5], Timestamp) @@ -191,7 +191,7 @@ def test_series_box_timestamp(): def test_series_box_timedelta(): - rng = pd.timedelta_range("1 day 1 s", periods=5, freq="h") + rng = timedelta_range("1 day 1 s", periods=5, freq="h") ser = Series(rng) assert isinstance(ser[0], Timedelta) assert isinstance(ser.at[1], Timedelta) @@ -322,7 +322,7 @@ def test_basic_getitem_setitem_corner(datetime_series): @pytest.mark.parametrize("tz", ["US/Eastern", "UTC", "Asia/Tokyo"]) def test_setitem_with_tz(tz): - orig = Series(pd.date_range("2016-01-01", freq="H", periods=3, tz=tz)) + orig = Series(date_range("2016-01-01", freq="H", periods=3, tz=tz)) assert orig.dtype == f"datetime64[ns, {tz}]" # scalar @@ -374,7 +374,7 @@ def test_setitem_with_tz(tz): def test_setitem_with_tz_dst(): # GH XXX TODO: fill in GH ref tz = "US/Eastern" - orig = Series(pd.date_range("2016-11-06", freq="H", periods=3, tz=tz)) + orig = Series(date_range("2016-11-06", freq="H", periods=3, tz=tz)) assert orig.dtype == f"datetime64[ns, {tz}]" # scalar @@ -558,7 +558,7 @@ def test_dt64_series_assign_nat(nat_val, should_cast, tz): # some nat-like values should be cast to datetime64 when inserting # into a datetime64 series. Others should coerce to object # and retain their dtypes. - dti = pd.date_range("2016-01-01", periods=3, tz=tz) + dti = date_range("2016-01-01", periods=3, tz=tz) base = Series(dti) expected = Series([pd.NaT] + list(dti[1:]), dtype=dti.dtype) if not should_cast: diff --git a/pandas/tests/series/indexing/test_where.py b/pandas/tests/series/indexing/test_where.py index 59c68fba53e25..ec075af5473e7 100644 --- a/pandas/tests/series/indexing/test_where.py +++ b/pandas/tests/series/indexing/test_where.py @@ -470,7 +470,7 @@ def test_where_datetimelike_categorical(tz_naive_fixture): # GH#37682 tz = tz_naive_fixture - dr = pd.date_range("2001-01-01", periods=3, tz=tz)._with_freq(None) + dr = date_range("2001-01-01", periods=3, tz=tz)._with_freq(None) lvals = pd.DatetimeIndex([dr[0], dr[1], pd.NaT]) rvals = pd.Categorical([dr[0], pd.NaT, dr[2]]) diff --git a/pandas/tests/series/methods/test_interpolate.py b/pandas/tests/series/methods/test_interpolate.py index 8740a309eec13..f31c9ad0c3140 100644 --- a/pandas/tests/series/methods/test_interpolate.py +++ b/pandas/tests/series/methods/test_interpolate.py @@ -636,7 +636,7 @@ def test_interp_datetime64(self, method, tz_naive_fixture): def test_interp_pad_datetime64tz_values(self): # GH#27628 missing.interpolate_2d should handle datetimetz values - dti = pd.date_range("2015-04-05", periods=3, tz="US/Central") + dti = date_range("2015-04-05", periods=3, tz="US/Central") ser = Series(dti) ser[1] = pd.NaT result = ser.interpolate(method="pad") @@ -729,13 +729,13 @@ def test_series_interpolate_method_values(self): def test_series_interpolate_intraday(self): # #1698 - index = pd.date_range("1/1/2012", periods=4, freq="12D") + index = date_range("1/1/2012", periods=4, freq="12D") ts = Series([0, 12, 24, 36], index) new_index = index.append(index + pd.DateOffset(days=1)).sort_values() exp = ts.reindex(new_index).interpolate(method="time") - index = pd.date_range("1/1/2012", periods=4, freq="12H") + index = date_range("1/1/2012", periods=4, freq="12H") ts = Series([0, 12, 24, 36], index) new_index = index.append(index + pd.DateOffset(hours=1)).sort_values() result = ts.reindex(new_index).interpolate(method="time") diff --git a/pandas/tests/series/methods/test_shift.py b/pandas/tests/series/methods/test_shift.py index 60ec0a90e906f..73684e300ed77 100644 --- a/pandas/tests/series/methods/test_shift.py +++ b/pandas/tests/series/methods/test_shift.py @@ -353,14 +353,14 @@ def test_shift_preserve_freqstr(self, periods): # GH#21275 ser = Series( range(periods), - index=pd.date_range("2016-1-1 00:00:00", periods=periods, freq="H"), + index=date_range("2016-1-1 00:00:00", periods=periods, freq="H"), ) result = ser.shift(1, "2H") expected = Series( range(periods), - index=pd.date_range("2016-1-1 02:00:00", periods=periods, freq="H"), + index=date_range("2016-1-1 02:00:00", periods=periods, freq="H"), ) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/series/methods/test_to_csv.py b/pandas/tests/series/methods/test_to_csv.py index a22e125e68cba..cacb5beb9ca3b 100644 --- a/pandas/tests/series/methods/test_to_csv.py +++ b/pandas/tests/series/methods/test_to_csv.py @@ -9,6 +9,7 @@ import pandas._testing as tm from pandas.io.common import get_handle +from pandas.io.parsers import read_csv class TestSeriesToCSV: @@ -17,7 +18,7 @@ def read_csv(self, path, **kwargs): params.update(**kwargs) header = params.get("header") - out = pd.read_csv(path, **params) + out = read_csv(path, **params) if header is None: out.name = out.index.name = None @@ -133,7 +134,7 @@ def test_to_csv_compression(self, s, encoding, compression): s.to_csv(filename, compression=compression, encoding=encoding, header=True) # test the round trip - to_csv -> read_csv - result = pd.read_csv( + result = read_csv( filename, compression=compression, encoding=encoding, @@ -148,7 +149,7 @@ def test_to_csv_compression(self, s, encoding, compression): ) as handles: s.to_csv(handles.handle, encoding=encoding, header=True) - result = pd.read_csv( + result = read_csv( filename, compression=compression, encoding=encoding, @@ -164,7 +165,7 @@ def test_to_csv_compression(self, s, encoding, compression): with tm.decompress_file(filename, compression) as fh: tm.assert_series_equal( - s, pd.read_csv(fh, index_col=0, squeeze=True, encoding=encoding) + s, read_csv(fh, index_col=0, squeeze=True, encoding=encoding) ) def test_to_csv_interval_index(self): diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index c7bd38bbd00b9..740159be78ded 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -272,8 +272,8 @@ def test_constructor_index_dtype(self, dtype): [ ([1, 2]), (["1", "2"]), - (list(pd.date_range("1/1/2011", periods=2, freq="H"))), - (list(pd.date_range("1/1/2011", periods=2, freq="H", tz="US/Eastern"))), + (list(date_range("1/1/2011", periods=2, freq="H"))), + (list(date_range("1/1/2011", periods=2, freq="H", tz="US/Eastern"))), ([Interval(left=0, right=5)]), ], ) @@ -628,10 +628,10 @@ def test_constructor_copy(self): @pytest.mark.parametrize( "index", [ - pd.date_range("20170101", periods=3, tz="US/Eastern"), - pd.date_range("20170101", periods=3), - pd.timedelta_range("1 day", periods=3), - pd.period_range("2012Q1", periods=3, freq="Q"), + date_range("20170101", periods=3, tz="US/Eastern"), + date_range("20170101", periods=3), + timedelta_range("1 day", periods=3), + period_range("2012Q1", periods=3, freq="Q"), Index(list("abc")), pd.Int64Index([1, 2, 3]), RangeIndex(0, 3), @@ -1038,7 +1038,7 @@ def test_construction_consistency(self): # make sure that we are not re-localizing upon construction # GH 14928 - s = Series(pd.date_range("20130101", periods=3, tz="US/Eastern")) + s = Series(date_range("20130101", periods=3, tz="US/Eastern")) result = Series(s, dtype=s.dtype) tm.assert_series_equal(result, s) @@ -1374,7 +1374,7 @@ def test_convert_non_ns(self): # convert from a numpy array of non-ns timedelta64 arr = np.array([1, 2, 3], dtype="timedelta64[s]") s = Series(arr) - expected = Series(pd.timedelta_range("00:00:01", periods=3, freq="s")) + expected = Series(timedelta_range("00:00:01", periods=3, freq="s")) tm.assert_series_equal(s, expected) # convert from a numpy array of non-ns datetime64 diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 026f6bd2d453d..ab2b4d9d2bf0f 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -166,7 +166,7 @@ def test_repr_should_return_str(self): def test_repr_max_rows(self): # GH 6863 - with pd.option_context("max_rows", None): + with option_context("max_rows", None): str(Series(range(1001))) # should not raise exception def test_unicode_string_with_unicode(self): diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index 278a315a479bd..9c38b90304f50 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -101,14 +101,14 @@ def test_to_datetime_format_YYYYMMDD(self, cache): # coercion # GH 7930 s = Series([20121231, 20141231, 99991231]) - result = pd.to_datetime(s, format="%Y%m%d", errors="ignore", cache=cache) + result = to_datetime(s, format="%Y%m%d", errors="ignore", cache=cache) expected = Series( [datetime(2012, 12, 31), datetime(2014, 12, 31), datetime(9999, 12, 31)], dtype=object, ) tm.assert_series_equal(result, expected) - result = pd.to_datetime(s, format="%Y%m%d", errors="coerce", cache=cache) + result = to_datetime(s, format="%Y%m%d", errors="coerce", cache=cache) expected = Series(["20121231", "20141231", "NaT"], dtype="M8[ns]") tm.assert_series_equal(result, expected) @@ -132,7 +132,7 @@ def test_to_datetime_format_YYYYMMDD_with_none(self, input_s): # format='%Y%m%d' # with None expected = Series([Timestamp("19801222"), Timestamp("20010112"), pd.NaT]) - result = Series(pd.to_datetime(input_s, format="%Y%m%d")) + result = Series(to_datetime(input_s, format="%Y%m%d")) tm.assert_series_equal(result, expected) @pytest.mark.parametrize( @@ -163,7 +163,7 @@ def test_to_datetime_format_YYYYMMDD_with_none(self, input_s): def test_to_datetime_format_YYYYMMDD_overflow(self, input_s, expected): # GH 25512 # format='%Y%m%d', errors='coerce' - result = pd.to_datetime(input_s, format="%Y%m%d", errors="coerce") + result = to_datetime(input_s, format="%Y%m%d", errors="coerce") tm.assert_series_equal(result, expected) @pytest.mark.parametrize("cache", [True, False]) @@ -263,8 +263,8 @@ def test_parse_nanoseconds_with_formula(self, cache): "2012-01-01 09:00:00.001000", "2012-01-01 09:00:00.001000000", ]: - expected = pd.to_datetime(v, cache=cache) - result = pd.to_datetime(v, format="%Y-%m-%d %H:%M:%S.%f", cache=cache) + expected = to_datetime(v, cache=cache) + result = to_datetime(v, format="%Y-%m-%d %H:%M:%S.%f", cache=cache) assert result == expected @pytest.mark.parametrize("cache", [True, False]) @@ -329,7 +329,7 @@ def test_to_datetime_format_weeks(self, cache): ) def test_to_datetime_parse_tzname_or_tzoffset(self, fmt, dates, expected_dates): # GH 13486 - result = pd.to_datetime(dates, format=fmt) + result = to_datetime(dates, format=fmt) expected = Index(expected_dates) tm.assert_equal(result, expected) @@ -349,7 +349,7 @@ def test_to_datetime_parse_tzname_or_tzoffset_different_tz_to_utc(self): ] fmt = "%Y-%m-%d %H:%M:%S %z" - result = pd.to_datetime(dates, format=fmt, utc=True) + result = to_datetime(dates, format=fmt, utc=True) expected = DatetimeIndex(expected_dates) tm.assert_index_equal(result, expected) @@ -362,13 +362,13 @@ def test_to_datetime_parse_timezone_malformed(self, offset): msg = "does not match format|unconverted data remains" with pytest.raises(ValueError, match=msg): - pd.to_datetime([date], format=fmt) + to_datetime([date], format=fmt) def test_to_datetime_parse_timezone_keeps_name(self): # GH 21697 fmt = "%Y-%m-%d %H:%M:%S %z" arg = Index(["2010-01-01 12:00:00 Z"], name="foo") - result = pd.to_datetime(arg, format=fmt) + result = to_datetime(arg, format=fmt) expected = DatetimeIndex(["2010-01-01 12:00:00"], tz="UTC", name="foo") tm.assert_index_equal(result, expected) @@ -497,25 +497,25 @@ def test_to_datetime_dtarr(self, tz): assert result is arr def test_to_datetime_pydatetime(self): - actual = pd.to_datetime(datetime(2008, 1, 15)) + actual = to_datetime(datetime(2008, 1, 15)) assert actual == datetime(2008, 1, 15) def test_to_datetime_YYYYMMDD(self): - actual = pd.to_datetime("20080115") + actual = to_datetime("20080115") assert actual == datetime(2008, 1, 15) def test_to_datetime_unparseable_ignore(self): # unparseable s = "Month 1, 1999" - assert pd.to_datetime(s, errors="ignore") == s + assert to_datetime(s, errors="ignore") == s @td.skip_if_windows # `tm.set_timezone` does not work in windows def test_to_datetime_now(self): # See GH#18666 with tm.set_timezone("US/Eastern"): npnow = np.datetime64("now").astype("datetime64[ns]") - pdnow = pd.to_datetime("now") - pdnow2 = pd.to_datetime(["now"])[0] + pdnow = to_datetime("now") + pdnow2 = to_datetime(["now"])[0] # These should all be equal with infinite perf; this gives # a generous margin of 10 seconds @@ -535,8 +535,8 @@ def test_to_datetime_today(self): # so this test will not detect the regression introduced in #18666. with tm.set_timezone("Pacific/Auckland"): # 12-13 hours ahead of UTC nptoday = np.datetime64("today").astype("datetime64[ns]").astype(np.int64) - pdtoday = pd.to_datetime("today") - pdtoday2 = pd.to_datetime(["today"])[0] + pdtoday = to_datetime("today") + pdtoday2 = to_datetime(["today"])[0] tstoday = Timestamp("today") tstoday2 = Timestamp.today() @@ -553,8 +553,8 @@ def test_to_datetime_today(self): with tm.set_timezone("US/Samoa"): # 11 hours behind UTC nptoday = np.datetime64("today").astype("datetime64[ns]").astype(np.int64) - pdtoday = pd.to_datetime("today") - pdtoday2 = pd.to_datetime(["today"])[0] + pdtoday = to_datetime("today") + pdtoday2 = to_datetime(["today"])[0] # These should all be equal with infinite perf; this gives # a generous margin of 10 seconds @@ -573,7 +573,7 @@ def test_to_datetime_dt64s(self, cache): in_bound_dts = [np.datetime64("2000-01-01"), np.datetime64("2000-01-02")] for dt in in_bound_dts: - assert pd.to_datetime(dt, cache=cache) == Timestamp(dt) + assert to_datetime(dt, cache=cache) == Timestamp(dt) @pytest.mark.parametrize( "dt", [np.datetime64("1000-01-01"), np.datetime64("5000-01-02")] @@ -582,10 +582,10 @@ def test_to_datetime_dt64s(self, cache): def test_to_datetime_dt64s_out_of_bounds(self, cache, dt): msg = f"Out of bounds nanosecond timestamp: {dt}" with pytest.raises(OutOfBoundsDatetime, match=msg): - pd.to_datetime(dt, errors="raise") + to_datetime(dt, errors="raise") with pytest.raises(OutOfBoundsDatetime, match=msg): Timestamp(dt) - assert pd.to_datetime(dt, errors="coerce", cache=cache) is NaT + assert to_datetime(dt, errors="coerce", cache=cache) is NaT @pytest.mark.parametrize("cache", [True, False]) @pytest.mark.parametrize("unit", ["s", "D"]) @@ -599,7 +599,7 @@ def test_to_datetime_array_of_dt64s(self, cache, unit): # Assuming all datetimes are in bounds, to_datetime() returns # an array that is equal to Timestamp() parsing tm.assert_index_equal( - pd.to_datetime(dts, cache=cache), + to_datetime(dts, cache=cache), DatetimeIndex([Timestamp(x).asm8 for x in dts]), ) @@ -608,10 +608,10 @@ def test_to_datetime_array_of_dt64s(self, cache, unit): msg = "Out of bounds nanosecond timestamp: 9999-01-01 00:00:00" with pytest.raises(OutOfBoundsDatetime, match=msg): - pd.to_datetime(dts_with_oob, errors="raise") + to_datetime(dts_with_oob, errors="raise") tm.assert_index_equal( - pd.to_datetime(dts_with_oob, errors="coerce", cache=cache), + to_datetime(dts_with_oob, errors="coerce", cache=cache), DatetimeIndex( [Timestamp(dts_with_oob[0]).asm8, Timestamp(dts_with_oob[1]).asm8] * 30 + [pd.NaT], @@ -622,7 +622,7 @@ def test_to_datetime_array_of_dt64s(self, cache, unit): # are converted to their .item(), which depending on the version of # numpy is either a python datetime.datetime or datetime.date tm.assert_index_equal( - pd.to_datetime(dts_with_oob, errors="ignore", cache=cache), + to_datetime(dts_with_oob, errors="ignore", cache=cache), Index([dt.item() for dt in dts_with_oob]), ) @@ -635,7 +635,7 @@ def test_to_datetime_tz(self, cache): Timestamp("2013-01-01 13:00:00-0800", tz="US/Pacific"), Timestamp("2013-01-02 14:00:00-0800", tz="US/Pacific"), ] - result = pd.to_datetime(arr, cache=cache) + result = to_datetime(arr, cache=cache) expected = DatetimeIndex( ["2013-01-01 13:00:00", "2013-01-02 14:00:00"], tz="US/Pacific" ) @@ -651,7 +651,7 @@ def test_to_datetime_tz(self, cache): "converted to datetime64 unless utc=True" ) with pytest.raises(ValueError, match=msg): - pd.to_datetime(arr, cache=cache) + to_datetime(arr, cache=cache) @pytest.mark.parametrize("cache", [True, False]) def test_to_datetime_different_offsets(self, cache): @@ -661,7 +661,7 @@ def test_to_datetime_different_offsets(self, cache): ts_string_2 = "March 1, 2018 12:00:00+0500" arr = [ts_string_1] * 5 + [ts_string_2] * 5 expected = Index([parse(x) for x in arr]) - result = pd.to_datetime(arr, cache=cache) + result = to_datetime(arr, cache=cache) tm.assert_index_equal(result, expected) @pytest.mark.parametrize("cache", [True, False]) @@ -679,7 +679,7 @@ def test_to_datetime_tz_pytz(self, cache): ], dtype=object, ) - result = pd.to_datetime(arr, utc=True, cache=cache) + result = to_datetime(arr, utc=True, cache=cache) expected = DatetimeIndex( ["2000-01-01 08:00:00+00:00", "2000-06-01 07:00:00+00:00"], dtype="datetime64[ns, UTC]", @@ -707,7 +707,7 @@ def test_to_datetime_utc_true( Timestamp("2010-01-02 12:13:15", tz="utc"), ] - result = pd.to_datetime( + result = to_datetime( init_constructor(data), format="%Y%m%d %H%M%S", utc=True, cache=cache ) expected = end_constructor(expected_data) @@ -715,16 +715,14 @@ def test_to_datetime_utc_true( # Test scalar case as well for scalar, expected in zip(data, expected_data): - result = pd.to_datetime( - scalar, format="%Y%m%d %H%M%S", utc=True, cache=cache - ) + result = to_datetime(scalar, format="%Y%m%d %H%M%S", utc=True, cache=cache) assert result == expected @pytest.mark.parametrize("cache", [True, False]) def test_to_datetime_utc_true_with_series_single_value(self, cache): # GH 15760 UTC=True with Series ts = 1.5e18 - result = pd.to_datetime(Series([ts]), utc=True, cache=cache) + result = to_datetime(Series([ts]), utc=True, cache=cache) expected = Series([Timestamp(ts, tz="utc")]) tm.assert_series_equal(result, expected) @@ -733,7 +731,7 @@ def test_to_datetime_utc_true_with_series_tzaware_string(self, cache): ts = "2013-01-01 00:00:00-01:00" expected_ts = "2013-01-01 01:00:00" data = Series([ts] * 3) - result = pd.to_datetime(data, utc=True, cache=cache) + result = to_datetime(data, utc=True, cache=cache) expected = Series([Timestamp(expected_ts, tz="utc")] * 3) tm.assert_series_equal(result, expected) @@ -747,7 +745,7 @@ def test_to_datetime_utc_true_with_series_tzaware_string(self, cache): ) def test_to_datetime_utc_true_with_series_datetime_ns(self, cache, date, dtype): expected = Series([Timestamp("2013-01-01 01:00:00", tz="UTC")]) - result = pd.to_datetime(Series([date], dtype=dtype), utc=True, cache=cache) + result = to_datetime(Series([date], dtype=dtype), utc=True, cache=cache) tm.assert_series_equal(result, expected) @pytest.mark.parametrize("cache", [True, False]) @@ -768,7 +766,7 @@ def test_to_datetime_tz_psycopg2(self, cache): dtype=object, ) - result = pd.to_datetime(arr, errors="coerce", utc=True, cache=cache) + result = to_datetime(arr, errors="coerce", utc=True, cache=cache) expected = DatetimeIndex( ["2000-01-01 08:00:00+00:00", "2000-06-01 07:00:00+00:00"], dtype="datetime64[ns, UTC]", @@ -784,10 +782,10 @@ def test_to_datetime_tz_psycopg2(self, cache): assert is_datetime64_ns_dtype(i) # tz coercion - result = pd.to_datetime(i, errors="coerce", cache=cache) + result = to_datetime(i, errors="coerce", cache=cache) tm.assert_index_equal(result, i) - result = pd.to_datetime(i, errors="coerce", utc=True, cache=cache) + result = to_datetime(i, errors="coerce", utc=True, cache=cache) expected = DatetimeIndex(["2000-01-01 13:00:00"], dtype="datetime64[ns, UTC]") tm.assert_index_equal(result, expected) @@ -819,21 +817,21 @@ def test_datetime_invalid_datatype(self): # GH13176 msg = "is not convertible to datetime" with pytest.raises(TypeError, match=msg): - pd.to_datetime(bool) + to_datetime(bool) with pytest.raises(TypeError, match=msg): - pd.to_datetime(pd.to_datetime) + to_datetime(to_datetime) @pytest.mark.parametrize("value", ["a", "00:01:99"]) @pytest.mark.parametrize("infer", [True, False]) @pytest.mark.parametrize("format", [None, "H%:M%:S%"]) def test_datetime_invalid_scalar(self, value, format, infer): # GH24763 - res = pd.to_datetime( + res = to_datetime( value, errors="ignore", format=format, infer_datetime_format=infer ) assert res == value - res = pd.to_datetime( + res = to_datetime( value, errors="coerce", format=format, infer_datetime_format=infer ) assert res is pd.NaT @@ -844,7 +842,7 @@ def test_datetime_invalid_scalar(self, value, format, infer): "Given date string not likely a datetime" ) with pytest.raises(ValueError, match=msg): - pd.to_datetime( + to_datetime( value, errors="raise", format=format, infer_datetime_format=infer ) @@ -853,12 +851,12 @@ def test_datetime_invalid_scalar(self, value, format, infer): @pytest.mark.parametrize("format", [None, "H%:M%:S%"]) def test_datetime_outofbounds_scalar(self, value, format, infer): # GH24763 - res = pd.to_datetime( + res = to_datetime( value, errors="ignore", format=format, infer_datetime_format=infer ) assert res == value - res = pd.to_datetime( + res = to_datetime( value, errors="coerce", format=format, infer_datetime_format=infer ) assert res is pd.NaT @@ -866,13 +864,13 @@ def test_datetime_outofbounds_scalar(self, value, format, infer): if format is not None: msg = "is a bad directive in format|Out of bounds nanosecond timestamp" with pytest.raises(ValueError, match=msg): - pd.to_datetime( + to_datetime( value, errors="raise", format=format, infer_datetime_format=infer ) else: msg = "Out of bounds nanosecond timestamp" with pytest.raises(OutOfBoundsDatetime, match=msg): - pd.to_datetime( + to_datetime( value, errors="raise", format=format, infer_datetime_format=infer ) @@ -881,12 +879,12 @@ def test_datetime_outofbounds_scalar(self, value, format, infer): @pytest.mark.parametrize("format", [None, "H%:M%:S%"]) def test_datetime_invalid_index(self, values, format, infer): # GH24763 - res = pd.to_datetime( + res = to_datetime( values, errors="ignore", format=format, infer_datetime_format=infer ) tm.assert_index_equal(res, Index(values)) - res = pd.to_datetime( + res = to_datetime( values, errors="coerce", format=format, infer_datetime_format=infer ) tm.assert_index_equal(res, DatetimeIndex([pd.NaT] * len(values))) @@ -897,7 +895,7 @@ def test_datetime_invalid_index(self, values, format, infer): "second must be in 0..59" ) with pytest.raises(ValueError, match=msg): - pd.to_datetime( + to_datetime( values, errors="raise", format=format, infer_datetime_format=infer ) @@ -909,8 +907,8 @@ def test_to_datetime_cache(self, utc, format, constructor): test_dates = [date] * 10 ** 5 data = constructor(test_dates) - result = pd.to_datetime(data, utc=utc, format=format, cache=True) - expected = pd.to_datetime(data, utc=utc, format=format, cache=False) + result = to_datetime(data, utc=utc, format=format, cache=True) + expected = to_datetime(data, utc=utc, format=format, cache=False) tm.assert_index_equal(result, expected) @@ -928,8 +926,8 @@ def test_no_slicing_errors_in_should_cache(self, listlike): def test_to_datetime_from_deque(self): # GH 29403 - result = pd.to_datetime(deque([Timestamp("2010-06-02 09:30:00")] * 51)) - expected = pd.to_datetime([Timestamp("2010-06-02 09:30:00")] * 51) + result = to_datetime(deque([Timestamp("2010-06-02 09:30:00")] * 51)) + expected = to_datetime([Timestamp("2010-06-02 09:30:00")] * 51) tm.assert_index_equal(result, expected) @pytest.mark.parametrize("utc", [True, None]) @@ -938,13 +936,13 @@ def test_to_datetime_cache_series(self, utc, format): date = "20130101 00:00:00" test_dates = [date] * 10 ** 5 data = Series(test_dates) - result = pd.to_datetime(data, utc=utc, format=format, cache=True) - expected = pd.to_datetime(data, utc=utc, format=format, cache=False) + result = to_datetime(data, utc=utc, format=format, cache=True) + expected = to_datetime(data, utc=utc, format=format, cache=False) tm.assert_series_equal(result, expected) def test_to_datetime_cache_scalar(self): date = "20130101 00:00:00" - result = pd.to_datetime(date, cache=True) + result = to_datetime(date, cache=True) expected = Timestamp("20130101 00:00:00") assert result == expected @@ -964,7 +962,7 @@ def test_week_without_day_and_calendar_year(self, date, format): msg = "Cannot use '%W' or '%U' without day and year" with pytest.raises(ValueError, match=msg): - pd.to_datetime(date, format=format) + to_datetime(date, format=format) def test_to_datetime_coerce(self): # GH 26122 @@ -1028,7 +1026,7 @@ def test_iso_8601_strings_with_different_offsets(self): def test_iso8601_strings_mixed_offsets_with_naive(self): # GH 24992 - result = pd.to_datetime( + result = to_datetime( [ "2018-11-28T00:00:00", "2018-11-28T00:00:00+12:00", @@ -1038,7 +1036,7 @@ def test_iso8601_strings_mixed_offsets_with_naive(self): ], utc=True, ) - expected = pd.to_datetime( + expected = to_datetime( [ "2018-11-28T00:00:00", "2018-11-27T12:00:00", @@ -1051,8 +1049,8 @@ def test_iso8601_strings_mixed_offsets_with_naive(self): tm.assert_index_equal(result, expected) items = ["2018-11-28T00:00:00+12:00", "2018-11-28T00:00:00"] - result = pd.to_datetime(items, utc=True) - expected = pd.to_datetime(list(reversed(items)), utc=True)[::-1] + result = to_datetime(items, utc=True) + expected = to_datetime(list(reversed(items)), utc=True)[::-1] tm.assert_index_equal(result, expected) def test_mixed_offsets_with_native_datetime_raises(self): @@ -1067,7 +1065,7 @@ def test_mixed_offsets_with_native_datetime_raises(self): ] ) with pytest.raises(ValueError, match="Tz-aware datetime.datetime"): - pd.to_datetime(s) + to_datetime(s) def test_non_iso_strings_with_tz_offset(self): result = to_datetime(["March 1, 2018 12:00:00+0400"] * 2) @@ -1096,7 +1094,7 @@ def test_to_datetime_with_format_out_of_bounds(self, dt_str): # GH 9107 msg = "Out of bounds nanosecond timestamp" with pytest.raises(OutOfBoundsDatetime, match=msg): - pd.to_datetime(dt_str, format="%Y%m%d") + to_datetime(dt_str, format="%Y%m%d") def test_to_datetime_utc(self): arr = np.array([parse("2012-06-13T01:39:00Z")], dtype=object) @@ -1181,15 +1179,15 @@ def test_unit_consistency(self, cache): # consistency of conversions expected = Timestamp("1970-05-09 14:25:11") - result = pd.to_datetime(11111111, unit="s", errors="raise", cache=cache) + result = to_datetime(11111111, unit="s", errors="raise", cache=cache) assert result == expected assert isinstance(result, Timestamp) - result = pd.to_datetime(11111111, unit="s", errors="coerce", cache=cache) + result = to_datetime(11111111, unit="s", errors="coerce", cache=cache) assert result == expected assert isinstance(result, Timestamp) - result = pd.to_datetime(11111111, unit="s", errors="ignore", cache=cache) + result = to_datetime(11111111, unit="s", errors="ignore", cache=cache) assert result == expected assert isinstance(result, Timestamp) @@ -1202,24 +1200,24 @@ def test_unit_with_numeric(self, cache): arr1 = [1.434692e18, 1.432766e18] arr2 = np.array(arr1).astype("int64") for errors in ["ignore", "raise", "coerce"]: - result = pd.to_datetime(arr1, errors=errors, cache=cache) + result = to_datetime(arr1, errors=errors, cache=cache) tm.assert_index_equal(result, expected) - result = pd.to_datetime(arr2, errors=errors, cache=cache) + result = to_datetime(arr2, errors=errors, cache=cache) tm.assert_index_equal(result, expected) # but we want to make sure that we are coercing # if we have ints/strings expected = DatetimeIndex(["NaT", "2015-06-19 05:33:20", "2015-05-27 22:33:20"]) arr = ["foo", 1.434692e18, 1.432766e18] - result = pd.to_datetime(arr, errors="coerce", cache=cache) + result = to_datetime(arr, errors="coerce", cache=cache) tm.assert_index_equal(result, expected) expected = DatetimeIndex( ["2015-06-19 05:33:20", "2015-05-27 22:33:20", "NaT", "NaT"] ) arr = [1.434692e18, 1.432766e18, "foo", "NaT"] - result = pd.to_datetime(arr, errors="coerce", cache=cache) + result = to_datetime(arr, errors="coerce", cache=cache) tm.assert_index_equal(result, expected) @pytest.mark.parametrize("cache", [True, False]) @@ -1228,26 +1226,26 @@ def test_unit_mixed(self, cache): # mixed integers/datetimes expected = DatetimeIndex(["2013-01-01", "NaT", "NaT"]) arr = [Timestamp("20130101"), 1.434692e18, 1.432766e18] - result = pd.to_datetime(arr, errors="coerce", cache=cache) + result = to_datetime(arr, errors="coerce", cache=cache) tm.assert_index_equal(result, expected) msg = "mixed datetimes and integers in passed array" with pytest.raises(ValueError, match=msg): - pd.to_datetime(arr, errors="raise", cache=cache) + to_datetime(arr, errors="raise", cache=cache) expected = DatetimeIndex(["NaT", "NaT", "2013-01-01"]) arr = [1.434692e18, 1.432766e18, Timestamp("20130101")] - result = pd.to_datetime(arr, errors="coerce", cache=cache) + result = to_datetime(arr, errors="coerce", cache=cache) tm.assert_index_equal(result, expected) with pytest.raises(ValueError, match=msg): - pd.to_datetime(arr, errors="raise", cache=cache) + to_datetime(arr, errors="raise", cache=cache) @pytest.mark.parametrize("cache", [True, False]) def test_unit_rounding(self, cache): # GH 14156 & GH 20445: argument will incur floating point errors # but no premature rounding - result = pd.to_datetime(1434743731.8770001, unit="s", cache=cache) + result = to_datetime(1434743731.8770001, unit="s", cache=cache) expected = Timestamp("2015-06-19 19:55:31.877000192") assert result == expected @@ -1255,7 +1253,7 @@ def test_unit_rounding(self, cache): def test_unit_ignore_keeps_name(self, cache): # GH 21697 expected = Index([15e9] * 2, name="name") - result = pd.to_datetime(expected, errors="ignore", unit="s", cache=cache) + result = to_datetime(expected, errors="ignore", unit="s", cache=cache) tm.assert_index_equal(result, expected) @pytest.mark.parametrize("cache", [True, False]) @@ -1427,7 +1425,7 @@ def test_dataframe_dtypes(self, cache): def test_dataframe_utc_true(self): # GH 23760 df = DataFrame({"year": [2015, 2016], "month": [2, 3], "day": [4, 5]}) - result = pd.to_datetime(df, utc=True) + result = to_datetime(df, utc=True) expected = Series( np.array(["2015-02-04", "2016-03-05"], dtype="datetime64[ns]") ).dt.tz_localize("UTC") @@ -1435,7 +1433,7 @@ def test_dataframe_utc_true(self): def test_to_datetime_errors_ignore_utc_true(self): # GH 23758 - result = pd.to_datetime([1], unit="s", utc=True, errors="ignore") + result = to_datetime([1], unit="s", utc=True, errors="ignore") expected = DatetimeIndex(["1970-01-01 00:00:01"], tz="UTC") tm.assert_index_equal(result, expected) @@ -1583,20 +1581,20 @@ def test_to_datetime_with_apply(self, cache): # GH 5195 # with a format and coerce a single item to_datetime fails td = Series(["May 04", "Jun 02", "Dec 11"], index=[1, 2, 3]) - expected = pd.to_datetime(td, format="%b %y", cache=cache) - result = td.apply(pd.to_datetime, format="%b %y", cache=cache) + expected = to_datetime(td, format="%b %y", cache=cache) + result = td.apply(to_datetime, format="%b %y", cache=cache) tm.assert_series_equal(result, expected) td = Series(["May 04", "Jun 02", ""], index=[1, 2, 3]) msg = r"time data '' does not match format '%b %y' \(match\)" with pytest.raises(ValueError, match=msg): - pd.to_datetime(td, format="%b %y", errors="raise", cache=cache) + to_datetime(td, format="%b %y", errors="raise", cache=cache) with pytest.raises(ValueError, match=msg): - td.apply(pd.to_datetime, format="%b %y", errors="raise", cache=cache) - expected = pd.to_datetime(td, format="%b %y", errors="coerce", cache=cache) + td.apply(to_datetime, format="%b %y", errors="raise", cache=cache) + expected = to_datetime(td, format="%b %y", errors="coerce", cache=cache) result = td.apply( - lambda x: pd.to_datetime(x, format="%b %y", errors="coerce", cache=cache) + lambda x: to_datetime(x, format="%b %y", errors="coerce", cache=cache) ) tm.assert_series_equal(result, expected) @@ -1760,9 +1758,7 @@ def test_string_na_nat_conversion(self, cache): @pytest.mark.parametrize("cache", [True, False]) def test_dti_constructor_numpy_timeunits(self, cache, dtype): # GH 9114 - base = pd.to_datetime( - ["2000-01-01T00:00", "2000-01-02T00:00", "NaT"], cache=cache - ) + base = to_datetime(["2000-01-01T00:00", "2000-01-02T00:00", "NaT"], cache=cache) values = base.values.astype(dtype) @@ -1826,20 +1822,18 @@ def test_guess_datetime_format_for_array(self): class TestToDatetimeInferFormat: @pytest.mark.parametrize("cache", [True, False]) def test_to_datetime_infer_datetime_format_consistent_format(self, cache): - s = Series(pd.date_range("20000101", periods=50, freq="H")) + s = Series(date_range("20000101", periods=50, freq="H")) test_formats = ["%m-%d-%Y", "%m/%d/%Y %H:%M:%S.%f", "%Y-%m-%dT%H:%M:%S.%f"] for test_format in test_formats: s_as_dt_strings = s.apply(lambda x: x.strftime(test_format)) - with_format = pd.to_datetime( - s_as_dt_strings, format=test_format, cache=cache - ) - no_infer = pd.to_datetime( + with_format = to_datetime(s_as_dt_strings, format=test_format, cache=cache) + no_infer = to_datetime( s_as_dt_strings, infer_datetime_format=False, cache=cache ) - yes_infer = pd.to_datetime( + yes_infer = to_datetime( s_as_dt_strings, infer_datetime_format=True, cache=cache ) @@ -1859,15 +1853,15 @@ def test_to_datetime_infer_datetime_format_inconsistent_format(self, cache): # When the format is inconsistent, infer_datetime_format should just # fallback to the default parsing tm.assert_series_equal( - pd.to_datetime(s, infer_datetime_format=False, cache=cache), - pd.to_datetime(s, infer_datetime_format=True, cache=cache), + to_datetime(s, infer_datetime_format=False, cache=cache), + to_datetime(s, infer_datetime_format=True, cache=cache), ) s = Series(np.array(["Jan/01/2011", "Feb/01/2011", "Mar/01/2011"])) tm.assert_series_equal( - pd.to_datetime(s, infer_datetime_format=False, cache=cache), - pd.to_datetime(s, infer_datetime_format=True, cache=cache), + to_datetime(s, infer_datetime_format=False, cache=cache), + to_datetime(s, infer_datetime_format=True, cache=cache), ) @pytest.mark.parametrize("cache", [True, False]) @@ -1876,8 +1870,8 @@ def test_to_datetime_infer_datetime_format_series_with_nans(self, cache): np.array(["01/01/2011 00:00:00", np.nan, "01/03/2011 00:00:00", np.nan]) ) tm.assert_series_equal( - pd.to_datetime(s, infer_datetime_format=False, cache=cache), - pd.to_datetime(s, infer_datetime_format=True, cache=cache), + to_datetime(s, infer_datetime_format=False, cache=cache), + to_datetime(s, infer_datetime_format=True, cache=cache), ) @pytest.mark.parametrize("cache", [True, False]) @@ -1895,8 +1889,8 @@ def test_to_datetime_infer_datetime_format_series_start_with_nans(self, cache): ) tm.assert_series_equal( - pd.to_datetime(s, infer_datetime_format=False, cache=cache), - pd.to_datetime(s, infer_datetime_format=True, cache=cache), + to_datetime(s, infer_datetime_format=False, cache=cache), + to_datetime(s, infer_datetime_format=True, cache=cache), ) @pytest.mark.parametrize( @@ -1922,10 +1916,8 @@ def test_to_datetime_iso8601_noleading_0s(self, cache): Timestamp("2015-03-03"), ] ) - tm.assert_series_equal(pd.to_datetime(s, cache=cache), expected) - tm.assert_series_equal( - pd.to_datetime(s, format="%Y-%m-%d", cache=cache), expected - ) + tm.assert_series_equal(to_datetime(s, cache=cache), expected) + tm.assert_series_equal(to_datetime(s, format="%Y-%m-%d", cache=cache), expected) class TestDaysInMonth: @@ -2268,7 +2260,7 @@ def epochs(epoch_1960, request): @pytest.fixture def julian_dates(): - return pd.date_range("2014-1-1", periods=10).to_julian_date().values + return date_range("2014-1-1", periods=10).to_julian_date().values class TestOrigin: @@ -2276,33 +2268,33 @@ def test_to_basic(self, julian_dates): # gh-11276, gh-11745 # for origin as julian - result = Series(pd.to_datetime(julian_dates, unit="D", origin="julian")) + result = Series(to_datetime(julian_dates, unit="D", origin="julian")) expected = Series( - pd.to_datetime(julian_dates - Timestamp(0).to_julian_date(), unit="D") + to_datetime(julian_dates - Timestamp(0).to_julian_date(), unit="D") ) tm.assert_series_equal(result, expected) - result = Series(pd.to_datetime([0, 1, 2], unit="D", origin="unix")) + result = Series(to_datetime([0, 1, 2], unit="D", origin="unix")) expected = Series( [Timestamp("1970-01-01"), Timestamp("1970-01-02"), Timestamp("1970-01-03")] ) tm.assert_series_equal(result, expected) # default - result = Series(pd.to_datetime([0, 1, 2], unit="D")) + result = Series(to_datetime([0, 1, 2], unit="D")) expected = Series( [Timestamp("1970-01-01"), Timestamp("1970-01-02"), Timestamp("1970-01-03")] ) tm.assert_series_equal(result, expected) def test_julian_round_trip(self): - result = pd.to_datetime(2456658, origin="julian", unit="D") + result = to_datetime(2456658, origin="julian", unit="D") assert result.to_julian_date() == 2456658 # out-of-bounds msg = "1 is Out of Bounds for origin='julian'" with pytest.raises(ValueError, match=msg): - pd.to_datetime(1, origin="julian", unit="D") + to_datetime(1, origin="julian", unit="D") def test_invalid_unit(self, units, julian_dates): @@ -2310,17 +2302,17 @@ def test_invalid_unit(self, units, julian_dates): if units != "D": msg = "unit must be 'D' for origin='julian'" with pytest.raises(ValueError, match=msg): - pd.to_datetime(julian_dates, unit=units, origin="julian") + to_datetime(julian_dates, unit=units, origin="julian") def test_invalid_origin(self): # need to have a numeric specified msg = "it must be numeric with a unit specified" with pytest.raises(ValueError, match=msg): - pd.to_datetime("2005-01-01", origin="1960-01-01") + to_datetime("2005-01-01", origin="1960-01-01") with pytest.raises(ValueError, match=msg): - pd.to_datetime("2005-01-01", origin="1960-01-01", unit="D") + to_datetime("2005-01-01", origin="1960-01-01", unit="D") def test_epoch(self, units, epochs, epoch_1960, units_from_epochs): @@ -2328,7 +2320,7 @@ def test_epoch(self, units, epochs, epoch_1960, units_from_epochs): [pd.Timedelta(x, unit=units) + epoch_1960 for x in units_from_epochs] ) - result = Series(pd.to_datetime(units_from_epochs, unit=units, origin=epochs)) + result = Series(to_datetime(units_from_epochs, unit=units, origin=epochs)) tm.assert_series_equal(result, expected) @pytest.mark.parametrize( @@ -2344,12 +2336,12 @@ def test_invalid_origins(self, origin, exc, units, units_from_epochs): msg = f"origin {origin} (is Out of Bounds|cannot be converted to a Timestamp)" with pytest.raises(exc, match=msg): - pd.to_datetime(units_from_epochs, unit=units, origin=origin) + to_datetime(units_from_epochs, unit=units, origin=origin) def test_invalid_origins_tzinfo(self): # GH16842 with pytest.raises(ValueError, match="must be tz-naive"): - pd.to_datetime(1, unit="D", origin=datetime(2000, 1, 1, tzinfo=pytz.utc)) + to_datetime(1, unit="D", origin=datetime(2000, 1, 1, tzinfo=pytz.utc)) @pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"]) def test_to_datetime_out_of_bounds_with_format_arg(self, format): @@ -2362,15 +2354,15 @@ def test_processing_order(self): # make sure we handle out-of-bounds *before* # constructing the dates - result = pd.to_datetime(200 * 365, unit="D") + result = to_datetime(200 * 365, unit="D") expected = Timestamp("2169-11-13 00:00:00") assert result == expected - result = pd.to_datetime(200 * 365, unit="D", origin="1870-01-01") + result = to_datetime(200 * 365, unit="D", origin="1870-01-01") expected = Timestamp("2069-11-13 00:00:00") assert result == expected - result = pd.to_datetime(300 * 365, unit="D", origin="1870-01-01") + result = to_datetime(300 * 365, unit="D", origin="1870-01-01") expected = Timestamp("2169-10-20 00:00:00") assert result == expected @@ -2422,7 +2414,7 @@ def test_nullable_integer_to_datetime(): ser = ser.astype("Int64") ser_copy = ser.copy() - res = pd.to_datetime(ser, unit="ns") + res = to_datetime(ser, unit="ns") expected = Series( [ @@ -2440,7 +2432,7 @@ def test_nullable_integer_to_datetime(): @pytest.mark.parametrize("klass", [np.array, list]) def test_na_to_datetime(nulls_fixture, klass): - result = pd.to_datetime(klass([nulls_fixture])) + result = to_datetime(klass([nulls_fixture])) assert result[0] is pd.NaT @@ -2451,26 +2443,26 @@ def test_empty_string_datetime_coerce__format(): format = "%m/%d/%Y" # coerce empty string to pd.NaT - result = pd.to_datetime(td, format=format, errors="coerce") + result = to_datetime(td, format=format, errors="coerce") expected = Series(["2016-03-24", "2016-03-25", pd.NaT], dtype="datetime64[ns]") tm.assert_series_equal(expected, result) # raise an exception in case a format is given with pytest.raises(ValueError, match="does not match format"): - result = pd.to_datetime(td, format=format, errors="raise") + result = to_datetime(td, format=format, errors="raise") # don't raise an expection in case no format is given - result = pd.to_datetime(td, errors="raise") + result = to_datetime(td, errors="raise") tm.assert_series_equal(result, expected) def test_empty_string_datetime_coerce__unit(): # GH13044 # coerce empty string to pd.NaT - result = pd.to_datetime([1, ""], unit="s", errors="coerce") + result = to_datetime([1, ""], unit="s", errors="coerce") expected = DatetimeIndex(["1970-01-01 00:00:01", "NaT"], dtype="datetime64[ns]") tm.assert_index_equal(expected, result) # verify that no exception is raised even when errors='raise' is set - result = pd.to_datetime([1, ""], unit="s", errors="raise") + result = to_datetime([1, ""], unit="s", errors="raise") tm.assert_index_equal(expected, result) diff --git a/pandas/tests/tools/test_to_numeric.py b/pandas/tests/tools/test_to_numeric.py index 80446e464985c..def5e33ac9ef2 100644 --- a/pandas/tests/tools/test_to_numeric.py +++ b/pandas/tests/tools/test_to_numeric.py @@ -580,7 +580,7 @@ def test_downcast_uint64(ser, expected): # see gh-14422: # BUG: to_numeric doesn't work uint64 numbers - result = pd.to_numeric(ser, downcast="unsigned") + result = to_numeric(ser, downcast="unsigned") tm.assert_series_equal(result, expected) @@ -635,8 +635,8 @@ def test_downcast_empty(dc1, dc2): # GH32493 tm.assert_numpy_array_equal( - pd.to_numeric([], downcast=dc1), - pd.to_numeric([], downcast=dc2), + to_numeric([], downcast=dc1), + to_numeric([], downcast=dc2), check_dtype=False, ) @@ -761,6 +761,6 @@ def test_to_numeric_from_nullable_string(values, expected): ) def test_downcast_nullable_numeric(data, input_dtype, downcast, expected_dtype): arr = pd.array(data, dtype=input_dtype) - result = pd.to_numeric(arr, downcast=downcast) + result = to_numeric(arr, downcast=downcast) expected = pd.array(data, dtype=expected_dtype) tm.assert_extension_array_equal(result, expected) diff --git a/pandas/tests/tools/test_to_timedelta.py b/pandas/tests/tools/test_to_timedelta.py index c0196549cee33..73cb288fc4a05 100644 --- a/pandas/tests/tools/test_to_timedelta.py +++ b/pandas/tests/tools/test_to_timedelta.py @@ -148,7 +148,7 @@ def test_to_timedelta_invalid(self): ) def test_unambiguous_timedelta_values(self, val, warning): # GH36666 Deprecate use of strings denoting units with 'M', 'Y', 'm' or 'y' - # in pd.to_timedelta + # in to_timedelta with tm.assert_produces_warning(warning, check_stacklevel=False): to_timedelta(val) @@ -165,37 +165,37 @@ def test_to_timedelta_on_missing_values(self): # GH5438 timedelta_NaT = np.timedelta64("NaT") - actual = pd.to_timedelta(Series(["00:00:01", np.nan])) + actual = to_timedelta(Series(["00:00:01", np.nan])) expected = Series( [np.timedelta64(1000000000, "ns"), timedelta_NaT], dtype=" None: match = pattern.search(contents) if match is None: continue - if match.group(2) is not None: + if match.group(2) is not None and match.group(2).decode() not in BLOCK_LIST: raise AssertionError( ERROR_MESSAGE.format(class_name=match.group(2).decode(), path=str(path)) ) - if match.group(4) is not None: + if match.group(4) is not None and match.group(4).decode() not in BLOCK_LIST: raise AssertionError( ERROR_MESSAGE.format(class_name=match.group(4).decode(), path=str(path)) )