Skip to content

Commit c0b41ea

Browse files
jbrockmendelvictor
authored and
victor
committed
move is_null_datetimelike to nattype for self-containment (pandas-dev#21692)
1 parent 7d05dd6 commit c0b41ea

File tree

7 files changed

+81
-65
lines changed

7 files changed

+81
-65
lines changed

pandas/_libs/missing.pxd

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# -*- coding: utf-8 -*-
22
# cython: profile=False
33

4-
cdef bint is_null_datetimelike(object val)
4+
from tslibs.nattype cimport is_null_datetimelike
5+
56
cpdef bint checknull(object val)
67
cpdef bint checknull_old(object val)
8+
9+
cdef bint is_null_datetime64(v)
10+
cdef bint is_null_timedelta64(v)
11+
cdef bint is_null_period(v)

pandas/_libs/missing.pyx

+35-16
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,14 @@ cimport util
1515

1616
from tslibs.np_datetime cimport get_timedelta64_value, get_datetime64_value
1717
from tslibs.nattype import NaT
18+
from tslibs.nattype cimport is_null_datetimelike
1819

1920
cdef double INF = <double> np.inf
2021
cdef double NEGINF = -INF
2122

2223
cdef int64_t NPY_NAT = util.get_nat()
2324

2425

25-
cdef inline bint is_null_datetimelike(object val):
26-
# determine if we have a null for a timedelta/datetime (or integer
27-
# versions)
28-
if util._checknull(val):
29-
return True
30-
elif val is NaT:
31-
return True
32-
elif util.is_timedelta64_object(val):
33-
return val.view('int64') == NPY_NAT
34-
elif util.is_datetime64_object(val):
35-
return val.view('int64') == NPY_NAT
36-
elif util.is_integer_object(val):
37-
return val == NPY_NAT
38-
return False
39-
40-
4126
cdef inline bint _check_all_nulls(object val):
4227
""" utility to check if a value is any type of null """
4328
cdef bint res
@@ -308,3 +293,37 @@ cpdef bint isneginf_scalar(object val):
308293
return True
309294
else:
310295
return False
296+
297+
298+
cdef inline bint is_null_datetime64(v):
299+
# determine if we have a null for a datetime (or integer versions),
300+
# excluding np.timedelta64('nat')
301+
if util._checknull(v):
302+
return True
303+
elif v is NaT:
304+
return True
305+
elif util.is_datetime64_object(v):
306+
return v.view('int64') == NPY_NAT
307+
return False
308+
309+
310+
cdef inline bint is_null_timedelta64(v):
311+
# determine if we have a null for a timedelta (or integer versions),
312+
# excluding np.datetime64('nat')
313+
if util._checknull(v):
314+
return True
315+
elif v is NaT:
316+
return True
317+
elif util.is_timedelta64_object(v):
318+
return v.view('int64') == NPY_NAT
319+
return False
320+
321+
322+
cdef inline bint is_null_period(v):
323+
# determine if we have a null for a Period (or integer versions),
324+
# excluding np.datetime64('nat') and np.timedelta64('nat')
325+
if util._checknull(v):
326+
return True
327+
elif v is NaT:
328+
return True
329+
return False

pandas/_libs/src/inference.pyx

+2-34
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ cdef extern from "numpy/arrayobject.h":
2828
cdef object fields
2929
cdef tuple names
3030

31+
from missing cimport is_null_datetime64, is_null_timedelta64, is_null_period
32+
3133
from util cimport UINT8_MAX, UINT64_MAX, INT64_MAX, INT64_MIN
3234

3335
# core.common import for fast inference checks
@@ -574,40 +576,6 @@ cpdef object infer_datetimelike_array(object arr):
574576
return 'mixed'
575577

576578

577-
cdef inline bint is_null_datetime64(v):
578-
# determine if we have a null for a datetime (or integer versions),
579-
# excluding np.timedelta64('nat')
580-
if util._checknull(v):
581-
return True
582-
elif v is NaT:
583-
return True
584-
elif util.is_datetime64_object(v):
585-
return v.view('int64') == iNaT
586-
return False
587-
588-
589-
cdef inline bint is_null_timedelta64(v):
590-
# determine if we have a null for a timedelta (or integer versions),
591-
# excluding np.datetime64('nat')
592-
if util._checknull(v):
593-
return True
594-
elif v is NaT:
595-
return True
596-
elif util.is_timedelta64_object(v):
597-
return v.view('int64') == iNaT
598-
return False
599-
600-
601-
cdef inline bint is_null_period(v):
602-
# determine if we have a null for a Period (or integer versions),
603-
# excluding np.datetime64('nat') and np.timedelta64('nat')
604-
if util._checknull(v):
605-
return True
606-
elif v is NaT:
607-
return True
608-
return False
609-
610-
611579
cdef inline bint is_datetime(object o):
612580
return PyDateTime_Check(o)
613581

pandas/_libs/tslibs/nattype.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ cdef int64_t NPY_NAT
77
cdef bint _nat_scalar_rules[6]
88

99
cdef bint checknull_with_nat(object val)
10+
cdef bint is_null_datetimelike(object val)

pandas/_libs/tslibs/nattype.pyx

+26
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cimport numpy as cnp
1616
from numpy cimport int64_t
1717
cnp.import_array()
1818

19+
cimport util
1920
from util cimport (get_nat,
2021
is_integer_object, is_float_object,
2122
is_datetime64_object, is_timedelta64_object)
@@ -587,3 +588,28 @@ cdef inline bint checknull_with_nat(object val):
587588
""" utility to check if a value is a nat or not """
588589
return val is None or (
589590
PyFloat_Check(val) and val != val) or val is NaT
591+
592+
593+
cdef inline bint is_null_datetimelike(object val):
594+
"""
595+
Determine if we have a null for a timedelta/datetime (or integer versions)
596+
597+
Parameters
598+
----------
599+
val : object
600+
601+
Returns
602+
-------
603+
null_datetimelike : bool
604+
"""
605+
if util._checknull(val):
606+
return True
607+
elif val is NaT:
608+
return True
609+
elif util.is_timedelta64_object(val):
610+
return val.view('int64') == NPY_NAT
611+
elif util.is_datetime64_object(val):
612+
return val.view('int64') == NPY_NAT
613+
elif util.is_integer_object(val):
614+
return val == NPY_NAT
615+
return False

pandas/_libs/tslibs/period.pyx

+1-3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ cdef extern from "../src/datetime/np_datetime.h":
3636
cimport util
3737
from util cimport is_period_object, is_string_object, INT32_MIN
3838

39-
from pandas._libs.missing cimport is_null_datetimelike
40-
4139
from timestamps import Timestamp
4240
from timezones cimport is_utc, is_tzlocal, get_utcoffset, get_dst_info
4341
from timedeltas cimport delta_to_nanoseconds
@@ -52,7 +50,7 @@ from frequencies cimport (get_freq_code, get_base_alias,
5250
from parsing import parse_time_string, NAT_SENTINEL
5351
from resolution import Resolution
5452
from nattype import nat_strings, NaT, iNaT
55-
from nattype cimport _nat_scalar_rules, NPY_NAT
53+
from nattype cimport _nat_scalar_rules, NPY_NAT, is_null_datetimelike
5654
from offsets cimport to_offset
5755

5856
from pandas.tseries import offsets

setup.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -531,17 +531,6 @@ def pxd(name):
531531
'pyxfile': '_libs/ops',
532532
'pxdfiles': ['_libs/src/util',
533533
'_libs/missing']},
534-
'_libs.tslibs.period': {
535-
'pyxfile': '_libs/tslibs/period',
536-
'pxdfiles': ['_libs/src/util',
537-
'_libs/missing',
538-
'_libs/tslibs/ccalendar',
539-
'_libs/tslibs/timedeltas',
540-
'_libs/tslibs/timezones',
541-
'_libs/tslibs/nattype',
542-
'_libs/tslibs/offsets'],
543-
'depends': tseries_depends + ['pandas/_libs/src/period_helper.h'],
544-
'sources': np_datetime_sources + ['pandas/_libs/src/period_helper.c']},
545534
'_libs.properties': {
546535
'pyxfile': '_libs/properties',
547536
'include': []},
@@ -603,6 +592,16 @@ def pxd(name):
603592
'_libs.tslibs.parsing': {
604593
'pyxfile': '_libs/tslibs/parsing',
605594
'pxdfiles': ['_libs/src/util']},
595+
'_libs.tslibs.period': {
596+
'pyxfile': '_libs/tslibs/period',
597+
'pxdfiles': ['_libs/src/util',
598+
'_libs/tslibs/ccalendar',
599+
'_libs/tslibs/timedeltas',
600+
'_libs/tslibs/timezones',
601+
'_libs/tslibs/nattype',
602+
'_libs/tslibs/offsets'],
603+
'depends': tseries_depends + ['pandas/_libs/src/period_helper.h'],
604+
'sources': np_datetime_sources + ['pandas/_libs/src/period_helper.c']},
606605
'_libs.tslibs.resolution': {
607606
'pyxfile': '_libs/tslibs/resolution',
608607
'pxdfiles': ['_libs/src/util',

0 commit comments

Comments
 (0)