Skip to content

Commit 3b17ba5

Browse files
jbrockmendelPingviinituutti
authored andcommitted
CLN: _try_coerce_result, redundant dtypes.missing method (pandas-dev#24619)
1 parent 492e006 commit 3b17ba5

File tree

8 files changed

+32
-55
lines changed

8 files changed

+32
-55
lines changed

pandas/_libs/missing.pyx

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ cimport pandas._libs.util as util
1212

1313
from pandas._libs.tslibs.np_datetime cimport (
1414
get_timedelta64_value, get_datetime64_value)
15-
from pandas._libs.tslibs.nattype cimport checknull_with_nat
16-
from pandas._libs.tslibs.nattype import NaT
15+
from pandas._libs.tslibs.nattype cimport checknull_with_nat, c_NaT
1716

1817
cdef float64_t INF = <float64_t>np.inf
1918
cdef float64_t NEGINF = -INF
@@ -27,7 +26,7 @@ cdef inline bint _check_all_nulls(object val):
2726

2827
if isinstance(val, (float, complex)):
2928
res = val != val
30-
elif val is NaT:
29+
elif val is c_NaT:
3130
res = 1
3231
elif val is None:
3332
res = 1
@@ -67,7 +66,7 @@ cpdef bint checknull(object val):
6766
return val != val # and val != INF and val != NEGINF
6867
elif util.is_datetime64_object(val):
6968
return get_datetime64_value(val) == NPY_NAT
70-
elif val is NaT:
69+
elif val is c_NaT:
7170
return True
7271
elif util.is_timedelta64_object(val):
7372
return get_timedelta64_value(val) == NPY_NAT
@@ -106,7 +105,7 @@ cpdef bint checknull_old(object val):
106105
return val != val or val == INF or val == NEGINF
107106
elif util.is_datetime64_object(val):
108107
return get_datetime64_value(val) == NPY_NAT
109-
elif val is NaT:
108+
elif val is c_NaT:
110109
return True
111110
elif util.is_timedelta64_object(val):
112111
return get_timedelta64_value(val) == NPY_NAT
@@ -190,7 +189,7 @@ def isnaobj_old(ndarray arr):
190189
result = np.zeros(n, dtype=np.uint8)
191190
for i in range(n):
192191
val = arr[i]
193-
result[i] = val is NaT or _check_none_nan_inf_neginf(val)
192+
result[i] = val is c_NaT or _check_none_nan_inf_neginf(val)
194193
return result.view(np.bool_)
195194

196195

pandas/_libs/tslibs/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# flake8: noqa
33

44
from .conversion import normalize_date, localize_pydatetime, tz_convert_single
5-
from .nattype import NaT, iNaT
5+
from .nattype import NaT, iNaT, is_null_datetimelike
66
from .np_datetime import OutOfBoundsDatetime
77
from .period import Period, IncompatibleFrequency
88
from .timestamps import Timestamp

pandas/_libs/tslibs/nattype.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ cdef _NaT c_NaT
1717

1818

1919
cdef bint checknull_with_nat(object val)
20-
cdef bint is_null_datetimelike(object val)
20+
cpdef bint is_null_datetimelike(object val)

pandas/_libs/tslibs/nattype.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ cdef inline bint checknull_with_nat(object val):
686686
return val is None or util.is_nan(val) or val is c_NaT
687687

688688

689-
cdef inline bint is_null_datetimelike(object val):
689+
cpdef bint is_null_datetimelike(object val):
690690
"""
691691
Determine if we have a null for a timedelta/datetime (or integer versions)
692692

pandas/core/dtypes/missing.py

+3-19
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
_NS_DTYPE, _TD_DTYPE, ensure_object, is_bool_dtype, is_complex_dtype,
1111
is_datetime64_dtype, is_datetime64tz_dtype, is_datetimelike,
1212
is_datetimelike_v_numeric, is_dtype_equal, is_extension_array_dtype,
13-
is_float_dtype, is_integer, is_integer_dtype, is_object_dtype,
14-
is_period_dtype, is_scalar, is_string_dtype, is_string_like_dtype,
15-
is_timedelta64_dtype, needs_i8_conversion, pandas_dtype)
13+
is_float_dtype, is_integer_dtype, is_object_dtype, is_period_dtype,
14+
is_scalar, is_string_dtype, is_string_like_dtype, is_timedelta64_dtype,
15+
needs_i8_conversion, pandas_dtype)
1616
from .generic import (
1717
ABCDatetimeArray, ABCExtensionArray, ABCGeneric, ABCIndexClass,
1818
ABCMultiIndex, ABCSeries, ABCTimedeltaArray)
@@ -339,22 +339,6 @@ def notna(obj):
339339
notnull = notna
340340

341341

342-
def is_null_datelike_scalar(other):
343-
""" test whether the object is a null datelike, e.g. Nat
344-
but guard against passing a non-scalar """
345-
if other is NaT or other is None:
346-
return True
347-
elif is_scalar(other):
348-
349-
# a timedelta
350-
if hasattr(other, 'dtype'):
351-
return other.view('i8') == iNaT
352-
elif is_integer(other) and other == iNaT:
353-
return True
354-
return isna(other)
355-
return False
356-
357-
358342
def _isna_compat(arr, fill_value=np.nan):
359343
"""
360344
Parameters

pandas/core/internals/blocks.py

+18-25
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import numpy as np
99

1010
from pandas._libs import internals as libinternals, lib, tslib, tslibs
11-
from pandas._libs.tslibs import Timedelta, conversion
11+
from pandas._libs.tslibs import Timedelta, conversion, is_null_datetimelike
1212
import pandas.compat as compat
1313
from pandas.compat import range, zip
1414
from pandas.util._validators import validate_bool_kwarg
@@ -31,7 +31,7 @@
3131
ABCDataFrame, ABCDatetimeIndex, ABCExtensionArray, ABCIndexClass,
3232
ABCSeries)
3333
from pandas.core.dtypes.missing import (
34-
_isna_compat, array_equivalent, is_null_datelike_scalar, isna, notna)
34+
_isna_compat, array_equivalent, isna, notna)
3535

3636
import pandas.core.algorithms as algos
3737
from pandas.core.arrays import (
@@ -2085,10 +2085,6 @@ def get_values(self, dtype=None):
20852085
return values
20862086
return self.values
20872087

2088-
@property
2089-
def asi8(self):
2090-
return self.values.view('i8')
2091-
20922088

20932089
class DatetimeBlock(DatetimeLikeBlockMixin, Block):
20942090
__slots__ = ()
@@ -2170,7 +2166,7 @@ def _try_coerce_args(self, values, other):
21702166

21712167
if isinstance(other, bool):
21722168
raise TypeError
2173-
elif is_null_datelike_scalar(other):
2169+
elif is_null_datetimelike(other):
21742170
other = tslibs.iNaT
21752171
elif isinstance(other, (datetime, np.datetime64, date)):
21762172
other = self._box_func(other)
@@ -2183,18 +2179,16 @@ def _try_coerce_args(self, values, other):
21832179
else:
21842180
# coercion issues
21852181
# let higher levels handle
2186-
raise TypeError
2182+
raise TypeError(other)
21872183

21882184
return values, other
21892185

21902186
def _try_coerce_result(self, result):
21912187
""" reverse of try_coerce_args """
21922188
if isinstance(result, np.ndarray):
2193-
if result.dtype.kind in ['i', 'f', 'O']:
2194-
try:
2195-
result = result.astype('M8[ns]')
2196-
except ValueError:
2197-
pass
2189+
if result.dtype.kind in ['i', 'f']:
2190+
result = result.astype('M8[ns]')
2191+
21982192
elif isinstance(result, (np.integer, np.float, np.datetime64)):
21992193
result = self._box_func(result)
22002194
return result
@@ -2378,8 +2372,7 @@ def _try_coerce_args(self, values, other):
23782372
# add the tz back
23792373
other = self._holder(other, dtype=self.dtype)
23802374

2381-
elif (is_null_datelike_scalar(other) or
2382-
(lib.is_scalar(other) and isna(other))):
2375+
elif is_null_datetimelike(other):
23832376
other = tslibs.iNaT
23842377
elif isinstance(other, self._holder):
23852378
if other.tz != self.values.tz:
@@ -2394,17 +2387,19 @@ def _try_coerce_args(self, values, other):
23942387
raise ValueError("incompatible or non tz-aware value")
23952388
other = other.value
23962389
else:
2397-
raise TypeError
2390+
raise TypeError(other)
23982391

23992392
return values, other
24002393

24012394
def _try_coerce_result(self, result):
24022395
""" reverse of try_coerce_args """
24032396
if isinstance(result, np.ndarray):
2404-
if result.dtype.kind in ['i', 'f', 'O']:
2397+
if result.dtype.kind in ['i', 'f']:
24052398
result = result.astype('M8[ns]')
2399+
24062400
elif isinstance(result, (np.integer, np.float, np.datetime64)):
24072401
result = self._box_func(result)
2402+
24082403
if isinstance(result, np.ndarray):
24092404
# allow passing of > 1dim if its trivial
24102405

@@ -2545,32 +2540,30 @@ def _try_coerce_args(self, values, other):
25452540

25462541
if isinstance(other, bool):
25472542
raise TypeError
2548-
elif is_null_datelike_scalar(other):
2543+
elif is_null_datetimelike(other):
25492544
other = tslibs.iNaT
2550-
elif isinstance(other, Timedelta):
2551-
other = other.value
2552-
elif isinstance(other, timedelta):
2553-
other = Timedelta(other).value
2554-
elif isinstance(other, np.timedelta64):
2545+
elif isinstance(other, (timedelta, np.timedelta64)):
25552546
other = Timedelta(other).value
25562547
elif hasattr(other, 'dtype') and is_timedelta64_dtype(other):
25572548
other = other.astype('i8', copy=False).view('i8')
25582549
else:
25592550
# coercion issues
25602551
# let higher levels handle
2561-
raise TypeError
2552+
raise TypeError(other)
25622553

25632554
return values, other
25642555

25652556
def _try_coerce_result(self, result):
25662557
""" reverse of try_coerce_args / try_operate """
25672558
if isinstance(result, np.ndarray):
25682559
mask = isna(result)
2569-
if result.dtype.kind in ['i', 'f', 'O']:
2560+
if result.dtype.kind in ['i', 'f']:
25702561
result = result.astype('m8[ns]')
25712562
result[mask] = tslibs.iNaT
2563+
25722564
elif isinstance(result, (np.integer, np.float)):
25732565
result = self._box_func(result)
2566+
25742567
return result
25752568

25762569
def should_store(self, value):

pandas/core/sparse/series.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
from pandas.compat.numpy import function as nv
1717
from pandas.util._decorators import Appender, Substitution
1818

19-
from pandas.core.dtypes.common import is_scalar
19+
from pandas.core.dtypes.common import is_integer, is_scalar
2020
from pandas.core.dtypes.generic import ABCSeries, ABCSparseSeries
21-
from pandas.core.dtypes.missing import is_integer, isna, notna
21+
from pandas.core.dtypes.missing import isna, notna
2222

2323
from pandas.core import generic
2424
from pandas.core.arrays import SparseArray

pandas/tests/tslibs/test_api.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def test_namespace():
2323

2424
api = ['NaT',
2525
'iNaT',
26+
'is_null_datetimelike',
2627
'OutOfBoundsDatetime',
2728
'Period',
2829
'IncompatibleFrequency',

0 commit comments

Comments
 (0)