Skip to content

CLN: remove ABCPeriod #34218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ cnp.import_array()
from pandas._libs cimport util

from pandas._libs.tslibs.nattype cimport c_NaT as NaT
from pandas._libs.tslibs.base cimport ABCTimestamp, ABCTimedelta, ABCPeriod
from pandas._libs.tslibs.base cimport ABCTimestamp, ABCTimedelta
from pandas._libs.tslibs.period cimport is_period_object

from pandas._libs.hashtable cimport HashTable

Expand Down Expand Up @@ -479,7 +480,7 @@ cdef class PeriodEngine(Int64Engine):
cdef int64_t _unbox_scalar(self, scalar) except? -1:
if scalar is NaT:
return scalar.value
if isinstance(scalar, ABCPeriod):
if is_period_object(scalar):
# NB: we assume that we have the correct freq here.
return scalar.ordinal
raise TypeError(scalar)
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ from pandas._libs.tslibs.nattype cimport (
from pandas._libs.tslibs.conversion cimport convert_to_tsobject
from pandas._libs.tslibs.timedeltas cimport convert_to_timedelta64
from pandas._libs.tslibs.timezones cimport get_timezone, tz_compare
from pandas._libs.tslibs.base cimport is_period_object
from pandas._libs.tslibs.period cimport is_period_object

from pandas._libs.missing cimport (
checknull,
Expand Down
7 changes: 0 additions & 7 deletions pandas/_libs/tslibs/base.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,3 @@ cdef class ABCTimedelta(timedelta):

cdef class ABCTimestamp(datetime):
pass


cdef class ABCPeriod:
pass


cdef bint is_period_object(object obj)
8 changes: 0 additions & 8 deletions pandas/_libs/tslibs/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,3 @@ cdef class ABCTimedelta(timedelta):

cdef class ABCTimestamp(datetime):
pass


cdef class ABCPeriod:
pass


cdef bint is_period_object(object obj):
return isinstance(obj, ABCPeriod)
9 changes: 5 additions & 4 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ from cpython.datetime cimport (datetime, time, tzinfo,
PyDateTime_IMPORT)
PyDateTime_IMPORT

from pandas._libs.tslibs.base cimport ABCTimestamp, is_period_object
from pandas._libs.tslibs.base cimport ABCTimestamp

from pandas._libs.tslibs.np_datetime cimport (
check_dts_bounds, npy_datetimestruct, pandas_datetime_to_datetimestruct,
Expand Down Expand Up @@ -290,10 +290,11 @@ cdef convert_to_tsobject(object ts, object tz, object unit,
# Keep the converter same as PyDateTime's
ts = datetime.combine(ts, time())
return convert_datetime_to_tsobject(ts, tz)
elif is_period_object(ts):
raise ValueError("Cannot convert Period to Timestamp "
"unambiguously. Use to_timestamp")
else:
from .period import Period
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can't use the is_period_object here, does tihs import make any perf difference?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cimporting is_period_object at the module level raises ImportError at import-time. The rules for when circular cimports are allowed are not clear to me.

if isinstance(ts, Period):
raise ValueError("Cannot convert Period to Timestamp "
"unambiguously. Use to_timestamp")
raise TypeError(f'Cannot convert input [{ts}] of type {type(ts)} to '
f'Timestamp')

Expand Down
7 changes: 4 additions & 3 deletions pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ from pandas._libs.tslibs.np_datetime cimport (
get_timedelta64_value,
)
cimport pandas._libs.tslibs.util as util
from pandas._libs.tslibs.base cimport is_period_object


# ----------------------------------------------------------------------
Expand Down Expand Up @@ -149,7 +148,7 @@ cdef class _NaT(datetime):
elif util.is_offset_object(other):
return c_NaT

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

# Includes Period going through here
return NotImplemented

def __sub__(self, other):
Expand All @@ -185,7 +185,7 @@ cdef class _NaT(datetime):
elif util.is_offset_object(other):
return c_NaT

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

# Includes Period going through here
return NotImplemented

def __pos__(self):
Expand Down
1 change: 1 addition & 0 deletions pandas/_libs/tslibs/period.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cdef bint is_period_object(object obj)
11 changes: 8 additions & 3 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ cdef extern from "src/datetime/np_datetime.h":

cimport pandas._libs.tslibs.util as util

from pandas._libs.tslibs.base cimport ABCPeriod, is_period_object

from pandas._libs.tslibs.timestamps import Timestamp
from pandas._libs.tslibs.timezones cimport is_utc, is_tzlocal, get_dst_info
from pandas._libs.tslibs.timedeltas import Timedelta
Expand Down Expand Up @@ -1533,7 +1531,7 @@ class IncompatibleFrequency(ValueError):
pass


cdef class _Period(ABCPeriod):
cdef class _Period:

cdef readonly:
int64_t ordinal
Expand Down Expand Up @@ -1657,8 +1655,11 @@ cdef class _Period(ABCPeriod):
raise IncompatibleFrequency(msg)
# GH 23915 - mul by base freq since __add__ is agnostic of n
return (self.ordinal - other.ordinal) * self.freq.base
elif other is NaT:
return NaT
return NotImplemented
elif is_period_object(other):
# this can be reached via __rsub__ because of cython rules
if self is NaT:
return NaT
return NotImplemented
Expand Down Expand Up @@ -2463,6 +2464,10 @@ class Period(_Period):
return cls._from_ordinal(ordinal, freq)


cdef bint is_period_object(object obj):
return isinstance(obj, _Period)


cdef int64_t _ordinal_from_fields(int year, int month, quarter, int day,
int hour, int minute, int second, freq):
base, mult = get_freq_code(freq)
Expand Down