Skip to content

remove is_string_object #25937

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
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ cpdef convert_scalar(ndarray arr, object value):
return Timestamp(value).value
elif value is None or value != value:
return NPY_NAT
elif util.is_string_object(value):
elif isinstance(value, str):
return Timestamp(value).value
raise ValueError("cannot set a Timestamp with a non-timestamp")

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

Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ def infer_datetimelike_array(arr: object) -> object:

for i in range(n):
v = arr[i]
if util.is_string_object(v):
if isinstance(v, str):
objs.append(v)

if len(objs) == 3:
Expand Down Expand Up @@ -2070,7 +2070,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
else:
seen.object_ = 1
break
elif try_float and not util.is_string_object(val):
elif try_float and not isinstance(val, str):
# this will convert Decimal objects
try:
floats[i] = float(val)
Expand Down
14 changes: 7 additions & 7 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cnp.import_array()
import pytz

from pandas._libs.util cimport (
is_integer_object, is_float_object, is_string_object, is_datetime64_object)
is_integer_object, is_float_object, is_datetime64_object)


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

if is_string_object(freq):
if isinstance(freq, str):
freq = to_offset(freq)
elif box == "time":
func_create = create_time_from_ts
Expand Down Expand Up @@ -383,7 +383,7 @@ def array_with_unit_to_datetime(ndarray values, object unit,
raise AssertionError
iresult[i] = NPY_NAT

elif is_string_object(val):
elif isinstance(val, str):
if len(val) == 0 or val in nat_strings:
iresult[i] = NPY_NAT

Expand Down Expand Up @@ -442,7 +442,7 @@ def array_with_unit_to_datetime(ndarray values, object unit,
except:
oresult[i] = val

elif is_string_object(val):
elif isinstance(val, str):
if len(val) == 0 or val in nat_strings:
oresult[i] = NaT

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

elif is_string_object(val):
elif isinstance(val, str):
# string
seen_string = 1

Expand Down Expand Up @@ -660,7 +660,7 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
if is_coerce:
iresult[i] = NPY_NAT
continue
elif require_iso8601 and is_string_object(val):
elif require_iso8601 and isinstance(val, str):
# GH#19382 for just-barely-OutOfBounds falling back to
# dateutil parser will return incorrect result because
# it will ignore nanoseconds
Expand Down Expand Up @@ -794,7 +794,7 @@ cdef array_to_datetime_object(ndarray[object] values, bint is_raise,
val = values[i]
if checknull_with_nat(val):
oresult[i] = val
elif is_string_object(val):
elif isinstance(val, str):
if len(val) == 0 or val in nat_strings:
oresult[i] = 'NaT'
continue
Expand Down
8 changes: 4 additions & 4 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ from pandas._libs.tslibs.np_datetime cimport (
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime

from pandas._libs.tslibs.util cimport (
is_string_object, is_datetime64_object, is_integer_object, is_float_object)
is_datetime64_object, is_integer_object, is_float_object)

from pandas._libs.tslibs.timedeltas cimport (cast_from_unit,
delta_to_nanoseconds)
Expand Down Expand Up @@ -284,7 +284,7 @@ cdef convert_to_tsobject(object ts, object tz, object unit,

obj = _TSObject()

if is_string_object(ts):
if isinstance(ts, str):
return convert_str_to_tsobject(ts, tz, unit, dayfirst, yearfirst)

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

obj = _TSObject()

assert is_string_object(ts)
assert isinstance(ts, str)

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

if is_string_object(ambiguous):
if isinstance(ambiguous, str):
if ambiguous == 'infer':
infer_dst = True
elif ambiguous == 'NaT':
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/frequencies.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import re
cimport numpy as cnp
cnp.import_array()

from pandas._libs.tslibs.util cimport is_integer_object, is_string_object
from pandas._libs.tslibs.util cimport is_integer_object

from pandas._libs.tslibs.ccalendar import MONTH_NUMBERS

Expand Down Expand Up @@ -316,7 +316,7 @@ cpdef object get_freq(object freq):
>>> get_freq('3A')
1000
"""
if is_string_object(freq):
if isinstance(freq, str):
base, mult = get_freq_code(freq)
freq = base
return freq
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ cnp.import_array()


from pandas._libs.tslibs cimport util
from pandas._libs.tslibs.util cimport is_string_object, is_integer_object
from pandas._libs.tslibs.util cimport is_integer_object

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


def _validate_business_time(t_input):
if is_string_object(t_input):
if isinsatnce(t_input, str):
try:
t = time.strptime(t_input, '%H:%M')
return dt_time(hour=t.tm_hour, minute=t.tm_min)
Expand Down Expand Up @@ -331,7 +331,7 @@ class _BaseOffset(object):
raise AttributeError("DateOffset objects are immutable.")

def __eq__(self, other):
if is_string_object(other):
if isinstance(other, str):
try:
# GH#23524 if to_offset fails, we are dealing with an
# incomparable type so == is False and != is True
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ cdef extern from "src/datetime/np_datetime.h":
npy_datetimestruct *d) nogil

cimport pandas._libs.tslibs.util as util
from pandas._libs.tslibs.util cimport is_period_object, is_string_object
from pandas._libs.tslibs.util cimport is_period_object

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

elif is_string_object(value) or util.is_integer_object(value):
elif isinsance(value, str) or util.is_integer_object(value):
if util.is_integer_object(value):
value = str(value)
value = value.upper()
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/resolution.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from numpy cimport ndarray, int64_t, int32_t

from pandas._libs.tslibs.util cimport is_string_object, get_nat
from pandas._libs.tslibs.util cimport get_nat

from pandas._libs.tslibs.np_datetime cimport (
npy_datetimestruct, dt64_to_dtstruct)
Expand Down Expand Up @@ -123,7 +123,7 @@ def get_freq_group(freq):
if getattr(freq, '_typ', None) == 'dateoffset':
freq = freq.rule_code

if is_string_object(freq):
if isinstance(freq, str):
base, mult = get_freq_code(freq)
freq = base
elif isinstance(freq, int):
Expand Down
4 changes: 1 addition & 3 deletions pandas/_libs/tslibs/strptime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ from numpy cimport int64_t
from pandas._libs.tslibs.np_datetime cimport (
check_dts_bounds, dtstruct_to_dt64, npy_datetimestruct)

from pandas._libs.tslibs.util cimport is_string_object

from pandas._libs.tslibs.nattype cimport checknull_with_nat, NPY_NAT
from pandas._libs.tslibs.nattype import nat_strings

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

for i in range(n):
val = values[i]
if is_string_object(val):
if isinstance(val, str):
if val in nat_strings:
iresult[i] = NPY_NAT
continue
Expand Down
8 changes: 4 additions & 4 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ PyDateTime_IMPORT
cimport pandas._libs.tslibs.util as util
from pandas._libs.tslibs.util cimport (
is_timedelta64_object, is_datetime64_object, is_integer_object,
is_float_object, is_string_object)
is_float_object)

from pandas._libs.tslibs.ccalendar import DAY_SECONDS

Expand Down Expand Up @@ -188,7 +188,7 @@ cdef convert_to_timedelta64(object ts, object unit):
else:
ts = cast_from_unit(ts, unit)
ts = np.timedelta64(ts)
elif is_string_object(ts):
elif isinstance(ts, str):
if len(ts) > 0 and ts[0] == 'P':
ts = parse_iso_format_string(ts)
else:
Expand Down Expand Up @@ -539,7 +539,7 @@ cdef bint _validate_ops_compat(other):
return True
elif PyDelta_Check(other) or is_timedelta64_object(other):
return True
elif is_string_object(other):
elif isinstance(other, str):
return True
elif hasattr(other, 'delta'):
return True
Expand Down Expand Up @@ -1206,7 +1206,7 @@ class Timedelta(_Timedelta):

if isinstance(value, Timedelta):
value = value.value
elif is_string_object(value):
elif isinstance(value, str):
if len(value) > 0 and value[0] == 'P':
value = parse_iso_format_string(value)
else:
Expand Down
8 changes: 4 additions & 4 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ PyDateTime_IMPORT

from pandas._libs.tslibs.util cimport (
is_datetime64_object, is_timedelta64_object, is_integer_object,
is_string_object, is_array, is_offset_object)
is_array, is_offset_object)

cimport pandas._libs.tslibs.ccalendar as ccalendar
from pandas._libs.tslibs.ccalendar import DAY_SECONDS
Expand Down Expand Up @@ -639,7 +639,7 @@ class Timestamp(_Timestamp):
tz : str or timezone object, default None
Timezone to localize to
"""
if is_string_object(tz):
if isinstance(tz, str):
tz = maybe_get_tz(tz)
return cls(datetime.now(tz))

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

if is_string_object(ts_input):
if isinstance(ts_input, str):
# User passed a date string to parse.
# Check that the user didn't also pass a date attribute kwarg.
if any(arg is not None for arg in _date_attributes):
Expand Down Expand Up @@ -1202,7 +1202,7 @@ class Timestamp(_Timestamp):
if self.tzinfo is None:
# tz naive, localize
tz = maybe_get_tz(tz)
if not is_string_object(ambiguous):
if not isinstance(ambiguous, str):
ambiguous = [ambiguous]
value = tz_localize_to_utc(np.array([self.value], dtype='i8'), tz,
ambiguous=ambiguous,
Expand Down
5 changes: 2 additions & 3 deletions pandas/_libs/tslibs/timezones.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ from numpy cimport int64_t
cnp.import_array()

# ----------------------------------------------------------------------
from pandas._libs.tslibs.util cimport (
is_string_object, is_integer_object, get_nat)
from pandas._libs.tslibs.util cimport is_integer_object, get_nat

cdef int64_t NPY_NAT = get_nat()
cdef object utc_stdlib = timezone.utc
Expand Down Expand Up @@ -88,7 +87,7 @@ cpdef inline object maybe_get_tz(object tz):
(Maybe) Construct a timezone object from a string. If tz is a string, use
it to construct a timezone object. Otherwise, just return tz.
"""
if is_string_object(tz):
if isinstance(tz, str):
if tz == 'tzlocal()':
tz = _dateutil_tzlocal()
elif tz.startswith('dateutil/'):
Expand Down
20 changes: 0 additions & 20 deletions pandas/_libs/tslibs/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ from numpy cimport int64_t

cdef extern from "numpy/arrayobject.h":
PyTypeObject PyFloatingArrType_Type
int _import_array() except -1
Copy link
Member

Choose a reason for hiding this comment

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

Was this just dead code?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes. I think i introduced it a while back as part of a failed attempt to get rid of the NPY_DEPRECATED_API warnings.


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


cdef inline int import_array() except -1:
_import_array()


# --------------------------------------------------------------------
# Type Checking

cdef inline bint is_string_object(object obj) nogil:
"""
Cython equivalent of `isinstance(val, compat.string_types)`

Parameters
----------
val : object

Returns
-------
is_string : bool
"""
return PyString_Check(obj) or PyUnicode_Check(obj)


cdef inline bint is_integer_object(object obj) nogil:
"""
Cython equivalent of
Expand Down