Skip to content

Commit 8a9b291

Browse files
jbrockmendeljreback
authored andcommitted
remove is_string_object (#25937)
1 parent b727c6b commit 8a9b291

13 files changed

+35
-58
lines changed

pandas/_libs/index.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ cpdef convert_scalar(ndarray arr, object value):
536536
return Timestamp(value).value
537537
elif value is None or value != value:
538538
return NPY_NAT
539-
elif util.is_string_object(value):
539+
elif isinstance(value, str):
540540
return Timestamp(value).value
541541
raise ValueError("cannot set a Timestamp with a non-timestamp")
542542

@@ -547,7 +547,7 @@ cpdef convert_scalar(ndarray arr, object value):
547547
return Timedelta(value).value
548548
elif value is None or value != value:
549549
return NPY_NAT
550-
elif util.is_string_object(value):
550+
elif isinstance(value, str):
551551
return Timedelta(value).value
552552
raise ValueError("cannot set a Timedelta with a non-timedelta")
553553

pandas/_libs/lib.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ def infer_datetimelike_array(arr: object) -> object:
13771377

13781378
for i in range(n):
13791379
v = arr[i]
1380-
if util.is_string_object(v):
1380+
if isinstance(v, str):
13811381
objs.append(v)
13821382

13831383
if len(objs) == 3:
@@ -2070,7 +2070,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
20702070
else:
20712071
seen.object_ = 1
20722072
break
2073-
elif try_float and not util.is_string_object(val):
2073+
elif try_float and not isinstance(val, str):
20742074
# this will convert Decimal objects
20752075
try:
20762076
floats[i] = float(val)

pandas/_libs/tslib.pyx

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ cnp.import_array()
1717
import pytz
1818

1919
from pandas._libs.util cimport (
20-
is_integer_object, is_float_object, is_string_object, is_datetime64_object)
20+
is_integer_object, is_float_object, is_datetime64_object)
2121

2222

2323
from pandas._libs.tslibs.np_datetime cimport (
@@ -111,7 +111,7 @@ def ints_to_pydatetime(int64_t[:] arr, object tz=None, object freq=None,
111111
elif box == "timestamp":
112112
func_create = create_timestamp_from_ts
113113

114-
if is_string_object(freq):
114+
if isinstance(freq, str):
115115
freq = to_offset(freq)
116116
elif box == "time":
117117
func_create = create_time_from_ts
@@ -383,7 +383,7 @@ def array_with_unit_to_datetime(ndarray values, object unit,
383383
raise AssertionError
384384
iresult[i] = NPY_NAT
385385

386-
elif is_string_object(val):
386+
elif isinstance(val, str):
387387
if len(val) == 0 or val in nat_strings:
388388
iresult[i] = NPY_NAT
389389

@@ -442,7 +442,7 @@ def array_with_unit_to_datetime(ndarray values, object unit,
442442
except:
443443
oresult[i] = val
444444

445-
elif is_string_object(val):
445+
elif isinstance(val, str):
446446
if len(val) == 0 or val in nat_strings:
447447
oresult[i] = NaT
448448

@@ -569,7 +569,7 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
569569
except:
570570
iresult[i] = NPY_NAT
571571

572-
elif is_string_object(val):
572+
elif isinstance(val, str):
573573
# string
574574
seen_string = 1
575575

@@ -660,7 +660,7 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
660660
if is_coerce:
661661
iresult[i] = NPY_NAT
662662
continue
663-
elif require_iso8601 and is_string_object(val):
663+
elif require_iso8601 and isinstance(val, str):
664664
# GH#19382 for just-barely-OutOfBounds falling back to
665665
# dateutil parser will return incorrect result because
666666
# it will ignore nanoseconds
@@ -794,7 +794,7 @@ cdef array_to_datetime_object(ndarray[object] values, bint is_raise,
794794
val = values[i]
795795
if checknull_with_nat(val):
796796
oresult[i] = val
797-
elif is_string_object(val):
797+
elif isinstance(val, str):
798798
if len(val) == 0 or val in nat_strings:
799799
oresult[i] = 'NaT'
800800
continue

pandas/_libs/tslibs/conversion.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ from pandas._libs.tslibs.np_datetime cimport (
2727
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
2828

2929
from pandas._libs.tslibs.util cimport (
30-
is_string_object, is_datetime64_object, is_integer_object, is_float_object)
30+
is_datetime64_object, is_integer_object, is_float_object)
3131

3232
from pandas._libs.tslibs.timedeltas cimport (cast_from_unit,
3333
delta_to_nanoseconds)
@@ -284,7 +284,7 @@ cdef convert_to_tsobject(object ts, object tz, object unit,
284284

285285
obj = _TSObject()
286286

287-
if is_string_object(ts):
287+
if isinstance(ts, str):
288288
return convert_str_to_tsobject(ts, tz, unit, dayfirst, yearfirst)
289289

290290
if ts is None or ts is NaT:
@@ -430,7 +430,7 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
430430

431431
obj = _TSObject()
432432

433-
assert is_string_object(ts)
433+
assert isinstance(ts, str)
434434

435435
if len(ts) == 0 or ts in nat_strings:
436436
ts = NaT
@@ -924,7 +924,7 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None,
924924
result[i] = _tz_convert_tzlocal_utc(v, tz, to_utc=True)
925925
return result
926926

927-
if is_string_object(ambiguous):
927+
if isinstance(ambiguous, str):
928928
if ambiguous == 'infer':
929929
infer_dst = True
930930
elif ambiguous == 'NaT':

pandas/_libs/tslibs/frequencies.pyx

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

7-
from pandas._libs.tslibs.util cimport is_integer_object, is_string_object
7+
from pandas._libs.tslibs.util cimport is_integer_object
88

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

@@ -316,7 +316,7 @@ cpdef object get_freq(object freq):
316316
>>> get_freq('3A')
317317
1000
318318
"""
319-
if is_string_object(freq):
319+
if isinstance(freq, str):
320320
base, mult = get_freq_code(freq)
321321
freq = base
322322
return freq

pandas/_libs/tslibs/offsets.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ cnp.import_array()
1919

2020

2121
from pandas._libs.tslibs cimport util
22-
from pandas._libs.tslibs.util cimport is_string_object, is_integer_object
22+
from pandas._libs.tslibs.util cimport is_integer_object
2323

2424
from pandas._libs.tslibs.ccalendar import MONTHS, DAYS
2525
from pandas._libs.tslibs.ccalendar cimport get_days_in_month, dayofweek
@@ -249,7 +249,7 @@ def _to_dt64(dt, dtype='datetime64'):
249249

250250

251251
def _validate_business_time(t_input):
252-
if is_string_object(t_input):
252+
if isinstance(t_input, str):
253253
try:
254254
t = time.strptime(t_input, '%H:%M')
255255
return dt_time(hour=t.tm_hour, minute=t.tm_min)
@@ -331,7 +331,7 @@ class _BaseOffset(object):
331331
raise AttributeError("DateOffset objects are immutable.")
332332

333333
def __eq__(self, other):
334-
if is_string_object(other):
334+
if isinstance(other, str):
335335
try:
336336
# GH#23524 if to_offset fails, we are dealing with an
337337
# incomparable type so == is False and != is True

pandas/_libs/tslibs/period.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ cdef extern from "src/datetime/np_datetime.h":
2929
npy_datetimestruct *d) nogil
3030

3131
cimport pandas._libs.tslibs.util as util
32-
from pandas._libs.tslibs.util cimport is_period_object, is_string_object
32+
from pandas._libs.tslibs.util cimport is_period_object
3333

3434
from pandas._libs.tslibs.timestamps import Timestamp
3535
from pandas._libs.tslibs.timezones cimport is_utc, is_tzlocal, get_dst_info
@@ -2457,7 +2457,7 @@ class Period(_Period):
24572457
elif is_null_datetimelike(value) or value in nat_strings:
24582458
ordinal = NPY_NAT
24592459

2460-
elif is_string_object(value) or util.is_integer_object(value):
2460+
elif isinstance(value, str) or util.is_integer_object(value):
24612461
if util.is_integer_object(value):
24622462
value = str(value)
24632463
value = value.upper()

pandas/_libs/tslibs/resolution.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
from numpy cimport ndarray, int64_t, int32_t
55

6-
from pandas._libs.tslibs.util cimport is_string_object, get_nat
6+
from pandas._libs.tslibs.util cimport get_nat
77

88
from pandas._libs.tslibs.np_datetime cimport (
99
npy_datetimestruct, dt64_to_dtstruct)
@@ -123,7 +123,7 @@ def get_freq_group(freq):
123123
if getattr(freq, '_typ', None) == 'dateoffset':
124124
freq = freq.rule_code
125125

126-
if is_string_object(freq):
126+
if isinstance(freq, str):
127127
base, mult = get_freq_code(freq)
128128
freq = base
129129
elif isinstance(freq, int):

pandas/_libs/tslibs/strptime.pyx

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ from numpy cimport int64_t
3030
from pandas._libs.tslibs.np_datetime cimport (
3131
check_dts_bounds, dtstruct_to_dt64, npy_datetimestruct)
3232

33-
from pandas._libs.tslibs.util cimport is_string_object
34-
3533
from pandas._libs.tslibs.nattype cimport checknull_with_nat, NPY_NAT
3634
from pandas._libs.tslibs.nattype import nat_strings
3735

@@ -136,7 +134,7 @@ def array_strptime(object[:] values, object fmt,
136134

137135
for i in range(n):
138136
val = values[i]
139-
if is_string_object(val):
137+
if isinstance(val, str):
140138
if val in nat_strings:
141139
iresult[i] = NPY_NAT
142140
continue

pandas/_libs/tslibs/timedeltas.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ PyDateTime_IMPORT
2424
cimport pandas._libs.tslibs.util as util
2525
from pandas._libs.tslibs.util cimport (
2626
is_timedelta64_object, is_datetime64_object, is_integer_object,
27-
is_float_object, is_string_object)
27+
is_float_object)
2828

2929
from pandas._libs.tslibs.ccalendar import DAY_SECONDS
3030

@@ -188,7 +188,7 @@ cdef convert_to_timedelta64(object ts, object unit):
188188
else:
189189
ts = cast_from_unit(ts, unit)
190190
ts = np.timedelta64(ts)
191-
elif is_string_object(ts):
191+
elif isinstance(ts, str):
192192
if len(ts) > 0 and ts[0] == 'P':
193193
ts = parse_iso_format_string(ts)
194194
else:
@@ -539,7 +539,7 @@ cdef bint _validate_ops_compat(other):
539539
return True
540540
elif PyDelta_Check(other) or is_timedelta64_object(other):
541541
return True
542-
elif is_string_object(other):
542+
elif isinstance(other, str):
543543
return True
544544
elif hasattr(other, 'delta'):
545545
return True
@@ -1206,7 +1206,7 @@ class Timedelta(_Timedelta):
12061206

12071207
if isinstance(value, Timedelta):
12081208
value = value.value
1209-
elif is_string_object(value):
1209+
elif isinstance(value, str):
12101210
if len(value) > 0 and value[0] == 'P':
12111211
value = parse_iso_format_string(value)
12121212
else:

pandas/_libs/tslibs/timestamps.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ PyDateTime_IMPORT
1818

1919
from pandas._libs.tslibs.util cimport (
2020
is_datetime64_object, is_timedelta64_object, is_integer_object,
21-
is_string_object, is_array, is_offset_object)
21+
is_array, is_offset_object)
2222

2323
cimport pandas._libs.tslibs.ccalendar as ccalendar
2424
from pandas._libs.tslibs.ccalendar import DAY_SECONDS
@@ -639,7 +639,7 @@ class Timestamp(_Timestamp):
639639
tz : str or timezone object, default None
640640
Timezone to localize to
641641
"""
642-
if is_string_object(tz):
642+
if isinstance(tz, str):
643643
tz = maybe_get_tz(tz)
644644
return cls(datetime.now(tz))
645645

@@ -748,7 +748,7 @@ class Timestamp(_Timestamp):
748748
# User passed tzinfo instead of tz; avoid silently ignoring
749749
tz, tzinfo = tzinfo, None
750750

751-
if is_string_object(ts_input):
751+
if isinstance(ts_input, str):
752752
# User passed a date string to parse.
753753
# Check that the user didn't also pass a date attribute kwarg.
754754
if any(arg is not None for arg in _date_attributes):
@@ -1202,7 +1202,7 @@ class Timestamp(_Timestamp):
12021202
if self.tzinfo is None:
12031203
# tz naive, localize
12041204
tz = maybe_get_tz(tz)
1205-
if not is_string_object(ambiguous):
1205+
if not isinstance(ambiguous, str):
12061206
ambiguous = [ambiguous]
12071207
value = tz_localize_to_utc(np.array([self.value], dtype='i8'), tz,
12081208
ambiguous=ambiguous,

pandas/_libs/tslibs/timezones.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ from numpy cimport int64_t
2020
cnp.import_array()
2121

2222
# ----------------------------------------------------------------------
23-
from pandas._libs.tslibs.util cimport (
24-
is_string_object, is_integer_object, get_nat)
23+
from pandas._libs.tslibs.util cimport is_integer_object, get_nat
2524

2625
cdef int64_t NPY_NAT = get_nat()
2726
cdef object utc_stdlib = timezone.utc
@@ -88,7 +87,7 @@ cpdef inline object maybe_get_tz(object tz):
8887
(Maybe) Construct a timezone object from a string. If tz is a string, use
8988
it to construct a timezone object. Otherwise, just return tz.
9089
"""
91-
if is_string_object(tz):
90+
if isinstance(tz, str):
9291
if tz == 'tzlocal()':
9392
tz = _dateutil_tzlocal()
9493
elif tz.startswith('dateutil/'):

pandas/_libs/tslibs/util.pxd

-20
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ from numpy cimport int64_t
3737

3838
cdef extern from "numpy/arrayobject.h":
3939
PyTypeObject PyFloatingArrType_Type
40-
int _import_array() except -1
4140

4241
cdef extern from "numpy/ndarrayobject.h":
4342
PyTypeObject PyTimedeltaArrType_Type
@@ -56,28 +55,9 @@ cdef inline int64_t get_nat():
5655
return NPY_MIN_INT64
5756

5857

59-
cdef inline int import_array() except -1:
60-
_import_array()
61-
62-
6358
# --------------------------------------------------------------------
6459
# Type Checking
6560

66-
cdef inline bint is_string_object(object obj) nogil:
67-
"""
68-
Cython equivalent of `isinstance(val, compat.string_types)`
69-
70-
Parameters
71-
----------
72-
val : object
73-
74-
Returns
75-
-------
76-
is_string : bool
77-
"""
78-
return PyString_Check(obj) or PyUnicode_Check(obj)
79-
80-
8161
cdef inline bint is_integer_object(object obj) nogil:
8262
"""
8363
Cython equivalent of

0 commit comments

Comments
 (0)