Skip to content

Commit fe680fc

Browse files
jbrockmendelJulianWgs
authored andcommitted
TYP: datetimelike (pandas-dev#41830)
1 parent 54546a8 commit fe680fc

File tree

6 files changed

+26
-26
lines changed

6 files changed

+26
-26
lines changed

pandas/core/arrays/datetimes.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
if TYPE_CHECKING:
8484
from typing import Literal
8585

86+
from pandas import DataFrame
8687
from pandas.core.arrays import (
8788
PeriodArray,
8889
TimedeltaArray,
@@ -1256,7 +1257,7 @@ def day_name(self, locale=None):
12561257
return result
12571258

12581259
@property
1259-
def time(self):
1260+
def time(self) -> np.ndarray:
12601261
"""
12611262
Returns numpy array of datetime.time. The time part of the Timestamps.
12621263
"""
@@ -1268,15 +1269,15 @@ def time(self):
12681269
return ints_to_pydatetime(timestamps, box="time")
12691270

12701271
@property
1271-
def timetz(self):
1272+
def timetz(self) -> np.ndarray:
12721273
"""
12731274
Returns numpy array of datetime.time also containing timezone
12741275
information. The time part of the Timestamps.
12751276
"""
12761277
return ints_to_pydatetime(self.asi8, self.tz, box="time")
12771278

12781279
@property
1279-
def date(self):
1280+
def date(self) -> np.ndarray:
12801281
"""
12811282
Returns numpy array of python datetime.date objects (namely, the date
12821283
part of Timestamps without timezone information).
@@ -1288,7 +1289,7 @@ def date(self):
12881289

12891290
return ints_to_pydatetime(timestamps, box="date")
12901291

1291-
def isocalendar(self):
1292+
def isocalendar(self) -> DataFrame:
12921293
"""
12931294
Returns a DataFrame with the year, week, and day calculated according to
12941295
the ISO 8601 standard.
@@ -1871,7 +1872,7 @@ def weekofyear(self):
18711872
""",
18721873
)
18731874

1874-
def to_julian_date(self):
1875+
def to_julian_date(self) -> np.ndarray:
18751876
"""
18761877
Convert Datetime Array to float64 ndarray of Julian Dates.
18771878
0 Julian date is noon January 1, 4713 BC.

pandas/core/arrays/period.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1072,11 +1072,10 @@ def dt64arr_to_periodarr(data, freq, tz=None):
10721072
elif isinstance(data, ABCSeries):
10731073
data, freq = data._values, data.dt.freq
10741074

1075-
freq = Period._maybe_convert_freq(freq)
1076-
1077-
if isinstance(data, (ABCIndex, ABCSeries)):
1075+
elif isinstance(data, (ABCIndex, ABCSeries)):
10781076
data = data._values
10791077

1078+
freq = Period._maybe_convert_freq(freq)
10801079
base = freq._period_dtype_code
10811080
return c_dt64arr_to_periodarr(data.view("i8"), base, tz), freq
10821081

@@ -1138,7 +1137,7 @@ def _range_from_fields(
11381137
minute=None,
11391138
second=None,
11401139
freq=None,
1141-
):
1140+
) -> tuple[np.ndarray, BaseOffset]:
11421141
if hour is None:
11431142
hour = 0
11441143
if minute is None:
@@ -1176,7 +1175,7 @@ def _range_from_fields(
11761175
return np.array(ordinals, dtype=np.int64), freq
11771176

11781177

1179-
def _make_field_arrays(*fields):
1178+
def _make_field_arrays(*fields) -> list[np.ndarray]:
11801179
length = None
11811180
for x in fields:
11821181
if isinstance(x, (list, np.ndarray, ABCSeries)):

pandas/core/arrays/timedeltas.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
from pandas.core.ops.common import unpack_zerodim_and_defer
7171

7272
if TYPE_CHECKING:
73+
from pandas import DataFrame
7374
from pandas.core.arrays import (
7475
DatetimeArray,
7576
PeriodArray,
@@ -880,14 +881,14 @@ def to_pytimedelta(self) -> np.ndarray:
880881
)
881882

882883
@property
883-
def components(self):
884+
def components(self) -> DataFrame:
884885
"""
885886
Return a dataframe of the components (days, hours, minutes,
886887
seconds, milliseconds, microseconds, nanoseconds) of the Timedeltas.
887888
888889
Returns
889890
-------
890-
a DataFrame
891+
DataFrame
891892
"""
892893
from pandas import DataFrame
893894

pandas/core/indexes/timedeltas.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@
4040
)
4141
@inherit_names(
4242
[
43-
"_bool_ops",
44-
"_object_ops",
45-
"_field_ops",
46-
"_datetimelike_ops",
47-
"_datetimelike_methods",
48-
"_other_ops",
4943
"components",
5044
"to_pytimedelta",
5145
"sum",
@@ -163,7 +157,7 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
163157
"""
164158
Can we compare values of the given dtype to our own?
165159
"""
166-
return is_timedelta64_dtype(dtype)
160+
return is_timedelta64_dtype(dtype) # aka self._data._is_recognized_dtype
167161

168162
# -------------------------------------------------------------------
169163
# Indexing Methods

pandas/tests/series/accessors/test_cat_accessor.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66
from pandas import (
77
Categorical,
88
DataFrame,
9-
DatetimeIndex,
109
Index,
1110
Series,
12-
TimedeltaIndex,
1311
Timestamp,
1412
date_range,
1513
period_range,
1614
timedelta_range,
1715
)
1816
import pandas._testing as tm
19-
from pandas.core.arrays import PeriodArray
17+
from pandas.core.arrays import (
18+
DatetimeArray,
19+
PeriodArray,
20+
TimedeltaArray,
21+
)
2022
from pandas.core.arrays.categorical import CategoricalAccessor
2123
from pandas.core.indexes.accessors import Properties
2224

@@ -178,9 +180,9 @@ def test_dt_accessor_api_for_categorical(self):
178180
get_ops = lambda x: x._datetimelike_ops
179181

180182
test_data = [
181-
("Datetime", get_ops(DatetimeIndex), s_dr, c_dr),
183+
("Datetime", get_ops(DatetimeArray), s_dr, c_dr),
182184
("Period", get_ops(PeriodArray), s_pr, c_pr),
183-
("Timedelta", get_ops(TimedeltaIndex), s_tdr, c_tdr),
185+
("Timedelta", get_ops(TimedeltaArray), s_tdr, c_tdr),
184186
]
185187

186188
assert isinstance(c_dr.dt, Properties)

pandas/tests/series/accessors/test_dt_accessor.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
timedelta_range,
3333
)
3434
import pandas._testing as tm
35-
from pandas.core.arrays import PeriodArray
35+
from pandas.core.arrays import (
36+
PeriodArray,
37+
TimedeltaArray,
38+
)
3639
import pandas.core.common as com
3740

3841

@@ -59,7 +62,7 @@ def test_dt_namespace_accessor(self):
5962
"month_name",
6063
"isocalendar",
6164
]
62-
ok_for_td = TimedeltaIndex._datetimelike_ops
65+
ok_for_td = TimedeltaArray._datetimelike_ops
6366
ok_for_td_methods = [
6467
"components",
6568
"to_pytimedelta",

0 commit comments

Comments
 (0)