Skip to content

Commit a95d567

Browse files
committed
BUG: move nan-handling to tslib
1 parent 42b8f26 commit a95d567

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

pandas/_libs/tslib.pyx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ def format_array_from_datetime(ndarray[int64_t] values, object tz=None,
296296
return result
297297

298298

299-
def array_with_unit_to_datetime(ndarray values, object unit,
300-
str errors='coerce'):
299+
def array_with_unit_to_datetime(ndarray values, object unit,
300+
str errors='coerce', ndarray mask=None):
301301
"""
302302
convert the ndarray according to the unit
303303
if errors:
@@ -316,7 +316,6 @@ def array_with_unit_to_datetime(ndarray values, object unit,
316316
Py_ssize_t i, j, n=len(values)
317317
int64_t m
318318
ndarray[float64_t] fvalues
319-
ndarray mask
320319
bint is_ignore = errors=='ignore'
321320
bint is_coerce = errors=='coerce'
322321
bint is_raise = errors=='raise'
@@ -329,9 +328,13 @@ def array_with_unit_to_datetime(ndarray values, object unit,
329328

330329
if unit == 'ns':
331330
if issubclass(values.dtype.type, np.integer):
332-
return values.astype('M8[ns]'), tz
333-
# This will return a tz
334-
return array_to_datetime(values.astype(object), errors=errors)
331+
result = values.astype('M8[ns]')
332+
else:
333+
result, tz = array_to_datetime(values.astype(object), errors=errors)
334+
if mask is not None:
335+
iresult = result.view('i8')
336+
iresult[mask] = NPY_NAT
337+
return result, tz
335338

336339
m = cast_from_unit(None, unit)
337340

@@ -343,7 +346,9 @@ def array_with_unit_to_datetime(ndarray values, object unit,
343346
if values.dtype.kind == "i":
344347
# Note: this condition makes the casting="same_kind" redundant
345348
iresult = values.astype('i8', casting='same_kind', copy=False)
346-
mask = iresult == NPY_NAT
349+
# If no mask, fill mask by comparing to NPY_NAT constant
350+
if mask is None:
351+
mask = iresult == NPY_NAT
347352
iresult[mask] = 0
348353
fvalues = iresult.astype('f8') * m
349354
need_to_iterate = False

pandas/core/tools/datetimes.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,12 @@ def _convert_listlike_datetimes(
321321
# GH 30050 pass an ndarray to tslib.array_with_unit_to_datetime
322322
# because it expects an ndarray argument
323323
if isinstance(arg, IntegerArray):
324-
# Send only non-na values to array_with_unit_to_datetime
324+
# Explicitly pass NaT mask to array_with_unit_to_datetime
325325
mask_na = arg.isna()
326+
arg = getattr(arg, "_ndarray_values", arg)
326327
result, tz_parsed = tslib.array_with_unit_to_datetime(
327-
np.compress(np.logical_not(mask_na), arg), unit, errors=errors
328+
arg, unit, mask=mask_na, errors=errors
328329
)
329-
# Insert na values back in proper positions
330-
ins_index = np.ravel(np.argwhere(mask_na))
331-
ins_index -= range(ins_index.shape[0])
332-
result = np.insert(result, ins_index, None)
333330
else:
334331
result, tz_parsed = tslib.array_with_unit_to_datetime(
335332
arg, unit, errors=errors

0 commit comments

Comments
 (0)