diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 306636278bcbe..4c7d03d51e909 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -157,8 +157,8 @@ cdef _wrap_timedelta_result(result): """ if PyDelta_Check(result): # convert Timedelta back to a Tick - from pandas.tseries.offsets import _delta_to_tick - return _delta_to_tick(result) + from pandas.tseries.offsets import delta_to_tick + return delta_to_tick(result) return result diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 99d9d69d66ec2..2d9522b00627c 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -39,7 +39,7 @@ import pandas.core.common as com from pandas.tseries import frequencies -from pandas.tseries.offsets import DateOffset, Tick, _delta_to_tick +from pandas.tseries.offsets import DateOffset, Tick, delta_to_tick def _field_accessor(name: str, alias: int, docstring=None): @@ -487,7 +487,7 @@ def _time_shift(self, periods, freq=None): def _box_func(self): return lambda x: Period._from_ordinal(ordinal=x, freq=self.freq) - def asfreq(self, freq=None, how="E") -> "PeriodArray": + def asfreq(self, freq=None, how: str = "E") -> "PeriodArray": """ Convert the Period Array/Index to the specified frequency `freq`. @@ -759,7 +759,7 @@ def raise_on_incompatible(left, right): elif isinstance(right, (ABCPeriodIndex, PeriodArray, Period, DateOffset)): other_freq = right.freqstr else: - other_freq = _delta_to_tick(Timedelta(right)).freqstr + other_freq = delta_to_tick(Timedelta(right)).freqstr msg = DIFFERENT_FREQ.format( cls=type(left).__name__, own_freq=left.freqstr, other_freq=other_freq diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index d44fed9e097e7..e2477932e29e4 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -429,7 +429,6 @@ def __new__(cls, data: "Series"): # we need to choose which parent (datetime or timedelta) is # appropriate. Since we're checking the dtypes anyway, we'll just # do all the validation here. - from pandas import Series if not isinstance(data, ABCSeries): raise TypeError( @@ -438,7 +437,7 @@ def __new__(cls, data: "Series"): orig = data if is_categorical_dtype(data) else None if orig is not None: - data = Series( + data = data._constructor( orig.array, name=orig.name, copy=False, diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 1f565828ec7a5..eb01053aacad9 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -6,9 +6,8 @@ from pandas._libs import index as libindex from pandas._libs.lib import no_default -from pandas._libs.tslibs import frequencies as libfrequencies, resolution +from pandas._libs.tslibs import Period, frequencies as libfrequencies, resolution from pandas._libs.tslibs.parsing import parse_time_string -from pandas._libs.tslibs.period import Period from pandas._typing import DtypeObj, Label from pandas.util._decorators import Appender, cache_readonly, doc diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 765b948f13e96..083586645196d 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -29,16 +29,7 @@ @inherit_names( - [ - "_box_values", - "__neg__", - "__pos__", - "__abs__", - "total_seconds", - "round", - "floor", - "ceil", - ] + ["__neg__", "__pos__", "__abs__", "total_seconds", "round", "floor", "ceil"] + TimedeltaArray._field_ops, TimedeltaArray, wrap=True, diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 185b0f4da2627..1ffc6d6b60967 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -218,7 +218,7 @@ def get_block_values_for_json(self) -> np.ndarray: """ This is used in the JSON C code. """ - # TODO(2DEA): reshape will be unnecessary with 2D EAs + # TODO(EA2D): reshape will be unnecessary with 2D EAs return np.asarray(self.values).reshape(self.shape) @property @@ -353,6 +353,7 @@ def apply(self, func, **kwargs) -> List["Block"]: def _split_op_result(self, result) -> List["Block"]: # See also: split_and_operate if is_extension_array_dtype(result) and result.ndim > 1: + # TODO(EA2D): unnecessary with 2D EAs # if we get a 2D ExtensionArray, we need to split it into 1D pieces nbs = [] for i, loc in enumerate(self.mgr_locs): @@ -1560,7 +1561,7 @@ def __init__(self, values, placement, ndim=None): super().__init__(values, placement, ndim=ndim) if self.ndim == 2 and len(self.mgr_locs) != 1: - # TODO(2DEA): check unnecessary with 2D EAs + # TODO(EA2D): check unnecessary with 2D EAs raise AssertionError("block.size != values.size") @property @@ -2307,7 +2308,7 @@ def equals(self, other) -> bool: def quantile(self, qs, interpolation="linear", axis=0): naive = self.values.view("M8[ns]") - # kludge for 2D block with 1D values + # TODO(EA2D): kludge for 2D block with 1D values naive = naive.reshape(self.shape) blk = self.make_block(naive) @@ -2432,7 +2433,7 @@ def f(mask, val, idx): copy=copy, ) if isinstance(values, np.ndarray): - # TODO: allow EA once reshape is supported + # TODO(EA2D): allow EA once reshape is supported values = values.reshape(shape) return values diff --git a/pandas/core/internals/concat.py b/pandas/core/internals/concat.py index 720e6799a3bf3..1cdfa07827d84 100644 --- a/pandas/core/internals/concat.py +++ b/pandas/core/internals/concat.py @@ -4,7 +4,7 @@ import numpy as np -from pandas._libs import internals as libinternals, tslibs +from pandas._libs import NaT, internals as libinternals from pandas.util._decorators import cache_readonly from pandas.core.dtypes.cast import maybe_promote @@ -395,7 +395,7 @@ def _get_empty_dtype_and_na(join_units): # GH-25014. We use NaT instead of iNaT, since this eventually # ends up in DatetimeArray.take, which does not allow iNaT. dtype = upcast_classes["datetimetz"] - return dtype[0], tslibs.NaT + return dtype[0], NaT elif "datetime" in upcast_classes: return np.dtype("M8[ns]"), np.datetime64("NaT", "ns") elif "timedelta" in upcast_classes: diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 1e93597d92a5d..bfef4f63e2e8a 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -323,7 +323,7 @@ def _gotitem(self, key, ndim: int, subset=None): Parameters ---------- key : string / list of selections - ndim : 1,2 + ndim : {1, 2} requested ndim of result subset : object, default None subset to act on diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index beb16c9549cc4..2c363ee82e5c1 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -313,7 +313,9 @@ def test_subtraction_ops(self): tm.assert_index_equal(result, expected, check_names=False) result = dti - td - expected = DatetimeIndex(["20121231", "20130101", "20130102"], name="bar") + expected = DatetimeIndex( + ["20121231", "20130101", "20130102"], freq="D", name="bar" + ) tm.assert_index_equal(result, expected, check_names=False) result = dt - tdi @@ -401,7 +403,9 @@ def _check(result, expected): _check(result, expected) result = dti_tz - td - expected = DatetimeIndex(["20121231", "20130101", "20130102"], tz="US/Eastern") + expected = DatetimeIndex( + ["20121231", "20130101", "20130102"], tz="US/Eastern", freq="D" + ) tm.assert_index_equal(result, expected) def test_dti_tdi_numeric_ops(self): diff --git a/pandas/tests/series/methods/test_replace.py b/pandas/tests/series/methods/test_replace.py index 1c54e2b988219..330c682216f53 100644 --- a/pandas/tests/series/methods/test_replace.py +++ b/pandas/tests/series/methods/test_replace.py @@ -186,11 +186,7 @@ def check_replace(to_rep, val, expected): check_replace(tr, v, e) # test an object with dates + floats + integers + strings - dr = ( - pd.date_range("1/1/2001", "1/10/2001", freq="D") - .to_series() - .reset_index(drop=True) - ) + dr = pd.Series(pd.date_range("1/1/2001", "1/10/2001", freq="D")) result = dr.astype(object).replace([dr[0], dr[1], dr[2]], [1.0, 2, "a"]) expected = pd.Series([1.0, 2, "a"] + dr[3:].tolist(), dtype=object) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/tseries/offsets/test_ticks.py b/pandas/tests/tseries/offsets/test_ticks.py index 297e5c3178379..89f5f362e51f3 100644 --- a/pandas/tests/tseries/offsets/test_ticks.py +++ b/pandas/tests/tseries/offsets/test_ticks.py @@ -33,11 +33,11 @@ def test_apply_ticks(): def test_delta_to_tick(): delta = timedelta(3) - tick = offsets._delta_to_tick(delta) + tick = offsets.delta_to_tick(delta) assert tick == offsets.Day(3) td = Timedelta(nanoseconds=5) - tick = offsets._delta_to_tick(td) + tick = offsets.delta_to_tick(td) assert tick == Nano(5) diff --git a/pandas/tseries/offsets.py b/pandas/tseries/offsets.py index bc20d784c8dee..8ba10f56f163c 100644 --- a/pandas/tseries/offsets.py +++ b/pandas/tseries/offsets.py @@ -2548,7 +2548,7 @@ def __add__(self, other): if type(self) == type(other): return type(self)(self.n + other.n) else: - return _delta_to_tick(self.delta + other.delta) + return delta_to_tick(self.delta + other.delta) elif isinstance(other, Period): return other + self try: @@ -2635,7 +2635,7 @@ def is_anchored(self) -> bool: return False -def _delta_to_tick(delta: timedelta) -> Tick: +def delta_to_tick(delta: timedelta) -> Tick: if delta.microseconds == 0 and getattr(delta, "nanoseconds", 0) == 0: # nanoseconds only for pd.Timedelta if delta.seconds == 0: