Skip to content

Commit ff0cd29

Browse files
authored
CLN: update imports, avoid hasattr checks (#34072)
1 parent 9f746a7 commit ff0cd29

12 files changed

+32
-52
lines changed

pandas/_libs/lib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ from pandas._libs.tslibs.nattype cimport (
7575
from pandas._libs.tslibs.conversion cimport convert_to_tsobject
7676
from pandas._libs.tslibs.timedeltas cimport convert_to_timedelta64
7777
from pandas._libs.tslibs.timezones cimport get_timezone, tz_compare
78-
from pandas._libs.tslibs.period cimport is_period_object
78+
from pandas._libs.tslibs.base cimport is_period_object
7979

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

pandas/_libs/tslibs/conversion.pyx

+6-5
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
16+
from pandas._libs.tslibs.base cimport ABCTimestamp, is_period_object
1717

1818
from pandas._libs.tslibs.np_datetime cimport (
1919
check_dts_bounds, npy_datetimestruct, pandas_datetime_to_datetimestruct,
@@ -37,10 +37,11 @@ from pandas._libs.tslibs.nattype import nat_strings
3737
from pandas._libs.tslibs.nattype cimport (
3838
NPY_NAT, checknull_with_nat, c_NaT as NaT)
3939

40-
from pandas._libs.tslibs.tzconversion import (
41-
tz_localize_to_utc, tz_convert_single)
40+
from pandas._libs.tslibs.tzconversion import tz_localize_to_utc
4241
from pandas._libs.tslibs.tzconversion cimport (
43-
_tz_convert_tzlocal_utc, _tz_convert_tzlocal_fromutc)
42+
_tz_convert_tzlocal_utc, _tz_convert_tzlocal_fromutc,
43+
tz_convert_single
44+
)
4445

4546
# ----------------------------------------------------------------------
4647
# Constants
@@ -286,7 +287,7 @@ cdef convert_to_tsobject(object ts, object tz, object unit,
286287
# Keep the converter same as PyDateTime's
287288
ts = datetime.combine(ts, time())
288289
return convert_datetime_to_tsobject(ts, tz)
289-
elif getattr(ts, '_typ', None) == 'period':
290+
elif is_period_object(ts):
290291
raise ValueError("Cannot convert Period to Timestamp "
291292
"unambiguously. Use to_timestamp")
292293
else:

pandas/_libs/tslibs/frequencies.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import re
33
cimport numpy as cnp
44
cnp.import_array()
55

6-
from pandas._libs.tslibs.util cimport is_integer_object
6+
from pandas._libs.tslibs.util cimport is_integer_object, is_offset_object
77

88
from pandas._libs.tslibs.ccalendar import MONTH_NUMBERS
99

@@ -153,7 +153,7 @@ cpdef get_freq_code(freqstr):
153153
>>> get_freq_code(('D', 3))
154154
(6000, 3)
155155
"""
156-
if getattr(freqstr, '_typ', None) == 'dateoffset':
156+
if is_offset_object(freqstr):
157157
freqstr = (freqstr.rule_code, freqstr.n)
158158

159159
if isinstance(freqstr, tuple):
@@ -451,8 +451,8 @@ cdef str _maybe_coerce_freq(code):
451451
code : string
452452
"""
453453
assert code is not None
454-
if getattr(code, '_typ', None) == 'dateoffset':
455-
# i.e. isinstance(code, ABCDateOffset):
454+
if is_offset_object(code):
455+
# i.e. isinstance(code, DateOffset):
456456
code = code.rule_code
457457
return code.upper()
458458

pandas/_libs/tslibs/nattype.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ from cpython.datetime cimport (
1515
datetime,
1616
timedelta,
1717
)
18+
PyDateTime_IMPORT
1819

1920
from cpython.version cimport PY_MINOR_VERSION
2021

21-
PyDateTime_IMPORT
22-
2322
import numpy as np
2423
cimport numpy as cnp
2524
from numpy cimport int64_t
@@ -30,6 +29,7 @@ from pandas._libs.tslibs.np_datetime cimport (
3029
get_timedelta64_value,
3130
)
3231
cimport pandas._libs.tslibs.util as util
32+
from pandas._libs.tslibs.base cimport is_period_object
3333

3434
from pandas._libs.missing cimport C_NA
3535

@@ -150,7 +150,7 @@ cdef class _NaT(datetime):
150150
elif util.is_offset_object(other):
151151
return c_NaT
152152

153-
elif util.is_integer_object(other) or util.is_period_object(other):
153+
elif util.is_integer_object(other) or is_period_object(other):
154154
# For Period compat
155155
# TODO: the integer behavior is deprecated, remove it
156156
return c_NaT
@@ -186,7 +186,7 @@ cdef class _NaT(datetime):
186186
elif util.is_offset_object(other):
187187
return c_NaT
188188

189-
elif util.is_integer_object(other) or util.is_period_object(other):
189+
elif util.is_integer_object(other) or is_period_object(other):
190190
# For Period compat
191191
# TODO: the integer behavior is deprecated, remove it
192192
return c_NaT

pandas/_libs/tslibs/offsets.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,12 @@ class _BaseOffset:
435435
return self.apply(other)
436436

437437
def __mul__(self, other):
438-
if hasattr(other, "_typ"):
439-
return NotImplemented
440438
if util.is_array(other):
441439
return np.array([self * x for x in other])
442-
return type(self)(n=other * self.n, normalize=self.normalize,
443-
**self.kwds)
440+
elif is_integer_object(other):
441+
return type(self)(n=other * self.n, normalize=self.normalize,
442+
**self.kwds)
443+
return NotImplemented
444444

445445
def __neg__(self):
446446
# Note: we are deferring directly to __mul__ instead of __rmul__, as

pandas/_libs/tslibs/parsing.pyx

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ from pandas._config import get_option
3434

3535
from pandas._libs.tslibs.ccalendar import MONTH_NUMBERS
3636
from pandas._libs.tslibs.nattype import nat_strings, NaT
37-
from pandas._libs.tslibs.util cimport is_array, get_c_string_buf_and_size
37+
from pandas._libs.tslibs.util cimport (
38+
is_array,
39+
is_offset_object,
40+
get_c_string_buf_and_size,
41+
)
3842
from pandas._libs.tslibs.frequencies cimport get_rule_month
3943

4044
cdef extern from "../src/headers/portable.h":
@@ -262,7 +266,7 @@ def parse_time_string(arg: str, freq=None, dayfirst=None, yearfirst=None):
262266
if not isinstance(arg, str):
263267
raise TypeError("parse_time_string argument must be str")
264268

265-
if getattr(freq, "_typ", None) == "dateoffset":
269+
if is_offset_object(freq):
266270
freq = freq.rule_code
267271

268272
if dayfirst is None or yearfirst is None:

pandas/_libs/tslibs/period.pxd

-1
This file was deleted.

pandas/_libs/tslibs/period.pyx

+1-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ 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
40+
from pandas._libs.tslibs.base cimport ABCPeriod, is_period_object
4141

4242
from pandas._libs.tslibs.timestamps import Timestamp
4343
from pandas._libs.tslibs.timezones cimport is_utc, is_tzlocal, get_dst_info
@@ -2452,15 +2452,6 @@ class Period(_Period):
24522452
return cls._from_ordinal(ordinal, freq)
24532453

24542454

2455-
cdef bint is_period_object(object obj):
2456-
"""
2457-
Cython-optimized equivalent of isinstance(obj, Period)
2458-
"""
2459-
# Note: this is significantly faster than the implementation in tslibs.util,
2460-
# only use the util version when necessary to prevent circular imports.
2461-
return isinstance(obj, _Period)
2462-
2463-
24642455
cdef int64_t _ordinal_from_fields(int year, int month, quarter, int day,
24652456
int hour, int minute, int second, freq):
24662457
base, mult = get_freq_code(freq)

pandas/_libs/tslibs/resolution.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
from numpy cimport ndarray, int64_t, int32_t
33

4-
from pandas._libs.tslibs.util cimport get_nat
4+
from pandas._libs.tslibs.util cimport get_nat, is_offset_object
55

66
from pandas._libs.tslibs.np_datetime cimport (
77
npy_datetimestruct, dt64_to_dtstruct)
@@ -118,7 +118,7 @@ def get_freq_group(freq) -> int:
118118
>>> get_freq_group('W-FRI')
119119
4000
120120
"""
121-
if getattr(freq, '_typ', None) == 'dateoffset':
121+
if is_offset_object(freq):
122122
freq = freq.rule_code
123123

124124
if isinstance(freq, str):

pandas/_libs/tslibs/util.pxd

-15
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,6 @@ cdef inline bint is_array(object val):
168168
return PyArray_Check(val)
169169

170170

171-
cdef inline bint is_period_object(object val):
172-
"""
173-
Cython equivalent of `isinstance(val, pd.Period)`
174-
175-
Parameters
176-
----------
177-
val : object
178-
179-
Returns
180-
-------
181-
is_period : bool
182-
"""
183-
return getattr(val, '_typ', '_typ') == 'period'
184-
185-
186171
cdef inline bint is_offset_object(object val):
187172
"""
188173
Check if an object is a DateOffset object.

pandas/core/resample.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1700,8 +1700,8 @@ def _get_period_range_edges(first, last, offset, closed="left", base=0):
17001700
first, last, offset, closed=closed, base=base
17011701
)
17021702

1703-
first = (first + adjust_first * offset).to_period(offset)
1704-
last = (last - adjust_last * offset).to_period(offset)
1703+
first = (first + int(adjust_first) * offset).to_period(offset)
1704+
last = (last - int(adjust_last) * offset).to_period(offset)
17051705
return first, last
17061706

17071707

pandas/tests/tslibs/test_conversion.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
def _compare_utc_to_local(tz_didx):
1414
def f(x):
15-
return conversion.tz_convert_single(x, UTC, tz_didx.tz)
15+
return tzconversion.tz_convert_single(x, UTC, tz_didx.tz)
1616

1717
result = tzconversion.tz_convert(tz_didx.asi8, UTC, tz_didx.tz)
1818
expected = np.vectorize(f)(tz_didx.asi8)
@@ -22,7 +22,7 @@ def f(x):
2222

2323
def _compare_local_to_utc(tz_didx, utc_didx):
2424
def f(x):
25-
return conversion.tz_convert_single(x, tz_didx.tz, UTC)
25+
return tzconversion.tz_convert_single(x, tz_didx.tz, UTC)
2626

2727
result = tzconversion.tz_convert(utc_didx.asi8, tz_didx.tz, UTC)
2828
expected = np.vectorize(f)(utc_didx.asi8)

0 commit comments

Comments
 (0)