Skip to content

REF: avoid importing pd.NA in tslibs #34009

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 5 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions pandas/_libs/missing.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cpdef ndarray[uint8_t] isnaobj(ndarray arr)

cdef bint is_null_datetime64(v)
cdef bint is_null_timedelta64(v)
cdef bint checknull_with_nat_and_na(object obj)

cdef class C_NAType:
pass
Expand Down
5 changes: 5 additions & 0 deletions pandas/_libs/missing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ cdef inline bint is_null_timedelta64(v):
return False


cdef bint checknull_with_nat_and_na(object obj):
# See GH#32214
return checknull_with_nat(obj) or obj is C_NA


# -----------------------------------------------------------------------------
# Implementation of NA singleton

Expand Down
16 changes: 9 additions & 7 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ from pandas._libs.tslibs.conversion cimport (
get_datetime64_nanos)

from pandas._libs.tslibs.nattype import nat_strings
from pandas._libs.tslibs.nattype cimport (
checknull_with_nat, NPY_NAT, c_NaT as NaT)
from pandas._libs.tslibs.nattype cimport NPY_NAT, c_NaT as NaT

from pandas._libs.tslibs.offsets cimport to_offset

Expand All @@ -64,6 +63,9 @@ from pandas._libs.tslibs.tzconversion cimport (
tz_convert_utc_to_tzlocal,
)

# Note: this is the only non-tslibs intra-pandas dependency here
from pandas._libs.missing cimport checknull_with_nat_and_na


cdef inline object create_datetime_from_ts(
int64_t value,
Expand Down Expand Up @@ -438,7 +440,7 @@ def array_with_unit_to_datetime(
for i in range(n):
val = values[i]

if checknull_with_nat(val):
if checknull_with_nat_and_na(val):
iresult[i] = NPY_NAT

elif is_integer_object(val) or is_float_object(val):
Expand Down Expand Up @@ -505,7 +507,7 @@ def array_with_unit_to_datetime(
for i in range(n):
val = values[i]

if checknull_with_nat(val):
if checknull_with_nat_and_na(val):
oresult[i] = <object>NaT
elif is_integer_object(val) or is_float_object(val):

Expand Down Expand Up @@ -602,7 +604,7 @@ cpdef array_to_datetime(
val = values[i]

try:
if checknull_with_nat(val):
if checknull_with_nat_and_na(val):
iresult[i] = NPY_NAT

elif PyDateTime_Check(val):
Expand Down Expand Up @@ -812,7 +814,7 @@ cdef ignore_errors_out_of_bounds_fallback(ndarray[object] values):
val = values[i]

# set as nan except if its a NaT
if checknull_with_nat(val):
if checknull_with_nat_and_na(val):
if isinstance(val, float):
oresult[i] = np.nan
else:
Expand Down Expand Up @@ -874,7 +876,7 @@ cdef array_to_datetime_object(
# 2) datetime strings, which we return as datetime.datetime
for i in range(n):
val = values[i]
if checknull_with_nat(val) or PyDateTime_Check(val):
if checknull_with_nat_and_na(val) or PyDateTime_Check(val):
# GH 25978. No need to parse NaT-like or datetime-like vals
oresult[i] = val
elif isinstance(val, str):
Expand Down
4 changes: 1 addition & 3 deletions pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ from pandas._libs.tslibs.np_datetime cimport (
)
cimport pandas._libs.tslibs.util as util

from pandas._libs.missing cimport C_NA


# ----------------------------------------------------------------------
# Constants
Expand Down Expand Up @@ -809,7 +807,7 @@ cdef inline bint checknull_with_nat(object val):
"""
Utility to check if a value is a nat or not.
"""
return val is None or util.is_nan(val) or val is c_NaT or val is C_NA
return val is None or util.is_nan(val) or val is c_NaT


cpdef bint is_null_datetimelike(object val, bint inat_is_null=True):
Expand Down