Skip to content

Commit b9e0935

Browse files
authored
CLN: remove ABCPeriod (#34218)
1 parent 46d0d64 commit b9e0935

File tree

8 files changed

+22
-28
lines changed

8 files changed

+22
-28
lines changed

pandas/_libs/index.pyx

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ cnp.import_array()
2222
from pandas._libs cimport util
2323

2424
from pandas._libs.tslibs.nattype cimport c_NaT as NaT
25-
from pandas._libs.tslibs.base cimport ABCTimestamp, ABCTimedelta, ABCPeriod
25+
from pandas._libs.tslibs.base cimport ABCTimestamp, ABCTimedelta
26+
from pandas._libs.tslibs.period cimport is_period_object
2627

2728
from pandas._libs.hashtable cimport HashTable
2829

@@ -479,7 +480,7 @@ cdef class PeriodEngine(Int64Engine):
479480
cdef int64_t _unbox_scalar(self, scalar) except? -1:
480481
if scalar is NaT:
481482
return scalar.value
482-
if isinstance(scalar, ABCPeriod):
483+
if is_period_object(scalar):
483484
# NB: we assume that we have the correct freq here.
484485
return scalar.ordinal
485486
raise TypeError(scalar)

pandas/_libs/lib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ from pandas._libs.tslibs.nattype cimport (
7474
from pandas._libs.tslibs.conversion cimport convert_to_tsobject
7575
from pandas._libs.tslibs.timedeltas cimport convert_to_timedelta64
7676
from pandas._libs.tslibs.timezones cimport get_timezone, tz_compare
77-
from pandas._libs.tslibs.base cimport is_period_object
77+
from pandas._libs.tslibs.period cimport is_period_object
7878

7979
from pandas._libs.missing cimport (
8080
checknull,

pandas/_libs/tslibs/base.pxd

-7
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,3 @@ cdef class ABCTimedelta(timedelta):
66

77
cdef class ABCTimestamp(datetime):
88
pass
9-
10-
11-
cdef class ABCPeriod:
12-
pass
13-
14-
15-
cdef bint is_period_object(object obj)

pandas/_libs/tslibs/base.pyx

-8
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,3 @@ cdef class ABCTimedelta(timedelta):
1414

1515
cdef class ABCTimestamp(datetime):
1616
pass
17-
18-
19-
cdef class ABCPeriod:
20-
pass
21-
22-
23-
cdef bint is_period_object(object obj):
24-
return isinstance(obj, ABCPeriod)

pandas/_libs/tslibs/conversion.pyx

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ from cpython.datetime cimport (datetime, time, tzinfo,
1313
PyDateTime_IMPORT)
1414
PyDateTime_IMPORT
1515

16-
from pandas._libs.tslibs.base cimport ABCTimestamp, is_period_object
16+
from pandas._libs.tslibs.base cimport ABCTimestamp
1717

1818
from pandas._libs.tslibs.np_datetime cimport (
1919
check_dts_bounds, npy_datetimestruct, pandas_datetime_to_datetimestruct,
@@ -290,10 +290,11 @@ cdef convert_to_tsobject(object ts, object tz, object unit,
290290
# Keep the converter same as PyDateTime's
291291
ts = datetime.combine(ts, time())
292292
return convert_datetime_to_tsobject(ts, tz)
293-
elif is_period_object(ts):
294-
raise ValueError("Cannot convert Period to Timestamp "
295-
"unambiguously. Use to_timestamp")
296293
else:
294+
from .period import Period
295+
if isinstance(ts, Period):
296+
raise ValueError("Cannot convert Period to Timestamp "
297+
"unambiguously. Use to_timestamp")
297298
raise TypeError(f'Cannot convert input [{ts}] of type {type(ts)} to '
298299
f'Timestamp')
299300

pandas/_libs/tslibs/nattype.pyx

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ from pandas._libs.tslibs.np_datetime cimport (
2929
get_timedelta64_value,
3030
)
3131
cimport pandas._libs.tslibs.util as util
32-
from pandas._libs.tslibs.base cimport is_period_object
3332

3433

3534
# ----------------------------------------------------------------------
@@ -149,7 +148,7 @@ cdef class _NaT(datetime):
149148
elif util.is_offset_object(other):
150149
return c_NaT
151150

152-
elif util.is_integer_object(other) or is_period_object(other):
151+
elif util.is_integer_object(other):
153152
# For Period compat
154153
# TODO: the integer behavior is deprecated, remove it
155154
return c_NaT
@@ -163,6 +162,7 @@ cdef class _NaT(datetime):
163162
return result
164163
raise TypeError(f"Cannot add NaT to ndarray with dtype {other.dtype}")
165164

165+
# Includes Period going through here
166166
return NotImplemented
167167

168168
def __sub__(self, other):
@@ -185,7 +185,7 @@ cdef class _NaT(datetime):
185185
elif util.is_offset_object(other):
186186
return c_NaT
187187

188-
elif util.is_integer_object(other) or is_period_object(other):
188+
elif util.is_integer_object(other):
189189
# For Period compat
190190
# TODO: the integer behavior is deprecated, remove it
191191
return c_NaT
@@ -216,6 +216,7 @@ cdef class _NaT(datetime):
216216
f"Cannot subtract NaT from ndarray with dtype {other.dtype}"
217217
)
218218

219+
# Includes Period going through here
219220
return NotImplemented
220221

221222
def __pos__(self):

pandas/_libs/tslibs/period.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cdef bint is_period_object(object obj)

pandas/_libs/tslibs/period.pyx

+8-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ cdef extern from "src/datetime/np_datetime.h":
3737

3838
cimport pandas._libs.tslibs.util as util
3939

40-
from pandas._libs.tslibs.base cimport ABCPeriod, is_period_object
41-
4240
from pandas._libs.tslibs.timestamps import Timestamp
4341
from pandas._libs.tslibs.timezones cimport is_utc, is_tzlocal, get_dst_info
4442
from pandas._libs.tslibs.timedeltas import Timedelta
@@ -1533,7 +1531,7 @@ class IncompatibleFrequency(ValueError):
15331531
pass
15341532

15351533

1536-
cdef class _Period(ABCPeriod):
1534+
cdef class _Period:
15371535

15381536
cdef readonly:
15391537
int64_t ordinal
@@ -1657,8 +1655,11 @@ cdef class _Period(ABCPeriod):
16571655
raise IncompatibleFrequency(msg)
16581656
# GH 23915 - mul by base freq since __add__ is agnostic of n
16591657
return (self.ordinal - other.ordinal) * self.freq.base
1658+
elif other is NaT:
1659+
return NaT
16601660
return NotImplemented
16611661
elif is_period_object(other):
1662+
# this can be reached via __rsub__ because of cython rules
16621663
if self is NaT:
16631664
return NaT
16641665
return NotImplemented
@@ -2463,6 +2464,10 @@ class Period(_Period):
24632464
return cls._from_ordinal(ordinal, freq)
24642465

24652466

2467+
cdef bint is_period_object(object obj):
2468+
return isinstance(obj, _Period)
2469+
2470+
24662471
cdef int64_t _ordinal_from_fields(int year, int month, quarter, int day,
24672472
int hour, int minute, int second, freq):
24682473
base, mult = get_freq_code(freq)

0 commit comments

Comments
 (0)